Giter Site home page Giter Site logo

bme280-driver's Introduction

BME280 library for Mongoose OS

Forked from https://github.com/BoschSensortec/BME280_driver

Example:

#include "mgos_bme280.h"
sensor_data data;
mgos_bme280_get_sensor_data(&data);
LOG(LL_INFO, ("Temperature: %0.2f\tPressure: %0.3f kPa\tHumidity: %0.3f %%\n", data.temperature, data.pressure, data.humidity));

JavaScript:

load('api_config.js');
load('api_sys.js');
load('api_timer.js');
load('api_bme280.js');

Timer.set(1000 /* 1 sec */, true /* repeat */, function () {
  let sensor = BME280.read();
  if (sensor) {
    print('temperature:', sensor.temperature, '*C -- humidity:', sensor.humidity, '%RH -- pressure:', sensor.pressure, 'hPa');
  }else{
    print('no sensor found');
  }
}, null);

Information below applies to Bosch driver. Ignore for Mongoose OS.

BME280 sensor API

Introduction

This package contains the Bosch Sensortec's BME280 pressure sensor driver (sensor API)

The sensor driver package includes bme280.c, bme280.h and bme280_defs.h files.

Version

File Version Date
bme280.c 3.2.0 21 Mar 2017
bme280.h 3.2.0 21 Mar 2017
bme280_defs.h 3.2.0 21 Mar 2017

Integration details

  • Integrate bme280.h, bme280_defs.h and bme280.c file in to the project.
  • Include the bme280.h file in your code like below.
#include "bme280.h"

File information

  • bme280_defs.h : This header file has the constants, macros and datatype declarations.
  • bme280.h : This header file contains the declarations of the sensor driver APIs.
  • bme280.c : This source file contains the definitions of the sensor driver APIs.

Supported sensor interfaces

  • SPI 4-wire
  • I2C

SPI 3-wire is currently not supported in the API.

Usage guide

Initializing the sensor

To initialize the sensor, user need to create a device structure. User can do this by creating an instance of the structure bme280_dev. After creating the device strcuture, user need to fill in the various parameters as shown below.

Example for SPI 4-Wire

struct bme280_dev dev;
int8_t rslt = BME280_OK;

/* Sensor_0 interface over SPI with native chip select line */
dev.id = 0;
dev.interface = BME280_SPI_INTF;
dev.read = user_spi_read;
dev.write = user_spi_write;
dev.delay_ms = user_delay_ms;

rslt = bme280_init(&dev);

Example for I2C

struct bme280_dev dev;
int8_t rslt = BME280_OK;

dev.id = BME280_I2C_ADDR_PRIM;
dev.interface = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;

rslt = bme280_init(&dev);

Regarding compensation functions for temperature,pressure and humidity we have two implementations.

  1. Double precision floating point version
  2. Integer version

By default, integer version is used in the API. If user needs double version, user has to enable FLOATING_POINT_REPRESENTATION macro in bme280_defs.h file.

In integer compensation functions, we also have below two implementations for pressure.

  1. For 32 bit machine.
  2. For 64 bit machine.

By default, 64 bit variant is used in the API. If user wants 32 bit variant, user can disable the macro MACHINE_64_BIT in bme280_defs.h file.

Get sensor data

Get sensor data in forced mode

int8_t get_sensor_data_forced_mode(struct bme280_dev *dev)
{
	int8_t rslt;
	uint8_t settings_sel;
	struct bme280_data comp_data;

	/* Continuously get the sensor data */
	while (1) {
		dev->settings.osr_h = BME280_OVERSAMPLING_4X;
		dev->settings.osr_p = BME280_OVERSAMPLING_4X;
		dev->settings.osr_t = BME280_OVERSAMPLING_4X;

		settings_sel = BME280_OSR_PRESS_SEL|BME280_OSR_TEMP_SEL|BME280_OSR_HUM_SEL;

		rslt = bme280_set_sensor_settings(settings_sel, dev);
		rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
		/* Give some delay for the sensor to go into force mode */
		dev->delay_ms(5);
		rslt = bme280_get_sensor_data(BME280_PRESS | BME280_HUM | BME280_TEMP, &comp_data, dev);
		print_sensor_data(&comp_data);
	}
	return rslt;
}

void print_sensor_data(struct bme280_data *comp_data)
{
#ifdef FLOATING_POINT_REPRESENTATION
		printf("%0.2f\t\t%0.2f\t\t%0.2f\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#else
		printf("%ld\t\t%ld\t\t%ld\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#endif
}
Get sensor data in normal mode
int8_t get_sensor_data_normal_mode(struct bme280_dev *dev)
{
	int8_t rslt;
	uint8_t settings_sel;
	struct bme280_data comp_data;

	dev->settings.osr_h = BME280_OVERSAMPLING_4X;
	dev->settings.osr_p = BME280_OVERSAMPLING_4X;
	dev->settings.osr_t = BME280_OVERSAMPLING_4X;

	settings_sel = BME280_OSR_PRESS_SEL|BME280_OSR_TEMP_SEL|BME280_OSR_HUM_SEL;
	rslt = bme280_set_sensor_settings(settings_sel, dev);
	rslt = bme280_set_sensor_mode(BME280_NORMAL_MODE, dev);
	/* Give some delay for the sensor to go into normal mode */
	dev->delay_ms(5);
	
	while (1) {
		rslt = bme280_get_sensor_data(BME280_PRESS | BME280_HUM | BME280_TEMP, &comp_data, dev);
		print_sensor_data(&comp_data);
	}

	return rslt;
}

void print_sensor_data(struct bme280_data *comp_data)
{
#ifdef FLOATING_POINT_REPRESENTATION
		printf("%0.2f\t\t%0.2f\t\t%0.2f\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#else
		printf("%ld\t\t%ld\t\t%ld\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#endif
}

Copyright (C) 2016 - 2017 Bosch Sensortec GmbH

bme280-driver's People

Contributors

anyhotcountry avatar bst-github-admin avatar shakataganai avatar

Watchers

 avatar

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.