Giter Site home page Giter Site logo

alejandroherr / async-bmp280 Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 463 KB

Interface for the weather sensor BMP280

Home Page: https://async-bmp280.alejandroherr.io

License: MIT License

JavaScript 2.25% TypeScript 97.75%
i2c bmp280 weather weather-sensors temperature-sensor pressure-sensor i2c-sensors raspberry-pi raspberrypi raspberry-pi-3

async-bmp280's Introduction

async-bmp280

Npm Package Dependencies Dev Dependencies CircleCI semantic-release MIT License

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.

function BMP280({ bus }: { bus: BusInterface }): 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):

init(params?: Partial<BMP280ControlMeasurement & BMP280Config>): 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

import { Bus } from 'async-i2c-bus';
import { BMP280, REGISTERS, OFFSETS, OVERSAMPLING, MODE, STANDBY_TIME, IIR_FILTER } from 'async-i2c-bus';

// ...

const bus = Bus();
await bus.open();

const bmp280 = BMP280({ bus });

// init version
await bmp280.init();

// no-init version;

await bmp280.reset();
await bmp280.readTemperatureCorrection();
await bmp280.readPressureCorrection();

await bmp280.writeByte(
  REGISTERS.CTRL_MEAS,
  (OVERSAMPLING.x1 << OFFSETS.OSRS_T) | (OVERSAMPLING.x1 << OFFSETS.OSRS_P) | MODE.NORMAL,
);
await bmp280.writeByte(REGISTERS.CONFIG, (STANDBY_TIME['500us'] << OFFSETS.T_SB) | (IIR_FILTER.x16 << OFFSETS.FILTER));

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.

Read/write config and ctrl_meas

The module exports There's two handy methods to read/write the registers config and ctrl_meas.

write

writeControlMeasurement(controlMeasurement: Partial<BMP280ControlMeasurement>): Promise<BMP280Interface>
writeConfig(controlMeasurement: Partial<BMP280Config>): 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.writeConfig({ iirFilter: 'x16' });

// it is equivalent as:

const currentValue = await bmp280.readByte(REGISTERS.config);
const nextValue = (currentValue ^ (MASKS.FILTER << OFFSETS.FILTER)) | (IIR_FILTER.x16 << OFFSETS.FILTER);
await bmp280.writeByte(REGISTERS.CONFIG, nextValue);

read

Read is the inverse function of the previous two functions:

readControlMeasurement(): Promise<BMP280ControlMeasurement>
readConfig(): Promise<BMP280Config>

Example:

await bmp280.writeConfig({ iirFilter: 'x16', standbyTime: '4s' });
await bmp280.readConfig(); // Returns { iirFilter: 'x16', standbyTime: '4s' }

readTemperature and readPressure

readTemperature(): Promise<number>
readPressure(): 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.writeByte(
  REGISTERS.CTRL_MEAS,
  (OVERSAMPLING.x1 << OFFSETS.OSRS_T) | (OVERSAMPLING.x1 << OFFSETS.OSRS_P) | MODE.NORMAL,
);

const ctrlMeas = await bmp280.readByte(REGISTERS.CTRL_MEAS);
const temperatureOversampling = (ctrlMeas & MASKS.OSRS_T) >>> OFFSETS.OSRS_T;

Full example of NORMAL (mode) usage

import { Bus } from 'async-i2c-bus';
import { BMP280 } from 'async-i2c-bus';

const main = async () => {
  const busNumber = 1;
  const bus = Bus({ busNumber });

  await bus.open();

  const bmp280 = BMP280({ bus });

  await bmp280.init();

  let temperature = 0;
  let pressure = 0;

  /** Read temperature/pressure every second */
  while (1) {
    [temperature, pressure] = await Promise.all([bmp280.readTemperature(), bmp280.readPressure()]);

    console.log(`Temperature: ${temperature}°C`);
    console.log(`Pressure: ${pressure}Pa`);

    await new Promise(resolve => {
      setTimeout(() => {
        resolve();
      }, 1000);
    });
  }
};

Example of FORCED (mode) usage

import { Bus } from 'async-i2c-bus';
import { BMP280, IIR_FILTER, MODE, OVERSAMPLING } from 'async-i2c-bus';

const main = async () => {
  const busNumber = 1;
  const bus = Bus({ busNumber });

  await bus.open();

  const bmp280 = BMP280({ bus });

  // Use your values
  await bmp280.init({
    temperatureOversampling: OVERSAMPLING.x16,
    pressureOversampling: OVERSAMPLING.x16,
    mode: MODE.FORCED,
    iirFilter: IIR_FILTER.x0,
  });

  /** Read temperature/pressure once */
  const [temperature, pressure] = await Promise.all([bmp280.readTemperature(), bmp280.readPressure()]);

  console.log(`Temperature: ${temperature}°C`);
  console.log(`Pressure: ${pressure}Pa`);
};

async-bmp280's People

Contributors

alejandroherr avatar renovate-bot avatar

Watchers

 avatar  avatar  avatar

async-bmp280's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm npm-run-all Available
npm tslint Unavailable
npm tslint-immutable Unavailable

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): replace dependency npm-run-all with npm-run-all2 5.0.0
  • fix(deps): update dependency typescript to v3.9.10
  • chore(deps): update dependency node to v20
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency tslint to v6
  • chore(deps): update dependency typedoc-plugin-markdown to v4
  • fix(deps): update dependency typescript to v5
  • 🔐 Create all rate-limited PRs at once 🔐

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • async-i2c-bus ^1.0.0
  • typescript ^3.2.2
  • @semantic-release/changelog 3.0.2
  • @types/jest 24.0.24
  • cz-conventional-changelog 2.1.0
  • gh-pages 2.0.1
  • jest 24.7.1
  • npm-run-all 4.1.5
  • prettier 1.17.0
  • semantic-release 15.13.3
  • simple-git 1.110.0
  • ts-jest 24.0.2
  • tslint 5.16.0
  • tslint-config-prettier 1.18.0
  • tslint-immutable 5.5.2
  • typedoc 0.14.2
  • typedoc-plugin-markdown 1.2.0
  • node >=8.10.0
nvm
.nvmrc
  • node 10.15.3

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.