BME280 integration (PR #309)

This commit is contained in:
Verkehrsrot 2019-03-13 21:15:28 +01:00
parent 8402dc0948
commit bfa28d4139
13 changed files with 91 additions and 135 deletions

View File

@ -49,7 +49,7 @@ Depending on board hardware following features are supported:
- Silicon unique ID - Silicon unique ID
- Battery voltage monitoring - Battery voltage monitoring
- GPS (Generic serial NMEA, or Quectel L76 I2C) - GPS (Generic serial NMEA, or Quectel L76 I2C)
- Environmental sensor (Bosch BME680 I2C) - Environmental sensor (Bosch BME280/BME680 I2C)
- Real Time Clock (Maxim DS3231 I2C) - Real Time Clock (Maxim DS3231 I2C)
- IF482 (serial) and DCF77 (gpio) time telegram generator - IF482 (serial) and DCF77 (gpio) time telegram generator

View File

@ -1,17 +0,0 @@
#ifndef _BME280_H
#define _BME280_H
#include "globals.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "../lib/Bosch-BSEC/src/bsec.h"
extern bmeStatus_t
bme_status; // Make struct for storing gps data globally available
extern TaskHandle_t Bme280Task;
int bme280_init();
void bme280_loop(void *pvParameters);
#endif

View File

@ -1,9 +1,15 @@
#ifndef _BME680MEMS_H #ifndef _BMESENSOR_H
#define _BME680MEMS_H #define _BMESENSOR_H
#include "globals.h" #include "globals.h"
#include <Wire.h> #include <Wire.h>
#ifdef HAS_BME680
#include "../lib/Bosch-BSEC/src/bsec.h" #include "../lib/Bosch-BSEC/src/bsec.h"
#elif defined HAS_BME280
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#endif
extern bmeStatus_t extern bmeStatus_t
bme_status; // Make struct for storing gps data globally available bme_status; // Make struct for storing gps data globally available

View File

@ -7,12 +7,8 @@
#include "spislave.h" #include "spislave.h"
#include <lmic.h> #include <lmic.h>
#ifdef HAS_BME680 #if (HAS_BME)
#include "bme680mems.h" #include "bmesensor.h"
#endif
#ifdef HAS_BME280
#include "bme280.h"
#endif #endif
extern Ticker housekeeper; extern Ticker housekeeper;

View File

@ -151,12 +151,8 @@ extern time_t userUTCTime;
#include "sensor.h" #include "sensor.h"
#endif #endif
#ifdef HAS_BME680 #if (HAS_BME)
#include "bme680mems.h" #include "bmesensor.h"
#endif
#ifdef HAS_BME280
#include "bme280.h"
#endif #endif
#endif #endif

View File

@ -6,7 +6,7 @@
; ---> SELECT TARGET PLATFORM HERE! <--- ; ---> SELECT TARGET PLATFORM HERE! <---
[platformio] [platformio]
;env_default = generic env_default = generic
;env_default = ebox ;env_default = ebox
;env_default = eboxtube ;env_default = eboxtube
;env_default = heltec ;env_default = heltec
@ -14,7 +14,7 @@
;env_default = ttgov1 ;env_default = ttgov1
;env_default = ttgov2 ;env_default = ttgov2
;env_default = ttgov21old ;env_default = ttgov21old
env_default = ttgov21new ;env_default = ttgov21new
;env_default = ttgobeam ;env_default = ttgobeam
;env_default = ttgofox ;env_default = ttgofox
;env_default = lopy ;env_default = lopy
@ -53,6 +53,9 @@ lib_deps_gps =
TinyGPSPlus@>=1.0.2 TinyGPSPlus@>=1.0.2
lib_deps_rtc = lib_deps_rtc =
RTC@^2.3.0 RTC@^2.3.0
lib_deps_sensors =
Adafruit Unified Sensor@^1.0.3
Adafruit BME280 Library@1.0.8
lib_deps_basic = lib_deps_basic =
ArduinoJson@^5.13.1 ArduinoJson@^5.13.1
Timezone@^1.2.2 Timezone@^1.2.2
@ -201,7 +204,7 @@ lib_deps =
${common.lib_deps_lora} ${common.lib_deps_lora}
${common.lib_deps_display} ${common.lib_deps_display}
build_flags = build_flags =
${common.build_flags_all} ${common.build_flags_basic}
upload_protocol = ${common.upload_protocol} upload_protocol = ${common.upload_protocol}
extra_scripts = ${common.extra_scripts} extra_scripts = ${common.extra_scripts}
monitor_speed = ${common.monitor_speed} monitor_speed = ${common.monitor_speed}

View File

@ -1,63 +0,0 @@
#ifdef HAS_BME280
#include "bme280.h"
// Local logging tag
static const char TAG[] = __FILE__;
bmeStatus_t bme_status;
TaskHandle_t Bme280Task;
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
// initialize BME280 sensor
int bme280_init(void) {
bool status;
// return = 0 -> error / return = 1 -> success
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
status = bme.begin(BME280_ADDR);
if (!status) {
ESP_LOGE(TAG, "BME280 sensor not found");
goto error;
}
ESP_LOGI(TAG, "BME280 sensor found and initialized");
} else {
ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error");
goto error;
}
I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1;
error:
I2C_MUTEX_UNLOCK(); // release i2c bus access
return 0;
} // bme_init()
// loop function which reads and processes data based on sensor settings
void bme280_loop(void *pvParameters) {
#ifdef HAS_BME280
while (1) {
if (I2C_MUTEX_LOCK()) {
bme_status.temperature = bme.readTemperature();
bme_status.pressure = (bme.readPressure() / 100.0); // conversion Pa -> hPa
// bme.readAltitude(SEALEVELPRESSURE_HPA);
bme_status.humidity = bme.readHumidity();
I2C_MUTEX_UNLOCK();
}
}
#endif
ESP_LOGE(TAG, "BME task ended");
vTaskDelete(Bme280Task); // should never be reached
} // bme_loop()
#endif // HAS_BME280

