¡Buenas tardes a todos!
Por alguna razón, hay mucha información sobre TCA9548A en Python, pero no mucha sobre NodeJS.
Estaba tratando de usar esta biblioteca de Github TCA9548A con esta biblioteca BME280
Ambos sensores BME280 tienen la dirección 0x76.
TCA9548A - 0x70.El código final que estaba ejecutando era este:
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); }Salida y problema: solo se lee un sensor y se duplica la información de salida. Este código no está leyendo información de ambos, sino solo de uno.
No soy muy fuerte en JavaScript, pero realmente quiero aprender. Parece que necesito crear una devolución de llamada, desde este comentario en el código:
//utilice una devolución de llamada para asegurarse de que el puerto esté habilitado antes de continuar con otro procesamiento
Apreciaré mucho cualquier ayuda sobre este tema.
Código que funcionó para mí:
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();PRODUCCIÓN:
Two { temperature: 28.303820903622544, pressure: 991.7407636221005, humidity: 39.50036416630162 } Seven { temperature: 24.77324210727238, pressure: 991.0048461479205, humidity: 25.391542980733707 }