Hello,
i might be wrong and if so i apologize but here's my understanding about the deep sleep example reported here
The assumption here is that:
- nextCall represent a future point in time when BSEC run() function shall be called
- 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:
- Fetch the latest point in time before esp32 goes to deep sleep --> this is done via GetTimestamp()
- Calculate how far in time it is the nextCall --> (nextCall - GetTimestamp()) * 1000
- 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
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