diff --git a/include/bme680mems.h b/include/bme680mems.h index 00742ff9..a97e0605 100644 --- a/include/bme680mems.h +++ b/include/bme680mems.h @@ -3,7 +3,6 @@ #include "globals.h" #include -#include "irqhandler.h" #include "../lib/Bosch-BSEC/src/bsec.h" extern bmeStatus_t diff --git a/include/globals.h b/include/globals.h index 261c1fb8..4203cef9 100644 --- a/include/globals.h +++ b/include/globals.h @@ -9,6 +9,7 @@ #include #include #include "Mallocator.h" +#include "inc/bsec_datatypes.h" // sniffing types #define MAC_SNIFF_WIFI 0 @@ -54,7 +55,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+1]; // init BSEC state for BME680 sensor + char bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 2]; // init BSEC state for BME680 sensor } configData_t; // Struct holding payload for data send queue @@ -98,6 +99,7 @@ extern std::array beacons; extern TaskHandle_t irqHandlerTask, wifiSwitchTask; +// application includes #include "led.h" #include "payload.h" #include "blescan.h" @@ -106,10 +108,6 @@ extern TaskHandle_t irqHandlerTask, wifiSwitchTask; #include "gpsread.h" #endif -#ifdef HAS_BME -#include "bme680mems.h" -#endif - #ifdef HAS_LORA #include "lorawan.h" #endif @@ -134,4 +132,8 @@ extern TaskHandle_t irqHandlerTask, wifiSwitchTask; #include "sensor.h" #endif +#ifdef HAS_BME +#include "bme680mems.h" +#endif + #endif \ No newline at end of file diff --git a/src/bme680mems.cpp b/src/bme680mems.cpp index 19111ba3..802221e3 100644 --- a/src/bme680mems.cpp +++ b/src/bme680mems.cpp @@ -23,12 +23,14 @@ bsec_virtual_sensor_t sensorList[10] = { 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; // initialize BME680 sensor int bme_init(void) { - + + // return = 0 -> error / return = 1 -> success + // block i2c bus access if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == pdTRUE) { @@ -46,7 +48,7 @@ int bme_init(void) { ESP_LOGI(TAG, "BME680 sensor found and initialized"); else { ESP_LOGE(TAG, "BME680 sensor not found"); - return 1; + goto error; } loadState(); @@ -58,16 +60,21 @@ int bme_init(void) { ESP_LOGI(TAG, "BSEC subscription succesful"); else { ESP_LOGE(TAG, "BSEC subscription error"); - return 1; + goto error; } - xSemaphoreGive(I2Caccess); // release i2c bus access - } else { 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() // Helper function definitions @@ -128,8 +135,8 @@ void loadState(void) { if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1] == BSEC_MAX_STATE_BLOB_SIZE) { // Existing state in NVS stored ESP_LOGI(TAG, "restoring BSEC state from NVRAM"); - memcpy(bsecState, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE); - iaqSensor.setState(bsecState); + memcpy(bsecstate_buffer, cfg.bsecstate, BSEC_MAX_STATE_BLOB_SIZE); + iaqSensor.setState(bsecstate_buffer); checkIaqSensorStatus(); } else // no state stored ESP_LOGI(TAG, @@ -154,9 +161,9 @@ void updateState(void) { } 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; - iaqSensor.getState(bsecState); + iaqSensor.getState(bsecstate_buffer); checkIaqSensorStatus(); ESP_LOGI(TAG, "saving BSEC state to NVRAM"); saveConfig(); diff --git a/src/configmanager.cpp b/src/configmanager.cpp index ba5ed25e..b59349ad 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -80,11 +80,11 @@ void saveConfig() { int16_t flash16 = 0; size_t required_size; 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 || - strcmp(bsecstate, cfg.bsecstate) != 0) + strcmp(bsecstate_buffer, cfg.bsecstate) != 0) nvs_set_str(my_handle, "bsecstate", cfg.bsecstate); 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 - char bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]; - if (nvs_get_str(my_handle, "bsecstate", NULL, &required_size) == ESP_OK) { nvs_get_str(my_handle, "bsecstate", cfg.bsecstate, &required_size); 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) { diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 6293b4af..007561c2 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -72,7 +72,6 @@ // Settings for BME680 environmental sensor (if present) #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 // OTA settings