BME680 bugfixes

This commit is contained in:
Klaus K Wilting 2018-12-31 15:46:52 +01:00
parent 7040408513
commit 998e122f3a
5 changed files with 29 additions and 24 deletions

View File

@ -3,7 +3,6 @@
#include "globals.h" #include "globals.h"
#include <Wire.h> #include <Wire.h>
#include "irqhandler.h"
#include "../lib/Bosch-BSEC/src/bsec.h" #include "../lib/Bosch-BSEC/src/bsec.h"
extern bmeStatus_t extern bmeStatus_t

View File

@ -9,6 +9,7 @@
#include <array> #include <array>
#include <algorithm> #include <algorithm>
#include "Mallocator.h" #include "Mallocator.h"
#include "inc/bsec_datatypes.h"
// sniffing types // sniffing types
#define MAC_SNIFF_WIFI 0 #define MAC_SNIFF_WIFI 0
@ -54,7 +55,7 @@ typedef struct {
uint8_t runmode; // 0=normal, 1=update uint8_t runmode; // 0=normal, 1=update
uint8_t payloadmask; // bitswitches for payload data uint8_t payloadmask; // bitswitches for payload data
char version[10]; // Firmware version char version[10]; // Firmware version
char bsecstate[BSEC_MAX_STATE_BLOB_SIZE+1]; // init BSEC state for BME680 sensor char bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 2]; // init BSEC state for BME680 sensor
} configData_t; } configData_t;
// Struct holding payload for data send queue // Struct holding payload for data send queue
@ -98,6 +99,7 @@ extern std::array<uint64_t, 0xff> beacons;
extern TaskHandle_t irqHandlerTask, wifiSwitchTask; extern TaskHandle_t irqHandlerTask, wifiSwitchTask;
// application includes
#include "led.h" #include "led.h"
#include "payload.h" #include "payload.h"
#include "blescan.h" #include "blescan.h"
@ -106,10 +108,6 @@ extern TaskHandle_t irqHandlerTask, wifiSwitchTask;
#include "gpsread.h" #include "gpsread.h"
#endif #endif
#ifdef HAS_BME
#include "bme680mems.h"
#endif
#ifdef HAS_LORA #ifdef HAS_LORA
#include "lorawan.h" #include "lorawan.h"
#endif #endif
@ -134,4 +132,8 @@ extern TaskHandle_t irqHandlerTask, wifiSwitchTask;
#include "sensor.h" #include "sensor.h"
#endif #endif
#ifdef HAS_BME
#include "bme680mems.h"
#endif
#endif #endif

View File

