Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    SOLVED

    ESP32 Deep Sleep example is sub-optimal, in principle wrong

    ESP32 Deep Sleep example is sub-optimal, in principle wrong

    itsmaxdd
    Member

    Hello,

    i might be wrong and if so i apologize but here's my understanding about the deep sleep example reported here

    https://github.com/BoschSensortec/BSEC-Arduino-library/blob/master/examples/esp32DeepSleep/esp32Deep...

    The assumption here is that:

    1. nextCall represent a future point in time when BSEC run() function shall be called
    2. esp_timer_get_time() is the time since the board woke up
     
    Said that, the constraint shall be that run() is called as close as possible to the selected sample rate.
     
    Theoretically the sleep calculation shall be as follow:
    1. Fetch the latest point in time before esp32 goes to deep sleep --> this is done via GetTimestamp()
    2. Calculate how far in time it is the nextCall --> (nextCall - GetTimestamp()) * 1000
    3. Subtract the theoretical amount of time the device would take to wake up and reach the run() function (including bootloader and setup() function)

    The example error is in the last point where the code subtracts esp_timer_get_time() by doing so you are taking into account all the "do other stuff" that are happening in between

    itsmaxdd_0-1666360764340.png

    My solution is than

     

     

    void loop() {
        int64_t since_start = esp_timer_get_time(); // this is the amount of time the "setup" function takes to run
        if (sensor.run(GetTimestamp())) {
           //...
        }
        // do other stuff
    
        uint64_t time_us = ((sensor.nextCall - GetTimestamp()) * 1000) - since_start;
        esp_sleep_enable_timer_wakeup(time_us);
        esp_deep_sleep_start();
    }

     

     

    This is also supported by the fact that if ones add

     

    int64_t calltime = GetTimestamp();

     

    right before the run() call the delta is not 3s but 2.796s which is roughly 200ms of "//do other stuff" which is erroneously subtracted on the deep sleep time (it wakes up earlier)
    With the change proposed i instead see 3.064ms (not sure why alway 65ms error is there).

    Let me know if i got it wrong, especially in the assumption part

    1 REPLY 1

    BSTRobin
    Community Moderator
    Community Moderator

    Hi itsmaxdd,

    Your assumption is right, the real misunderstanding is about the function esp_timer_get_time().

    This function is to get time in microseconds since boot, returns number of microseconds since underlying timer has been started.

    Your misunderstood it as getting time stamp.

    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