esp-open-rtos/examples/bmp280/bmp280_example.c
ourairquality f0c43ff5d5 BME280 support.
The BME280 is close to compatible with the existing BMP280 and extends it with support for measuring humidity, so support has been bundled into the bmp280 driver.

The example now auto-detects the device and displays the humidity for the BME280.

The I2C bus initialization has been moved out of the bmp280 driver to support multiple devices.

The check-id and reset logic has been bundled into the driver initialization. It needs to be re-initialized after reset anyway and the chip-id is need to initialize it, just re-initialize to reset.

Support has been added for multiple devices. The calibration data storage needs to be managed by the caller rather than static data. The caller can choose the I2C address to allow two BMx280 devices to be used on the same I2C bus.

An interface has been added to return the measurement values in an integer fixed float format. The float format interface is still there.

All the values are read in one I2C transaction to ensure they are a consistent set.

Renamed bmp280_calib_t to bmp280_t, and removed read_register8.
2016-08-17 23:33:04 +10:00

113 lines
3.2 KiB
C

#include <stdio.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "i2c/i2c.h"
#include "bmp280/bmp280.h"
// In forced mode user initiate measurement each time.
// In normal mode measurement is done continuously with specified standby time.
// #define MODE_FORCED
const uint8_t scl_pin = 0;
const uint8_t sda_pin = 2;
#ifdef MODE_FORCED
static void bmp280_task_forced(void *pvParameters)
{
bmp280_params_t params;
float pressure, temperature, humidity;
bmp280_init_default_params(&params);
params.mode = BMP280_MODE_FORCED;
bmp280_t bmp280_dev;
bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0;
while (1) {
while (!bmp280_init(&bmp280_dev, &params)) {
printf("BMP280 initialization failed\n");
vTaskDelay(1000 / portTICK_RATE_MS);
}
bool bme280p = bmp280_dev.id == BME280_CHIP_ID;
printf("BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
while(1) {
vTaskDelay(1000 / portTICK_RATE_MS);
if (!bmp280_force_measurement(&bmp280_dev)) {
printf("Failed initiating measurement\n");
break;
}
// wait for measurement to complete
while (bmp280_is_measuring(&bmp280_dev)) {};
if (!bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity)) {
printf("Temperature/pressure reading failed\n");
break;
}
printf("Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
if (bme280p)
printf(", Humidity: %.2f\n", humidity);
else
printf("\n");
}
}
}
#else
static void bmp280_task_normal(void *pvParameters)
{
bmp280_params_t params;
float pressure, temperature, humidity;
bmp280_init_default_params(&params);
bmp280_t bmp280_dev;
bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0;
while (1) {
while (!bmp280_init(&bmp280_dev, &params)) {
printf("BMP280 initialization failed\n");
vTaskDelay(1000 / portTICK_RATE_MS);
}
bool bme280p = bmp280_dev.id == BME280_CHIP_ID;
printf("BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
while(1) {
vTaskDelay(1000 / portTICK_RATE_MS);
if (!bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity)) {
printf("Temperature/pressure reading failed\n");
break;
}
printf("Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
if (bme280p)
printf(", Humidity: %.2f\n", humidity);
else
printf("\n");
}
}
}
#endif
void user_init(void)
{
uart_set_baud(0, 115200);
// Just some information
printf("\n");
printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV);
i2c_init(scl_pin, sda_pin);
#ifdef MODE_FORCED
xTaskCreate(bmp280_task_forced, (signed char *)"bmp280_task", 256, NULL, 2, NULL);
#else
xTaskCreate(bmp280_task_normal, (signed char *)"bmp280_task", 256, NULL, 2, NULL);
#endif
}