@ -23,12 +23,14 @@ bsec_virtual_sensor_t sensorList[10] = {
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
}; };
uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE] = {0}; uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE] = {0};
uint16_t stateUpdateCounter = 0; uint16_t stateUpdateCounter = 0;
// initialize BME680 sensor // initialize BME680 sensor
int bme_init(void) { int bme_init(void) {
// return = 0 -> error / return = 1 -> success
// block i2c bus access // block i2c bus access
if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) ==
pdTRUE) { pdTRUE) {
@ -46,7 +48,7 @@ int bme_init(void) {
ESP_LOGI(TAG, "BME680 sensor found and initialized"); ESP_LOGI(TAG, "BME680 sensor found and initialized");
else { else {
ESP_LOGE(TAG, "BME680 sensor not found"); ESP_LOGE(TAG, "BME680 sensor not found");
return 1; goto error;
} }
loadState(); loadState();
@ -58,16 +60,21 @@ int bme_init(void) {
ESP_LOGI(TAG, "BSEC subscription succesful"); ESP_LOGI(TAG, "BSEC subscription succesful");
else { else {
ESP_LOGE(TAG, "BSEC subscription error"); ESP_LOGE(TAG, "BSEC subscription error");
return 1; goto error;
} }
xSemaphoreGive(I2Caccess); // release i2c bus access
} else { } else {
ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error"); ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error");
return 1; goto error;
} }
xSemaphoreGive(I2Caccess); // release i2c bus access
return 1;
error:
xSemaphoreGive(I2Caccess); // release i2c bus access
return 0;
} // bme_init() } // bme_init()
// Helper function definitions // Helper function definitions
@ -128,8 +135,8 @@ void loadState(void) {
if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1] == BSEC_MAX_STATE_BLOB_SIZE) { if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1] == BSEC_MAX_STATE_BLOB_SIZE) {
// Existing state in NVS stored // Existing state in NVS stored
ESP_LOGI(TAG, "restoring BSEC state from NVRAM"); ESP_LOGI(TAG, "restoring BSEC state from NVRAM");
memcpy(bsecState, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE); memcpy(bsecstate_buffer, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE);
iaqSensor.setState(bsecState); iaqSensor.setState(bsecstate_buffer);
checkIaqSensorStatus(); checkIaqSensorStatus();
} else // no state stored } else // no state stored
ESP_LOGI(TAG, ESP_LOGI(TAG,
@ -154,9 +161,9 @@ void updateState(void) {
} }
if (update) { if (update) {
memcpy(bsecState, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE); memcpy(bsecstate_buffer, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE);
cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1] = BSEC_MAX_STATE_BLOB_SIZE; cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1] = BSEC_MAX_STATE_BLOB_SIZE;
iaqSensor.getState(bsecState); iaqSensor.getState(bsecstate_buffer);
checkIaqSensorStatus(); checkIaqSensorStatus();
ESP_LOGI(TAG, "saving BSEC state to NVRAM"); ESP_LOGI(TAG, "saving BSEC state to NVRAM");
saveConfig(); saveConfig();

View File

@ -80,11 +80,11 @@ void saveConfig() {
int16_t flash16 = 0; int16_t flash16 = 0;
size_t required_size; size_t required_size;
char storedversion[10]; char storedversion[10];
char bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]; char bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE + 1];
if (nvs_get_str(my_handle, "bsecstate", bsecstate, &required_size) != if (nvs_get_str(my_handle, "bsecstate", bsecstate_buffer, &required_size) !=
ESP_OK || ESP_OK ||
strcmp(bsecstate, cfg.bsecstate) != 0) strcmp(bsecstate_buffer, cfg.bsecstate) != 0)
nvs_set_str(my_handle, "bsecstate", cfg.bsecstate); nvs_set_str(my_handle, "bsecstate", cfg.bsecstate);
if (nvs_get_str(my_handle, "version", storedversion, &required_size) != if (nvs_get_str(my_handle, "version", storedversion, &required_size) !=
@ -212,12 +212,10 @@ void loadConfig() {
// populate pre set defaults with current values from NVRAM // populate pre set defaults with current values from NVRAM
char bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1];
if (nvs_get_str(my_handle, "bsecstate", NULL, &required_size) == ESP_OK) { if (nvs_get_str(my_handle, "bsecstate", NULL, &required_size) == ESP_OK) {
nvs_get_str(my_handle, "bsecstate", cfg.bsecstate, &required_size); nvs_get_str(my_handle, "bsecstate", cfg.bsecstate, &required_size);
ESP_LOGI(TAG, "bsecstate = %d", ESP_LOGI(TAG, "bsecstate = %d",
cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]); (int)(cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]));
} }
if (nvs_get_i8(my_handle, "lorasf", &flash8) == ESP_OK) { if (nvs_get_i8(my_handle, "lorasf", &flash8) == ESP_OK) {

View File

@ -72,7 +72,6 @@
// Settings for BME680 environmental sensor (if present) // Settings for BME680 environmental sensor (if present)
#define BME_TEMP_OFFSET 5.0f // Offset sensor on chip temp <-> ambient temp [default = 5°C] #define BME_TEMP_OFFSET 5.0f // Offset sensor on chip temp <-> ambient temp [default = 5°C]
#define BSEC_MAX_STATE_BLOB_SIZE 134 // size of Bosch BME680 state data blob
#define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // update every 360 minutes = 4 times a day #define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // update every 360 minutes = 4 times a day
// OTA settings // OTA settings