I'm putting the chip to sleep after a few seconds of inactivity, then wake it up through interrupts on a pin. I need a way of knowing how long the chip was asleep. It need not be accurate to the second, so I thought the built in RTC should be enough for this job. However, I couldn't find a way of doing this.
The relevant code is as follows:
Code: Select all
#define SLEEPTIMEOUT 3000
bool wasAsleep = false;
int lastAtive;
void setup(void)
{
lastActive = millis();
}
void loop()
{
// Check if the device has just woken up from sleep
if (wasAsleep)
{
// On waking
wasAsleep = false;
lastActive = millis();
// This is where I'd like to know how long the chip was asleep!
}
if (millis() - lastActive > SLEEPTIMEOUT)
{
uint64_t ext_wakeup_pin_1_mask = 0;
ext_wakeup_pin_1_mask |= (1ULL << WAKEUPPIN);
esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
wasAsleep = true;
esp_light_sleep_start();
delay(500);
}
}