View File

@ -1,6 +1,6 @@
#ifdef HAS_BME680 #if (HAS_BME)
#include "bme680mems.h" #include "bmesensor.h"
// Local logging tag // Local logging tag
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
@ -8,6 +8,9 @@ static const char TAG[] = __FILE__;
bmeStatus_t bme_status; bmeStatus_t bme_status;
TaskHandle_t BmeTask; TaskHandle_t BmeTask;
#define SEALEVELPRESSURE_HPA (1013.25)
#ifdef HAS_BME680
bsec_virtual_sensor_t sensorList[10] = { bsec_virtual_sensor_t sensorList[10] = {
BSEC_OUTPUT_RAW_TEMPERATURE, BSEC_OUTPUT_RAW_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE, BSEC_OUTPUT_RAW_PRESSURE,
@ -26,11 +29,20 @@ uint16_t stateUpdateCounter = 0;
Bsec iaqSensor; Bsec iaqSensor;
#elif defined HAS_BME280
Adafruit_BME280 bme; // I2C
// Adafruit_BME280 bme(BME_CS); // hardware SPI
// Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
#endif
// initialize BME680 sensor // initialize BME680 sensor
int bme_init(void) { int bme_init(void) {
// return = 0 -> error / return = 1 -> success // return = 0 -> error / return = 1 -> success
#ifdef HAS_BME680
// block i2c bus access // block i2c bus access
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
@ -66,6 +78,26 @@ int bme_init(void) {
goto error; goto error;
} }
#elif defined HAS_BME280
bool status;
// return = 0 -> error / return = 1 -> success
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
status = bme.begin(BME280_ADDR);
if (!status) {
ESP_LOGE(TAG, "BME280 sensor not found");
goto error;
}
ESP_LOGI(TAG, "BME280 sensor found and initialized");
} else {
ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error");
goto error;
}
#endif
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1; return 1;
@ -75,6 +107,8 @@ error:
} // bme_init() } // bme_init()
#ifdef HAS_BME680
// Helper function definitions // Helper function definitions
int checkIaqSensorStatus(void) { int checkIaqSensorStatus(void) {
int rslt = 1; // true = 1 = no error, false = 0 = error int rslt = 1; // true = 1 = no error, false = 0 = error
@ -97,6 +131,7 @@ int checkIaqSensorStatus(void) {
return rslt; return rslt;
} // checkIaqSensorStatus() } // checkIaqSensorStatus()
#endif
// loop function which reads and processes data based on sensor settings // loop function which reads and processes data based on sensor settings
void bme_loop(void *pvParameters) { void bme_loop(void *pvParameters) {
@ -122,12 +157,24 @@ void bme_loop(void *pvParameters) {
I2C_MUTEX_UNLOCK(); I2C_MUTEX_UNLOCK();
} }
} }
#elif defined HAS_BME280
while (1) {
if (I2C_MUTEX_LOCK()) {
bme_status.temperature = bme.readTemperature();
bme_status.pressure =
(bme.readPressure() / 100.0); // conversion Pa -> hPa
// bme.readAltitude(SEALEVELPRESSURE_HPA);
bme_status.humidity = bme.readHumidity();
I2C_MUTEX_UNLOCK();
}
}
#endif #endif
ESP_LOGE(TAG, "BME task ended"); ESP_LOGE(TAG, "BME task ended");
vTaskDelete(BmeTask); // should never be reached vTaskDelete(BmeTask); // should never be reached
} // bme_loop() } // bme_loop()
#ifdef HAS_BME680
void loadState(void) { void loadState(void) {
if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] == BSEC_MAX_STATE_BLOB_SIZE) { if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] == BSEC_MAX_STATE_BLOB_SIZE) {
// Existing state in NVS stored // Existing state in NVS stored
@ -167,5 +214,6 @@ void updateState(void) {
saveConfig(); saveConfig();
} }
} }
#endif
#endif // HAS_BME680 #endif // HAS_BME

