As a give back to the community, for anyone facing same issues, so basically to run BSEC library in deep sleep while switching of the uC completely (external RTC) you have to do the following: 1- Make sure to have save state and load state implemented correctly: void state_save(const uint8_t *state_buffer, uint32_t length)
{
// ...
// Save the string some form of non-volatile memory, if possible.
// ...
ESP_LOGI("BME680 - state_save", "Saving state: buffer-size %d", length );
nvs_handle_t my_handle;
esp_err_t err = nvs_open("state", NVS_READWRITE, &my_handle);
ESP_ERROR_CHECK(err);
err = nvs_set_blob(my_handle, sensor_binary, state_buffer, length);
ESP_ERROR_CHECK(err);
err = nvs_commit(my_handle);
ESP_ERROR_CHECK(err);
nvs_close(my_handle);
}
uint32_t state_load(uint8_t *state_buffer, uint32_t n_buffer)
{
// ...
// Load a previous library state from non-volatile memory, if available.
//
// Return zero if loading was unsuccessful or no state was available,
// otherwise return length of loaded state string.
// ...
ESP_LOGI("BME680 - state_load", "Load state: buffer-size %d", n_buffer );
nvs_handle_t my_handle;
esp_err_t err = nvs_open("state", NVS_READONLY, &my_handle);
ESP_ERROR_CHECK(err);
err = nvs_get_blob(my_handle, sensor_binary, state_buffer, &n_buffer);
// We close this anyway even if the operation didn't succeed.
nvs_close(my_handle);
if (err == ESP_OK)
{
return n_buffer;
}
ESP_LOGW(TAG, "loading sensor binary blob failed with code %d", err);
return 0;
} 2- Timestamp. After setting the RTC to correct date-time initially, you have to get timestamp using get gettimeofday() then convert it to uS. I've written a small function to get timestamp in uS; you can pass this function as the timestamp calculator: long long get_elapse_time_since_1970()
{
struct timeval current_time;
gettimeofday(¤t_time, NULL);
printf("get_elapse_time_since_1970 - seconds : %ld - micro seconds : %ld",current_time.tv_sec, current_time.tv_usec);
printf("current_time.tv_usec + (current_time.tv_sec * 1000000ll): %lld", current_time.tv_usec + (current_time.tv_sec * 1000000ll));
return current_time.tv_usec + (current_time.tv_sec * 1000000ll);
} 3- Make sure timestamps across readings are 300s or 3s (print timestamp from the bsec output function to cross-check) 4-Save state after each reading; you can set this. bsec_iot_loop(bme680_sleep, esp_timer_get_time, output_ready, state_save, 1/* 10000 */); I hope that this supports everyone else..
... View more