Good evening everyone!
For some reason there are a lot information on TCA9548A in Python, but not much on NodeJS.
I was trying to use this library from Github TCA9548A with this library BME280
Both BME280 sensors have address 0x76.
TCA9548A - 0x70.The final code I was running was this:
const tca9548a_1 = new TCA9548A({addr: 0x70, bus: 1});
//as an example, if using two bme280 temp sensors (sensor1 and sensor2)
//that have the same address need to enable the specific
//multiplexer port each time you want to read from a certain device
//singlePortOn activates the port of the argument and
//disables all other ports
//argument has to be a number 0-7
//
//use a callback to ensure that the port is enabled
//before proceeding with other processing
//
//for example, sensor1 is attached to port 2 on the multiplexer
//
tca9548a_1.singlePortOn(2, doSomethingWithSensor());
//then read from sensor2 attached to port 6 on the multiplexer
//
tca9548a_1.singlePortOn(7, doSomethingWithSensor());
function doSomethingWithSensor () {
//process sensor data magic
const bme280 = require('bme280');
const format = number => (Math.round(number * 100) / 100).toFixed(2);
const delay = millis => new Promise(resolve => setTimeout(resolve, millis));
const reportContinuous = async _ => {
const sensor = await bme280.open({
i2cBusNumber: 1,
i2cAddress: 0x76,
humidityOversampling: bme280.OVERSAMPLE.X1,
pressureOversampling: bme280.OVERSAMPLE.X16,
temperatureOversampling: bme280.OVERSAMPLE.X2,
filterCoefficient: bme280.FILTER.F16
});
for (let i = 1; i <= 250; ++i) {
const reading = await sensor.read();
console.log(
`${i} ` +
`${format(reading.temperature)}°C, ` +
`${format(reading.pressure)} hPa, ` +
`${format(reading.humidity)}%`
);
await delay(1000); // 1 second
}
await sensor.close();
};
reportContinuous().catch(console.log);
}
Output and the problem: Only one sensor is being read and is outputting information that is doubled. This code is not reading information from them both, but just from one.
I am not very strong in JavaScript, but really want to learn. Seems like I need to create a callback - from this comment in the code:
//use a callback to ensure that the port is enabled before proceeding with other processing
Will greatly appreciate any help on this subject.
Code that worked for me:
const TCA9548A = require('tca9548a');
const i2c = require('i2c-bus');
const tca9548a_1 = new TCA9548A({ addr: 0x70, bus: 1 });
function two(callback) {
tca9548a_1.singlePortOn(2);
console.log("Two")
const bme280 = require('bme280');
bme280.open().then(async sensor => {
console.log(await sensor.read());
await sensor.close();
}).catch(console.log);
callback()
}
function seven(callback) {
return setTimeout(() => {
tca9548a_1.singlePortOn(7);
console.log("Seven")
const bme280 = require('bme280');
bme280.open().then(async sensor => {
console.log(await sensor.read());
await sensor.close();
}).catch(console.log);
callback()
}, 1000);
}
function run() {
two(() => {
seven(() => { })
})
}
run();
OUTPUT:
Two
{
temperature: 28.303820903622544,
pressure: 991.7407636221005,
humidity: 39.50036416630162
}
Seven
{
temperature: 24.77324210727238,
pressure: 991.0048461479205,
humidity: 25.391542980733707
}