BME280 integration (PR #309)

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

No files matched your search

-63
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
+51 -3
View File
@@ -1,6 +1,6 @@
#ifdef HAS_BME680
#if (HAS_BME)
#include "bme680mems.h"
#include "bmesensor.h"
// Local logging tag
static const char TAG[] = __FILE__;
@@ -8,6 +8,9 @@ static const char TAG[] = __FILE__;
bmeStatus_t bme_status;
TaskHandle_t BmeTask;
#define SEALEVELPRESSURE_HPA (1013.25)
#ifdef HAS_BME680
bsec_virtual_sensor_t sensorList[10] = {
BSEC_OUTPUT_RAW_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE,
@@ -26,11 +29,20 @@ uint16_t stateUpdateCounter = 0;
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
int bme_init(void) {
// return = 0 -> error / return = 1 -> success
#ifdef HAS_BME680
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
@@ -66,6 +78,26 @@ int bme_init(void) {
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
return 1;
@@ -75,6 +107,8 @@ error:
} // bme_init()
#ifdef HAS_BME680
// Helper function definitions
int checkIaqSensorStatus(void) {
int rslt = 1; // true = 1 = no error, false = 0 = error
@@ -97,6 +131,7 @@ int checkIaqSensorStatus(void) {
return rslt;
} // checkIaqSensorStatus()
#endif
// loop function which reads and processes data based on sensor settings
void bme_loop(void *pvParameters) {
@@ -122,12 +157,24 @@ void bme_loop(void *pvParameters) {
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
ESP_LOGE(TAG, "BME task ended");
vTaskDelete(BmeTask); // should never be reached
} // bme_loop()
#ifdef HAS_BME680
void loadState(void) {
if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] == BSEC_MAX_STATE_BLOB_SIZE) {
// Existing state in NVS stored
@@ -167,5 +214,6 @@ void updateState(void) {
saveConfig();
}
}
#endif
#endif // HAS_BME680
#endif // HAS_BME
+1 -5
View File
@@ -36,14 +36,10 @@ void doHousekeeping() {
ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask));
#endif
#ifdef HAS_BME680
#if (HAS_BME)
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));
+3
View File
@@ -21,6 +21,9 @@
#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
// 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
//#define HAS_SENSORS 1 // comment out if device has user defined sensors
+3 -4
View File
@@ -14,10 +14,9 @@
#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
// 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
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C
#define HAS_LED (25) // green on board LED
+12 -23
View File
@@ -121,7 +121,7 @@ void setup() {
#endif
// setup debug output or silence device
#if(VERBOSE)
#if (VERBOSE)
Serial.begin(115200);
esp_log_level_set("*", ESP_LOG_VERBOSE);
#else
@@ -132,7 +132,7 @@ void setup() {
ESP_LOGI(TAG, "Starting %s v%s", PRODUCTNAME, PROGVERSION);
// print chip information on startup if in verbose mode
#if(VERBOSE)
#if (VERBOSE)
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
ESP_LOGI(TAG,
@@ -227,7 +227,7 @@ void setup() {
batt_voltage = read_voltage();
#endif
#if(USE_OTA)
#if (USE_OTA)
strcat_P(features, " OTA");
// reboot to firmware update mode if ota trigger switch is set
if (cfg.runmode == 1) {
@@ -239,7 +239,7 @@ void setup() {
// start BLE scan callback if BLE function is enabled in NVRAM configuration
// or switch off bluetooth, if not compiled
#if(BLECOUNTER)
#if (BLECOUNTER)
strcat_P(features, " BLE");
if (cfg.blescan) {
ESP_LOGI(TAG, "Starting Bluetooth...");
@@ -301,7 +301,7 @@ void setup() {
assert(spi_init() == ESP_OK);
#endif
#if(VENDORFILTER)
#if (VENDORFILTER)
strcat_P(features, " OUIFLT");
#endif
@@ -358,11 +358,15 @@ void setup() {
&irqHandlerTask, // task handle
1); // CPU core
// initialize bme680
// initialize BME sensor (BME280/BME680)
#if (HAS_BME)
#ifdef HAS_BME680
strcat_P(features, " BME680");
#elif defined HAS_BME280
strcat_P(features, " BME280");
#endif
if (bme_init()) {
ESP_LOGI(TAG, "Starting BME280 sensor...");
ESP_LOGI(TAG, "Starting BME sensor...");
xTaskCreatePinnedToCore(bme_loop, // task function
"bmeloop", // name of task
2048, // stack size of task
@@ -373,21 +377,6 @@ 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...");
@@ -415,7 +404,7 @@ void setup() {
#endif
#endif // HAS_BUTTON
#if(TIME_SYNC_INTERVAL)
#if (TIME_SYNC_INTERVAL)
// start pps timepulse
ESP_LOGI(TAG, "Starting Timekeeper...");
assert(timepulse_init()); // setup timepulse
+2 -2
View File
@@ -262,12 +262,12 @@ void get_gps(uint8_t val[]) {
void get_bme(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get bme680 sensor data");
#ifdef HAS_BME680
#if (HAS_BME)
payload.reset();
payload.addBME(bme_status);
SendPayload(BMEPORT, prio_high);
#else
ESP_LOGW(TAG, "BME680 sensor not supported");
ESP_LOGW(TAG, "BME sensor not supported");
#endif
};