Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BME680 Save and Load State Issue

    BME680 Save and Load State Issue

    Adi
    New Poster

    Hi

    I am using BSEC library with BME680 5min interval. The flow of our device goes like this:

    • Wakeup from device deep sleep "uC is off" then on, only external RTC is up,
    • Collect BME680 reading (accuracy 0), save state on each reading in EEPROM

     

     

    bsec_iot_loop(bme680_sleep, esp_timer_get_time, output_ready, state_save, 1/* 10000 */);​

     

     

    • Power-off uC.
    • Wake up, load the previous reading, and get new reading then save state, and so on..

    The problem is that accuracy is always 0. Appreciate your support on this and if I misunderstood the whole mechanism.. the assumption is that we'll store the state regardless of the accuracy, as it should be cumulatively changed to acc=3 over time.. also I didnt change the timestamp, so each time the device restart (wake-up from deep sleep it'll start counting from the beginning, since in my implementation the uC is completely off), each time it will reset, assuming it will load the state regardless of the timestamp since it should start from there? or I should only store it if its acc=3? Appreciate more clarification.

    Note: The sensor works perfectly in normal operation, with 5 min intervals; no deepsleep.

    14 REPLIES 14

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Adi,

    1.You set save_intvl to 1 in your code "bsec_iot_loop(bme680_sleep, esp_timer_get_time, output_ready, state_save, 1/* 10000 */);"
    @param[in] save_intvl interval at which BSEC state should be saved (in samples)
    Calling BSEC every 300 seconds and saving BSEC status every xxx seconds are two things, don't confuse them.
    2.For your application, the interval between two timestamps is approximately 300 seconds.

    Hi BSTRobin

    Thanks. However, for the first point its based on the comments provided by BOSCH it seems its per sample and not per frequency. So if we pass it as  '1' means that after each reading (regardless of the sleeping duration either 3s or 300s) it will save the state. Its very clear in the comments that came with the library

     

    /* State is saved every 10.000 samples, which means every 10.000 * 3 secs = 500 minutes  */

     

    In my case, since the uC will be shutoff, then the save frequancy should be 1. RTC trigger will switch it on again with fresh start.

    Having said that, I've managed to get the correct timestamp between readings to be approx 332s (which is less that 50%+- recommendation in the library documentation), however, still the accuracy is still zero even after 12hr.

    Adi
    New Poster

    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(&current_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..

    Adi
    New Poster

    Dear BSTRobin

    I am getting IAQ reading now, but the accuracy stuck at 1 and never changes?

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Adi,

    Ensure that your timestamp is correct. In addition, if the accuracy value remains at 1, it is necessary to move board to expose the BME688 to different air environments. If the surrounding air environment remains unchanged, the accuracy may also remain unchanged.

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist