Driver for Bosch BME680 sensor added with Examples
Bosch Sensortronic's BME680 is a brand new integrated environmental sensor. It integrates temperature, pressure, humidity and gas sensors with high accuracy within a single unit. These are the examples on how to use the new driver for BME680.
This commit is contained in:
parent
3bf3169535
commit
4927b2e04f
11 changed files with 574 additions and 0 deletions
35
examples/bme680/README.md
Normal file
35
examples/bme680/README.md
Normal file
|
@ -0,0 +1,35 @@
|
|||
# BME680 Driver Examples
|
||||
|
||||
These examples references two addtional drivers [i2c](https://github.com/kanflo/esp-open-rtos-driver-i2c) and [bme680](https://github.com/gschorcht/esp-open-rtos/extras/bme680), which are provided in the `../../extras` folder.
|
||||
|
||||
If you plan to use one or both of this drivers in your own projects, please check the main development pages for updated versions or reported issues.
|
||||
|
||||
## Hardware setup
|
||||
|
||||
There are examples using I2C with one or two sensors as well as examples using SPI with one sensor.
|
||||
|
||||
To run examples with **one sensor** as I2C slave, just use GPIO5 (SCL) and GPIO4 (SDA) to connect to the BME680 sensor's I2C interface.
|
||||
|
||||
If you want to run examples with **two sensors**, you could do this with only one bus and different I2C addresses or with two buses and the same or different I2C addresses. In later case, use GPIO16 (SCL) and GPIO14 (SDA) for the second I2C bus.
|
||||
|
||||
## Example description
|
||||
|
||||
__*shtx_callback_i2c*__
|
||||
|
||||
In this example, only **one sensor** connected to **I2C** is used. It shows how to use a **callback function** to get the results of the periodic measurements. In addition, it shows how to change some optional sensor and measurement parameters. In this example, there is no error handling.
|
||||
|
||||
__*shtx_callback_i2c_two_sensors*__
|
||||
|
||||
In this example, **two sensors** are used which are connected to **different I2C buses** but with the same slave addresses. It shows how to use a **callback function** to get the results of the periodic measurements. In addition, it shows how to change some optional sensor and measurement parameters for both sensors differently. In this example, there is no error handling.
|
||||
|
||||
__*shtx_callback_spi*__
|
||||
|
||||
In this example, only **one sensor** connected to **SPI** is used. It shows how to use a **callback function** to get the results of the periodic measurements. In addition, it shows how to change some optional sensor and measurement parameters. In this example, there is no error handling.
|
||||
|
||||
__*shtx_task_one_sensor*__
|
||||
|
||||
In this example, only **one sensor** connected to **I2C** is used. It shows how to use a **task** in conjunction with function __*bme680_get_values*__ to get the results of the periodic measurements. The task uses the *vTaskDelay* function to perform a periodic execution of the *bme680_get_values* function at the same rate as that of the periodic measurements. In this example, there is no error handling.
|
||||
|
||||
__*shtx_timer_one_sensor*__
|
||||
|
||||
In this example, only **one sensor** connected to **I2C** is used. It shows how to use a **software timer** and a **timer callback function** in conjunction with function __*bme680_get_values*__ to get the results of periodic measurements. The timer callback function executes function *bme680_get_values* to get the results. The timer is started with same period as periodic measurements. Thus, the timer callback function is executed at same rate as periodic measurements. There is no error handling in this example.
|
3
examples/bme680/bme680_callback_i2c/Makefile
Normal file
3
examples/bme680/bme680_callback_i2c/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=BME680_Callback_I2C
|
||||
EXTRA_COMPONENTS = extras/i2c extras/bme680
|
||||
include ../../../common.mk
|
99
examples/bme680/bme680_callback_i2c/bme680_callback_i2c.c
Normal file
99
examples/bme680/bme680_callback_i2c/bme680_callback_i2c.c
Normal file
|
@ -0,0 +1,99 @@
|
|||
/**
|
||||
* Simple example with one sensor connected to I2C bus 0
|
||||
* using BME680 driver and callback function to get the results.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* ESP8266 BME680
|
||||
* GPIO 5 (SCL) -> SCL
|
||||
* GPIO 4 (SDA) -- SDA
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680_drv.h"
|
||||
|
||||
// define I2C interfaces at which BME680 sensors can be connected
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
|
||||
#define BME680_ADDR1 0x76
|
||||
#define BME680_ADDR2 0x77 // default
|
||||
|
||||
static uint32_t sensor;
|
||||
|
||||
/**
|
||||
* Everything you need is a callback function that can handle
|
||||
* callbacks from the background measurement task of the
|
||||
* BME680 driver. This function is called periodically
|
||||
* and delivers the actual and average sensor value sets for
|
||||
* given sensor.
|
||||
*/
|
||||
|
||||
static void my_callback_function (uint32_t sensor,
|
||||
bme680_value_set_t actual,
|
||||
bme680_value_set_t average)
|
||||
{
|
||||
printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) Percent, %.2f (%.2f) hPa, %.2f (%.2f) Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3, sensor,
|
||||
actual.temperature, average.temperature,
|
||||
actual.humidity, average.humidity,
|
||||
actual.pressure, average.pressure,
|
||||
actual.gas, average.gas);
|
||||
}
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** Please note:
|
||||
* Function *bme680_create_sensor* returns a value greater or equal 0
|
||||
* on succes, all other *bme680_* functions return true on success. Using
|
||||
* these return values, you could realize an error handling. Error
|
||||
* handling is not realized in this example due to readability reasons.
|
||||
*/
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
// Init all I2C bus interfaces at which BME680 sensors are connected
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Init BME680 driver itself.
|
||||
bme680_init_driver();
|
||||
|
||||
// Create the sensor with slave address 0x77 (BME680_ADDR2) connected to
|
||||
// I2C bus 0 (I2C_BUS_1). Parameter *cs* is ignored in case of I2C.
|
||||
sensor = bme680_create_sensor (I2C_BUS, BME680_ADDR2, 0);
|
||||
|
||||
// Register the callback function to get measurement results.
|
||||
bme680_set_callback_function (sensor, &my_callback_function);
|
||||
|
||||
// That's it.
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Change the period of measurements (default 1000 ms) to 500 ms.
|
||||
bme680_set_measurement_period (sensor, 500);
|
||||
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling for
|
||||
// temperature and 2x oversampling for pressure. Humidity measurement is
|
||||
// skipped.
|
||||
bme680_set_oversampling_rates(sensor, os_4x, os_2x, none);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms) to
|
||||
// 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor, 200, 100);
|
||||
}
|
||||
|
3
examples/bme680/bme680_callback_i2c_two_sensors/Makefile
Normal file
3
examples/bme680/bme680_callback_i2c_two_sensors/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=BME680_Callback_I2C_Two_Sensors
|
||||
EXTRA_COMPONENTS = extras/i2c extras/bme680
|
||||
include ../../../common.mk
|
|
@ -0,0 +1,117 @@
|
|||
/**
|
||||
* Simple example with two sensors connected to differen I2C busses
|
||||
* using BME680 driver and callback function to get the results.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* ESP8266 Bus 0 BME680_1
|
||||
* GPIO 5 (SCL) -> SCL
|
||||
* GPIO 4 (SDA) -- SDA
|
||||
*
|
||||
* Bus 1 BME680_2
|
||||
* GPIO 12 (SCL) -> SCL
|
||||
* GPIO 14 (SDA) -- SDA
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680_drv.h"
|
||||
|
||||
// define I2C interfaces at which BME680 sensors can be connected
|
||||
#define I2C_1_BUS 0
|
||||
#define I2C_1_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_1_SDA_PIN GPIO_ID_PIN((4))
|
||||
|
||||
#define I2C_2_BUS 1
|
||||
#define I2C_2_SCL_PIN GPIO_ID_PIN((12))
|
||||
#define I2C_2_SDA_PIN GPIO_ID_PIN((14))
|
||||
|
||||
#define BME680_ADDR1 0x76
|
||||
#define BME680_ADDR2 0x77 // default
|
||||
|
||||
static uint32_t sensor1;
|
||||
static uint32_t sensor2;
|
||||
|
||||
/**
|
||||
* Everything you need is a callback function that can handle
|
||||
* callbacks from the background measurement task of the
|
||||
* BME680 driver. This function is called periodically
|
||||
* and delivers the actual and average sensor value sets for
|
||||
* given sensor.
|
||||
*/
|
||||
|
||||
static void my_callback_function (uint32_t sensor,
|
||||
bme680_value_set_t actual,
|
||||
bme680_value_set_t average)
|
||||
{
|
||||
printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) Percent, %.2f (%.2f) hPa, %.2f (%.2f) Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3, sensor,
|
||||
actual.temperature, average.temperature,
|
||||
actual.humidity, average.humidity,
|
||||
actual.pressure, average.pressure,
|
||||
actual.gas, average.gas);
|
||||
}
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** Please note:
|
||||
* Function *bme680_create_sensor* returns a value greater or equal 0
|
||||
* on succes, all other *bme680_* functions return true on success. Using
|
||||
* these return values, you could realize an error handling. Error
|
||||
* handling is not realized in this example due to readability reasons.
|
||||
*/
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
// Init all I2C bus interfaces at which BME680 sensors are connected
|
||||
i2c_init(I2C_1_BUS, I2C_1_SCL_PIN, I2C_1_SDA_PIN, I2C_FREQ_100K);
|
||||
i2c_init(I2C_2_BUS, I2C_2_SCL_PIN, I2C_2_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Init BME680 driver itself.
|
||||
bme680_init_driver();
|
||||
|
||||
// Create the sensors connected to I2C bus 0 (I2C_BUS_1) and (I2C_BUS_1).
|
||||
// They can use same or different addresses. Parameter *cs* is ignored
|
||||
// in case of I2C.
|
||||
sensor1 = bme680_create_sensor (I2C_1_BUS, BME680_ADDR2, 0);
|
||||
sensor2 = bme680_create_sensor (I2C_2_BUS, BME680_ADDR2, 0);
|
||||
|
||||
// Register the callback functions to get measurement results.
|
||||
bme680_set_callback_function (sensor1, &my_callback_function);
|
||||
bme680_set_callback_function (sensor2, &my_callback_function);
|
||||
|
||||
// That's it.
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Change the period of measurements (default 1000 ms) to 2000 ms and
|
||||
// 5000 ms, respectively.
|
||||
bme680_set_measurement_period (sensor1, 2000);
|
||||
bme680_set_measurement_period (sensor2, 5000);
|
||||
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling for
|
||||
// sensor1 and 16x oversampling for sensor2. Humidity measurement is
|
||||
// skipped for sensor1.
|
||||
bme680_set_oversampling_rates(sensor1, os_2x, os_2x, none);
|
||||
bme680_set_oversampling_rates(sensor2, os_8x, os_8x, os_8x);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 15 and 63, respectively.
|
||||
bme680_set_filter_size(sensor1, iir_size_15);
|
||||
bme680_set_filter_size(sensor2, iir_size_63);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms) to
|
||||
// 200 degree Celcius for 100 ms and 400 degrees for 150 ms, respectively.
|
||||
bme680_set_heater_profile (sensor1, 200, 100);
|
||||
bme680_set_heater_profile (sensor2, 400, 150);
|
||||
}
|
||||
|
3
examples/bme680/bme680_callback_spi/Makefile
Normal file
3
examples/bme680/bme680_callback_spi/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=BME680_Callback_SPI
|
||||
EXTRA_COMPONENTS = extras/i2c extras/bme680
|
||||
include ../../../common.mk
|
97
examples/bme680/bme680_callback_spi/bme680_callback_spi.c
Normal file
97
examples/bme680/bme680_callback_spi/bme680_callback_spi.c
Normal file
|
@ -0,0 +1,97 @@
|
|||
/**
|
||||
* Simple example with one sensor connected to SPI bus 1
|
||||
* using BME680 driver and callback function to get the results.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* ESP8266 BME680
|
||||
* GPIO 12 (MISO) <- SDO
|
||||
* GPIO 13 (MOSI) -> SDI
|
||||
* GPIO 14 (SCK) -> SCK
|
||||
* GPIO 2 (CS) -> CS
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680_drv.h"
|
||||
|
||||
#define SPI_BUS 1
|
||||
#define SPI_CS_GPIO 2 // GPIO 15, the default CS of SPI bus 1, can't be used
|
||||
|
||||
#define BME680_ADDR1 0x76
|
||||
#define BME680_ADDR2 0x77 // default
|
||||
|
||||
static uint32_t sensor;
|
||||
|
||||
/**
|
||||
* Everything you need is a callback function that can handle
|
||||
* callbacks from the background measurement task of the
|
||||
* BME680 driver. This function is called periodically
|
||||
* and delivers the actual and average sensor value sets for
|
||||
* given sensor.
|
||||
*/
|
||||
|
||||
static void my_callback_function (uint32_t sensor,
|
||||
bme680_value_set_t actual,
|
||||
bme680_value_set_t average)
|
||||
{
|
||||
printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) Percent, %.2f (%.2f) hPa, %.2f (%.2f) Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3, sensor,
|
||||
actual.temperature, average.temperature,
|
||||
actual.humidity, average.humidity,
|
||||
actual.pressure, average.pressure,
|
||||
actual.gas, average.gas);
|
||||
}
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** Please note:
|
||||
* Function *bme680_create_sensor* returns a value greater or equal 0
|
||||
* on succes, all other *bme680_* functions return true on success. Using
|
||||
* these return values, you could realize an error handling. Error
|
||||
* handling is not realized in this example due to readability reasons.
|
||||
*/
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
// Init BME680 driver itself.
|
||||
bme680_init_driver();
|
||||
|
||||
// Create the sensor at bus SPI_BUS with SPI_CS_GPIO as CS signal.
|
||||
// Parameter *addr* has to be 0 in case of SPI bus.
|
||||
sensor = bme680_create_sensor (SPI_BUS, 0, SPI_CS_GPIO);
|
||||
|
||||
// Register the callback function to get measurement results.
|
||||
bme680_set_callback_function (sensor, &my_callback_function);
|
||||
|
||||
// That's it.
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Change the period of measurements (default 1000 ms) to 500 ms.
|
||||
bme680_set_measurement_period (sensor, 500);
|
||||
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling for
|
||||
// temperature and 2x oversampling for pressure. Humidity measurement is
|
||||
// skipped.
|
||||
bme680_set_oversampling_rates(sensor, os_4x, os_2x, none);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms) to
|
||||
// 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor, 200, 100);
|
||||
|
||||
// That's it.
|
||||
}
|
||||
|
3
examples/bme680/bme680_task_i2c/Makefile
Normal file
3
examples/bme680/bme680_task_i2c/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=BME680_Task_I2C
|
||||
EXTRA_COMPONENTS = extras/i2c extras/bme680
|
||||
include ../../../common.mk
|
106
examples/bme680/bme680_task_i2c/bme680_task_i2c.c
Normal file
106
examples/bme680/bme680_task_i2c/bme680_task_i2c.c
Normal file
|
@ -0,0 +1,106 @@
|
|||
/**
|
||||
* Simple example with one sensor connected to I2C bus 0
|
||||
* using BME680 driver and a user task that polls the results.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* ESP8266 BME680
|
||||
* GPIO 5 (SCL) -> SCL
|
||||
* GPIO 4 (SDA) -- SDA
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680_drv.h"
|
||||
|
||||
// define I2C interfaces at which BME680 sensors can be connected
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
|
||||
#define BME680_ADDR1 0x76
|
||||
#define BME680_ADDR2 0x77 // default
|
||||
|
||||
static uint32_t sensor;
|
||||
|
||||
/**
|
||||
* Task that executes function bme680_get_values to get last
|
||||
* actual and average sensor values.
|
||||
*/
|
||||
void my_user_task(void *pvParameters)
|
||||
{
|
||||
bme680_value_set_t actual;
|
||||
bme680_value_set_t average;
|
||||
|
||||
while (1)
|
||||
{
|
||||
bme680_get_values(sensor, &actual, &average);
|
||||
|
||||
printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) Percent, %.2f (%.2f) hPa, %.2f (%.2f) Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3, sensor,
|
||||
actual.temperature, average.temperature,
|
||||
actual.humidity, average.humidity,
|
||||
actual.pressure, average.pressure,
|
||||
actual.gas, average.gas);
|
||||
|
||||
// Delay value has to be equal or greater than the period of measurment
|
||||
// task
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** Please note:
|
||||
* Function *bme680_create_sensor* returns a value greater or equal 0
|
||||
* on succes, all other *bme680_* functions return true on success. Using
|
||||
* these return values, you could realize an error handling. Error
|
||||
* handling is not realized in this example due to readability reasons.
|
||||
*/
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
// Init all I2C bus interfaces at which BME680 sensors are connected
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Init BME680 driver itself.
|
||||
bme680_init_driver();
|
||||
|
||||
// Create the sensor with slave address 0x77 (BME680_ADDR2) connected to
|
||||
// I2C bus 0 (I2C_BUS_1). Parameter *cs* is ignored in case of I2C.
|
||||
sensor = bme680_create_sensor (I2C_BUS, BME680_ADDR2, 0);
|
||||
|
||||
// Create a task for getting results
|
||||
xTaskCreate(my_user_task, "my_user_task", 256, NULL, 2, NULL);
|
||||
|
||||
// That's it.
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Change the period of measurements (default 1000 ms) to 500 ms.
|
||||
bme680_set_measurement_period (sensor, 500);
|
||||
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling for
|
||||
// temperature and 2x oversampling for pressure. Humidity measurement is
|
||||
// skipped.
|
||||
bme680_set_oversampling_rates(sensor, os_4x, os_2x, none);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms) to
|
||||
// 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor, 200, 100);
|
||||
}
|
||||
|
3
examples/bme680/bme680_timer_i2c/Makefile
Normal file
3
examples/bme680/bme680_timer_i2c/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=BME680_Timer_I2C
|
||||
EXTRA_COMPONENTS = extras/i2c extras/bme680
|
||||
include ../../../common.mk
|
105
examples/bme680/bme680_timer_i2c/bme680_timer_i2c.c
Normal file
105
examples/bme680/bme680_timer_i2c/bme680_timer_i2c.c
Normal file
|
@ -0,0 +1,105 @@
|
|||
/**
|
||||
* Simple example with one sensor connected to I2C bus 0
|
||||
* using BME680 driver and a timer to poll the results.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* ESP8266 BME680
|
||||
* GPIO 5 (SCL) -> SCL
|
||||
* GPIO 4 (SDA) -- SDA
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
#include "i2c/i2c.h"
|
||||
#include "timers.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680_drv.h"
|
||||
|
||||
// define I2C interfaces at which BME680 sensors can be connected
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
|
||||
#define BME680_ADDR1 0x76
|
||||
#define BME680_ADDR2 0x77 // default
|
||||
|
||||
static TimerHandle_t timerHandle;
|
||||
static uint32_t sensor;
|
||||
|
||||
/**
|
||||
* Timer callback function that executes function bme680_get_values to get last
|
||||
* actual and average sensor values for given sensor.
|
||||
*/
|
||||
|
||||
static void my_sensor_timer_cb(TimerHandle_t xTimer)
|
||||
{
|
||||
bme680_value_set_t actual;
|
||||
bme680_value_set_t average;
|
||||
|
||||
bme680_get_values(sensor, &actual, &average);
|
||||
|
||||
printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) Percent, %.2f (%.2f) hPa, %.2f (%.2f) Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3, sensor,
|
||||
actual.temperature, average.temperature,
|
||||
actual.humidity, average.humidity,
|
||||
actual.pressure, average.pressure,
|
||||
actual.gas, average.gas);
|
||||
}
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** Please note:
|
||||
* Function *bme680_create_sensor* returns a value greater or equal 0
|
||||
* on succes, all other *bme680_* functions return true on success. Using
|
||||
* these return values, you could realize an error handling. Error
|
||||
* handling is not realized in this example due to readability reasons.
|
||||
*/
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
// Init all I2C bus interfaces at which BME680 sensors are connected
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Init BME680 driver itself.
|
||||
bme680_init_driver();
|
||||
|
||||
// Create the sensor with slave address 0x77 (BME680_ADDR2) connected to
|
||||
// I2C bus 0 (I2C_BUS_1). Parameter *cs* is ignored in case of I2C.
|
||||
sensor = bme680_create_sensor (I2C_BUS, BME680_ADDR2, 0);
|
||||
|
||||
// Create and start a timer that calls my_sensor_timer_cb every 1000 ms
|
||||
// Timer value has to be equal or greater than the period of measurment
|
||||
// task above
|
||||
|
||||
if ((timerHandle = xTimerCreate("BME680 Trigger", 1000/portTICK_PERIOD_MS,
|
||||
pdTRUE, NULL, my_sensor_timer_cb)))
|
||||
xTimerStart(timerHandle, 0);
|
||||
|
||||
// That's it.
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Change the period of measurements (default 1000 ms) to 500 ms.
|
||||
bme680_set_measurement_period (sensor, 500);
|
||||
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling for
|
||||
// temperature and 2x oversampling for pressure. Humidity measurement is
|
||||
// skipped.
|
||||
bme680_set_oversampling_rates(sensor, os_4x, os_2x, none);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms) to
|
||||
// 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor, 200, 100);
|
||||
}
|
||||
|
Loading…
Reference in a new issue