BME680 state load/save bugfixes

This commit is contained in:
Klaus K Wilting 2019-01-01 21:23:45 +01:00
parent 8267e4e3b6
commit 0fd36b0051
4 changed files with 21 additions and 18 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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