From 0fd36b0051547170fc53d029dd0d57265758e9bd Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 1 Jan 2019 21:23:45 +0100 Subject: [PATCH] BME680 state load/save bugfixes --- include/globals.h | 2 +- src/bme680mems.cpp | 10 ++++++---- src/configmanager.cpp | 25 +++++++++++++------------ src/cyclic.cpp | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/globals.h b/include/globals.h index d279588a..bff3fc78 100644 --- a/include/globals.h +++ b/include/globals.h @@ -56,7 +56,7 @@ typedef struct { uint8_t runmode; // 0=normal, 1=update uint8_t payloadmask; // bitswitches for payload data char version[10]; // Firmware version - char bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 2]; // BSEC state for BME680 sensor + uint8_t bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]; // BSEC state for BME680 sensor } configData_t; // Struct holding payload for data send queue diff --git a/src/bme680mems.cpp b/src/bme680mems.cpp index b11c02e4..f888eb4e 100644 --- a/src/bme680mems.cpp +++ b/src/bme680mems.cpp @@ -23,7 +23,6 @@ bsec_virtual_sensor_t sensorList[10] = { BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, }; -uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE] = {0}; uint16_t stateUpdateCounter = 0; // initialize BME680 sensor @@ -132,7 +131,8 @@ void bme_loop(void *pvParameters) { } // bme_loop() void loadState(void) { - if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1] == BSEC_MAX_STATE_BLOB_SIZE) { + uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE] = {0}; + if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] == BSEC_MAX_STATE_BLOB_SIZE) { // Existing state in NVS stored ESP_LOGI(TAG, "restoring BSEC state from NVRAM"); memcpy(bsecstate_buffer, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE); @@ -145,14 +145,16 @@ void loadState(void) { void updateState(void) { bool update = false; + uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE] = {0}; if (stateUpdateCounter == 0) { - /* First state update when IAQ accuracy is >= 3 */ + // first state update when IAQ accuracy is >= 3 if (iaqSensor.iaqAccuracy >= 3) { update = true; stateUpdateCounter++; } } else { + /* Update every STATE_SAVE_PERIOD minutes */ if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) { update = true; @@ -164,7 +166,7 @@ void updateState(void) { iaqSensor.getState(bsecstate_buffer); checkIaqSensorStatus(); memcpy(cfg.bsecstate, bsecstate_buffer, 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] = BSEC_MAX_STATE_BLOB_SIZE; ESP_LOGI(TAG, "saving BSEC state to NVRAM"); saveConfig(); } diff --git a/src/configmanager.cpp b/src/configmanager.cpp index b59349ad..dd4fdcbd 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -32,7 +32,7 @@ void defaultConfig() { cfg.monitormode = 0; // 0=disabled, 1=enabled cfg.runmode = 0; // 0=normal, 1=update cfg.payloadmask = 0xFF; // all payload switched on - cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1] = { + cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] = { 0}; // init BSEC state for BME680 sensor strncpy(cfg.version, PROGVERSION, sizeof(cfg.version) - 1); @@ -79,13 +79,14 @@ void saveConfig() { int8_t flash8 = 0; int16_t flash16 = 0; size_t required_size; + uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE + 1]; char storedversion[10]; - char bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE + 1]; - if (nvs_get_str(my_handle, "bsecstate", bsecstate_buffer, &required_size) != - ESP_OK || - strcmp(bsecstate_buffer, cfg.bsecstate) != 0) - nvs_set_str(my_handle, "bsecstate", cfg.bsecstate); + if (nvs_get_blob(my_handle, "bsecstate", bsecstate_buffer, + &required_size) != ESP_OK || + memcmp(bsecstate_buffer, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE + 1) != 0) + nvs_set_blob(my_handle, "bsecstate", cfg.bsecstate, + BSEC_MAX_STATE_BLOB_SIZE + 1); if (nvs_get_str(my_handle, "version", storedversion, &required_size) != ESP_OK || @@ -212,11 +213,11 @@ void loadConfig() { // populate pre set defaults with current values from NVRAM - if (nvs_get_str(my_handle, "bsecstate", NULL, &required_size) == ESP_OK) { - nvs_get_str(my_handle, "bsecstate", cfg.bsecstate, &required_size); + if (nvs_get_blob(my_handle, "bsecstate", NULL, &required_size) == ESP_OK) { + nvs_get_blob(my_handle, "bsecstate", cfg.bsecstate, &required_size); ESP_LOGI(TAG, "bsecstate = %d", - (int)(cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1])); - } + cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE]); + }; if (nvs_get_i8(my_handle, "lorasf", &flash8) == ESP_OK) { cfg.lorasf = flash8; @@ -332,9 +333,9 @@ void loadConfig() { if (nvs_get_i8(my_handle, "payloadmask", &flash8) == ESP_OK) { cfg.payloadmask = flash8; - ESP_LOGI(TAG, "payloadmask = %u", flash8); + ESP_LOGI(TAG, "payloadmask = %d", flash8); } else { - ESP_LOGI(TAG, "payloadmask set to default %u", cfg.payloadmask); + ESP_LOGI(TAG, "payloadmask set to default %d", cfg.payloadmask); saveConfig(); } diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 237a86be..89564b01 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -57,7 +57,7 @@ void doHousekeeping() { // display BME sensor data if present #ifdef HAS_BME - ESP_LOGI(TAG, "BME680 Temp: %.2f°C | IAQ: %.2f", bme_status.temperature, bme_status.iaq); + ESP_LOGI(TAG, "BME680 Temp: %.2f°C | IAQ: %.2f | IAQacc: %d", bme_status.temperature, bme_status.iaq, bme_status.iaq_accuracy); updateState(); #endif