async-bmp280
JavaScript interface to control the temperature and pressure sensors BMP280, like the one used in the Enviro pHat. The BMP280Interface
extends the DeviceInterface
of async-i2c-bus.
Installation
To use this library you will also have to install async-i2c-bus.
Yarn:
yarn add async-i2c-bus async-i2c-bus
or npm:
npm i -P async-i2c-bus async-i2c-bus
And you're ready to go.
Requirements
The package requires node v8.10.x
or higher.
If you need a compatibility with lower versions of node, you can build it. To do so clone the repo in your workspace, and modify the target
options in the tsconfig.json
, e.g:
"compilerOptions": "target": "es5" // <-- Line changed "outDir": "dist/main" "rootDir": "src" // ..
And build the module with yarn build
or npm run build
.
Usage
The BMP280
factory takes as argument an instance of the BusInterface
and returns an instance of the BMP280Interface
.
: BMP280Interface;
The BMP280Interfaces
inherits from DeviceInterface
, hence all the low level methods such as writeByte
, readByte
,... are available to work with the device.
But it also offers some specific methods to work with the sensor.
init
and configuration
The init
method performs a reset of the device, acquires temperature/pressure correction and configures the device with the values selected (if none present, it will use the default ones):
: Promise<BMP280Interface>;
The interfaces in the params are:
interface BMP280ControlMeasurement temperatureOversampling: BMP280Oversampling; pressureOversampling: BMP280Oversampling; mode: BMP280Mode;interface BMP280Config standbyTime: BMP280StandbyTime; iirFilter: BMP280IirFilter; // Types:type BMP280Oversampling = 'x0' | 'x1' | 'x2' | 'x4' | 'x8' | 'x16';type BMP280Mode = 'SLEEP' | 'FORCED' | 'NORMAL';type BMP280StandbyTime = '500us' | '62ms' | '125ms' | '250ms' | '500ms' | '1s' | '2s' | '4s';type BMP280IirFilter = 'x0' | 'x1' | 'x2' | 'x4' | 'x8' | 'x16';
This is the recommended way of initializing the sensor.
If you don't use it, be sure to call readTemperatureCorrection
and readPressureCorrection
to be able to read the right temperature/pressure values.
Example of init
vs no init
;; // ... const bus = ;await bus; const bmp280 = ; // init versionawait bmp280; // no-init version; await bmp280;await bmp280;await bmp280; await bmp280;await bmp280;
After this step, the device is ready to readTemperature
and to readPressure
.
For more details, check the full auto-generated documentation and get familiar with BMP280 datasheet.
config
and ctrl_meas
Read/write The module exports
There's two handy methods to read/write the registers config
and ctrl_meas
.
write
: Promise<BMP280Interface>: Promise<BMP280Interface>
Both functions will apply the values passed in the argument and apply them on the current value. That means that it is possible to change only one value or more in the register and leave the rest untouched.
await bmp280; // it is equivalent as: const currentValue = await bmp280;const nextValue = currentValue ^ MASKSFILTER << OFFSETSFILTER | IIR_FILTERx16 << OFFSETSFILTER;await bmp280;
read
Read is the inverse function of the previous two functions:
: Promise<BMP280ControlMeasurement>: Promise<BMP280Config>
Example:
await bmp280;await bmp280; // Returns { iirFilter: 'x16', standbyTime: '4s' }
readTemperature
and readPressure
: Promise<number>: Promise<number>
Read temperature returns the celsius degrees. Read pressure returns the pressure in Pascals.
Constants
In case that you want or need to work with lower level method, the module exposes several constants to work with:
Example:
// ...await bmp280; const ctrlMeas = await bmp280;const temperatureOversampling = ctrlMeas & MASKSOSRS_T >>> OFFSETSOSRS_T;
NORMAL
(mode) usage
Full example of ;; const main = async { const busNumber = 1; const bus = ; await bus; const bmp280 = ; await bmp280; let temperature = 0; let pressure = 0; /** Read temperature/pressure every second */ while 1 temperature pressure = await Promiseallbmp280 bmp280; console; console; await { ; }; };
FORCED
(mode) usage
Example of ;; const main = async { const busNumber = 1; const bus = ; await bus; const bmp280 = ; // Use your values await bmp280; /** Read temperature/pressure once */ const temperature pressure = await Promiseallbmp280 bmp280; console; console;};