Node.js I2C driver for the BME280 humidity, pressure and temperature sensor
MIT License
Node.js I2C driver for the BME280 humidity, pressure and temperature sensor on Linux boards like the Raspberry Pi or BeagleBone.
Supports Node.js versions 10, 12, 14, 15 and 16.
npm install bme280
const bme280 = require('bme280');
bme280.open().then(async sensor => {
console.log(await sensor.read());
await sensor.close();
}).catch(console.log);
Sample output:
{
temperature: 22.80022401222959,
pressure: 990.4595757059205,
humidity: 51.50271664115457
}
const bme280 = require('bme280');
const delay = millis => new Promise(resolve => setTimeout(resolve, millis));
const forcedRead = async sensor => {
await sensor.triggerForcedMeasurement();
await delay(sensor.maximumMeasurementTime());
console.log(await sensor.read());
}
bme280.open({forcedMode: true}).then(sensor => {
setInterval(_ => {
forcedRead(sensor).catch(console.log);
}, 1000);
}).catch(console.log);
open
Here the BME280 is configured to run using oversampling and filtering options recommended for indoor navigation by the BME280 datasheet.
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: 0x77,
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(sensor.typicalMeasurementTime()); // 40 milliseconds, 25Hz
}
await sensor.close();
};
reportContinuous().catch(console.log);
Sample output:
1 23.57°C, 988.07 hPa, 50.16%
2 23.57°C, 988.07 hPa, 50.15%
3 23.57°C, 988.07 hPa, 50.19%
4 23.57°C, 988.07 hPa, 50.17%
5 23.57°C, 988.06 hPa, 50.19%
6 23.57°C, 988.06 hPa, 50.17%
...
Returns a Promise that will be resolved with a Bme280 object on success, or will be rejected if an error occurs.
The default behavior of open is to configure the BME280 on I2C bus 1 at address 0x77 to run in normal mode. The oversampling defaults for humidity, pressure and temperature are OVERSAMPLE.X1 and the default filterCoefficient is FILTER.OFF. The default standby period in normal mode is STANDBY.MS_0_5 for 0.5 milliseconds. Options are available for overriding these defaults.
If desired, the BME280 can be configured to run in forced mode rather than in normal mode by setting option forcedMode to true.
Normal mode comprises an automated perpetual cycling between an active measurement period and an inactive standby period. If the BME280 is configured to run in normal mode, open waits (asynchronously) until the BME280 has completed its first measurement before resolving. This makes it possible to invoke the read method immediately after invoking open to get the first reading.
In forced mode each measurement must be explicitly triggered. If the BME280 is configured to run forced mode, open will not trigger a measurement. It is the responsibility of the application to trigger each measurement and to wait for the measurement to complete before invoking read to get the reading.
The following options are supported:
Returns a Promise that will be resolved with an object containing the last sensor reading on success, or will be rejected if an error occurs.
An object containing a sensor reading has the following properties:
Returns a Promise that will be resolved with no arguments once the BME280 has been triggered to perform a forced measurement, or will be rejected if an error occurs.
triggerForcedMeasurement should only be called in forced mode.
Calling triggerForcedMeasurement will only trigger the BME280 to perform a forced measurement. It will not wait for that measurement to complete. It is the responsibility of the application to wait for the measurement to complete before invoking read to get the reading.
Returns the typical measurement time in milliseconds.
The typical measurement time depends on the selected values for humidity, pressure and temperature oversampling.
If OVERSAMPLE.X1 (the default) is used for humidity, pressure and temperature oversampling, the typical measurement time is 8 milliseconds.
If OVERSAMPLE.X16 is used for humidity, pressure and temperature oversampling, the typical measurement time is 98 milliseconds.
Returns the maximum measurement time in milliseconds.
The maximum measurement time depends on the selected values for humidity, pressure and temperature oversampling.
If OVERSAMPLE.X1 (the default) is used for humidity, pressure and temperature oversampling, the maximum measurement time is 10 milliseconds.
If OVERSAMPLE.X16 is used for humidity, pressure and temperature oversampling, the maximum measurement time is 113 milliseconds.
Returns a Promise that will be resolved with no arguments once the underlying resources have been released, or will be rejected if an error occurs while closing.
Controls oversampling of sensor data.
The filter is used to slow down the response to the sensor inputs.
Controls the inactive standby period in normal mode.