Support for BME280

This commit is contained in:
Gregor Wolf 2019-03-08 18:13:51 +01:00
parent a6663d44b5
commit 91cc9dba0c
15 changed files with 151 additions and 27 deletions

17
include/bme280.h Normal file
View File

@ -0,0 +1,17 @@
#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

@ -7,10 +7,14 @@
#include "spislave.h"
#include <lmic.h>
#ifdef HAS_BME
#ifdef HAS_BME680
#include "bme680mems.h"
#endif
#ifdef HAS_BME280
#include "bme280.h"
#endif
extern Ticker housekeeper;
void housekeeping(void);

View File

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

View File

@ -6,7 +6,7 @@
; ---> SELECT TARGET PLATFORM HERE! <---
[platformio]
env_default = generic
;env_default = generic
;env_default = ebox
;env_default = eboxtube
;env_default = heltec
@ -14,7 +14,7 @@ env_default = generic
;env_default = ttgov1
;env_default = ttgov2
;env_default = ttgov21old
;env_default = ttgov21new
env_default = ttgov21new
;env_default = ttgobeam
;env_default = ttgofox
;env_default = lopy
@ -202,7 +202,7 @@ lib_deps =
${common.lib_deps_lora}
${common.lib_deps_display}
build_flags =
${common.build_flags_basic}
${common.build_flags_all}
upload_protocol = ${common.upload_protocol}
extra_scripts = ${common.extra_scripts}
monitor_speed = ${common.monitor_speed}

63
src/bme280.cpp Normal file
View File

@ -0,0 +1,63 @@
#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,4 +1,4 @@
#ifdef HAS_BME
#ifdef HAS_BME680
#include "bme680mems.h"
@ -34,8 +34,8 @@ int bme_init(void) {
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
Wire.begin(HAS_BME);
iaqSensor.begin(BME_ADDR, Wire);
Wire.begin(HAS_BME680);
iaqSensor.begin(BME680_ADDR, Wire);
ESP_LOGI(TAG, "BSEC v%d.%d.%d.%d", iaqSensor.version.major,
iaqSensor.version.minor, iaqSensor.version.major_bugfix,
@ -103,7 +103,7 @@ void bme_loop(void *pvParameters) {
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
#ifdef HAS_BME
#ifdef HAS_BME680
while (1) {
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
@ -168,4 +168,4 @@ void updateState(void) {
}
}
#endif // HAS_BME
#endif // HAS_BME680

View File

@ -36,10 +36,14 @@ void doHousekeeping() {
ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask));
#endif
#ifdef HAS_BME
#ifdef HAS_BME680
ESP_LOGD(TAG, "Bmeloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(BmeTask), eTaskGetState(BmeTask));
#endif
#ifdef HAS_BME280
ESP_LOGD(TAG, "Bme280loop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(Bme280Task), eTaskGetState(Bme280Task));
#endif
#ifdef HAS_DCF77
ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask));
@ -58,11 +62,17 @@ void doHousekeeping() {
#endif
// display BME sensor data
#ifdef HAS_BME
#ifdef HAS_BME680
ESP_LOGI(TAG, "BME680 Temp: %.2f°C | IAQ: %.2f | IAQacc: %d",
bme_status.temperature, bme_status.iaq, bme_status.iaq_accuracy);
#endif
// display BME280 sensor data
#ifdef HAS_BME280
ESP_LOGI(TAG, "BME680 Temp: %.2f°C | Humidity: %.2f | Pressure: %.0f",
bme_status.temperature, bme_status.humidity, bme_status.pressure);
#endif
// check free heap memory
if (ESP.getMinFreeHeap() <= MEM_LOW) {
ESP_LOGI(TAG,

View File

@ -18,8 +18,9 @@
// enable only if device has these sensors, otherwise comment these lines
// BME680 sensor on I2C bus
#define HAS_BME GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
#define BME_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
#define HAS_BME 1 // Enable BME sensors in general
#define HAS_BME680 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
#define BME680_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
// user defined sensors
//#define HAS_SENSORS 1 // comment out if device has user defined sensors

View File

@ -7,8 +7,9 @@
// Hardware related definitions for Heltec V2 LoRa-32 Board
//#define HAS_BME GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
//#define BME_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
//#define HAS_BME 1 // Enable BME sensors in general
//#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 HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1

View File

@ -7,8 +7,9 @@
// Hardware related definitions for Heltec V2 LoRa-32 Board
//#define HAS_BME GPIO_NUM_4, GPIO_NUM_15 // SDA, SCL
//#define BME_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
//#define HAS_BME 1 // Enable BME sensors in general
//#define HAS_BME680 GPIO_NUM_4, GPIO_NUM_15 // SDA, SCL
//#define BME680_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1

View File

@ -12,8 +12,9 @@
#define DISABLE_BROWNOUT 1 // comment out if you want to keep brownout feature
// Octopus32 has a pre-populated BME680 on i2c addr 0x76
#define HAS_BME GPIO_NUM_23, GPIO_NUM_22 // SDA, SCL
#define BME_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
#define HAS_BME 1 // Enable BME sensors in general
#define HAS_BME680 GPIO_NUM_23, GPIO_NUM_22 // SDA, SCL
#define BME680_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
// user defined sensors
//#define HAS_SENSORS 1 // comment out if device has user defined sensors

View File

@ -31,8 +31,9 @@
// enable only if device has these sensors, otherwise comment these lines
// BME680 sensor on I2C bus
#define HAS_BME SDA, SCL
#define BME_ADDR BME680_I2C_ADDR_PRIMARY // !! connect SDIO of BME680 to GND !!
#define HAS_BME 1 // Enable BME sensors in general
#define HAS_BME680 SDA, SCL
#define BME680_ADDR BME680_I2C_ADDR_PRIMARY // !! connect SDIO of BME680 to GND !!
// display (if connected)
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C

View File

@ -13,6 +13,12 @@
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC
// enable only if device has these sensors, otherwise comment these lines
// BME680 sensor on I2C bus
#define HAS_BME 1 // Enable BME sensors in general
#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_LED (25) // green on board LED
#define HAS_BATTERY_PROBE ADC1_GPIO35_CHANNEL // battery probe GPIO pin -> ADC1_CHANNEL_7

View File

@ -358,11 +358,11 @@ void setup() {
&irqHandlerTask, // task handle
1); // CPU core
// initialize bme
#ifdef HAS_BME
strcat_P(features, " BME");
// initialize bme680
#ifdef HAS_BME680
strcat_P(features, " BME280");
if (bme_init()) {
ESP_LOGI(TAG, "Starting BME sensor...");
ESP_LOGI(TAG, "Starting BME280 sensor...");
xTaskCreatePinnedToCore(bme_loop, // task function
"bmeloop", // name of task
2048, // stack size of task
@ -373,6 +373,21 @@ void setup() {
}
#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
assert(irqHandlerTask != NULL); // has interrupt handler task started?
ESP_LOGI(TAG, "Starting Timers...");

View File

@ -262,7 +262,7 @@ void get_gps(uint8_t val[]) {
void get_bme(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get bme680 sensor data");
#ifdef HAS_BME
#ifdef HAS_BME680
payload.reset();
payload.addBME(bme_status);
SendPayload(BMEPORT, prio_high);