View File

@ -36,14 +36,10 @@ void doHousekeeping() {
ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d", ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask)); uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask));
#endif #endif
#ifdef HAS_BME680 #if (HAS_BME)
ESP_LOGD(TAG, "Bmeloop %d bytes left | Taskstate = %d", ESP_LOGD(TAG, "Bmeloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(BmeTask), eTaskGetState(BmeTask)); uxTaskGetStackHighWaterMark(BmeTask), eTaskGetState(BmeTask));
#endif #endif
#ifdef HAS_BME280
ESP_LOGD(TAG, "Bme280loop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(Bme280Task), eTaskGetState(Bme280Task));
#endif
#ifdef HAS_DCF77 #ifdef HAS_DCF77
ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d", ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask)); uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask));

View File

@ -21,6 +21,9 @@
#define HAS_BME 1 // Enable BME sensors in general #define HAS_BME 1 // Enable BME sensors in general
#define HAS_BME680 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL #define HAS_BME680 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
#define BME680_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND #define BME680_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
// BME280 sensor on I2C bus
//#define HAS_BME 1 // Enable BME sensors in general
//#define HAS_BME280 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
// user defined sensors // user defined sensors
//#define HAS_SENSORS 1 // comment out if device has user defined sensors //#define HAS_SENSORS 1 // comment out if device has user defined sensors

View File

@ -14,10 +14,9 @@
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC #define CFG_sx1276_radio 1 // HPD13A LoRa SoC
// enable only if device has these sensors, otherwise comment these lines // enable only if device has these sensors, otherwise comment these lines
// BME680 sensor on I2C bus // BME280 sensor on I2C bus
#define HAS_BME 1 // Enable BME sensors in general //#define HAS_BME 1 // Enable BME sensors in general
#define HAS_BME280 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL //#define HAS_BME280 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
#define BME280_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C #define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C
#define HAS_LED (25) // green on board LED #define HAS_LED (25) // green on board LED

View File

@ -358,11 +358,15 @@ void setup() {
&irqHandlerTask, // task handle &irqHandlerTask, // task handle
1); // CPU core 1); // CPU core
// initialize bme680 // initialize BME sensor (BME280/BME680)
#if (HAS_BME)
#ifdef HAS_BME680 #ifdef HAS_BME680
strcat_P(features, " BME680");
#elif defined HAS_BME280
strcat_P(features, " BME280"); strcat_P(features, " BME280");
#endif
if (bme_init()) { if (bme_init()) {
ESP_LOGI(TAG, "Starting BME280 sensor..."); ESP_LOGI(TAG, "Starting BME sensor...");
xTaskCreatePinnedToCore(bme_loop, // task function xTaskCreatePinnedToCore(bme_loop, // task function
"bmeloop", // name of task "bmeloop", // name of task
2048, // stack size of task 2048, // stack size of task
@ -373,21 +377,6 @@ void setup() {
} }
#endif #endif
// initialize BME280
#ifdef HAS_BME280
strcat_P(features, " BME280");
if (bme280_init()) {
ESP_LOGI(TAG, "Starting BME280 sensor...");
xTaskCreatePinnedToCore(bme280_loop, // task function
"bme280loop", // name of task
2048, // stack size of task
(void *)1, // parameter of the task
1, // priority of the task
&Bme280Task, // task handle
1); // CPU core
}
#endif
// starting timers and interrupts // starting timers and interrupts
assert(irqHandlerTask != NULL); // has interrupt handler task started? assert(irqHandlerTask != NULL); // has interrupt handler task started?
ESP_LOGI(TAG, "Starting Timers..."); ESP_LOGI(TAG, "Starting Timers...");

View File

@ -262,12 +262,12 @@ void get_gps(uint8_t val[]) {
void get_bme(uint8_t val[]) { void get_bme(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get bme680 sensor data"); ESP_LOGI(TAG, "Remote command: get bme680 sensor data");
#ifdef HAS_BME680 #if (HAS_BME)
payload.reset(); payload.reset();
payload.addBME(bme_status); payload.addBME(bme_status);
SendPayload(BMEPORT, prio_high); SendPayload(BMEPORT, prio_high);
#else #else
ESP_LOGW(TAG, "BME680 sensor not supported"); ESP_LOGW(TAG, "BME sensor not supported");
#endif #endif
}; };