Deep Sleep: Best Practices / FAQ
Posted: Mon Feb 24, 2020 2:45 pm
Hi,
Despite I'm experienced in software engineering, the world of ESP32 and Arduino is new to me.
I've some questions regarding the ESP deep sleep functionalities and which best practices has been established to solve these.
Kind regards
Danny
Despite I'm experienced in software engineering, the world of ESP32 and Arduino is new to me.
I've some questions regarding the ESP deep sleep functionalities and which best practices has been established to solve these.
- I use the 3V3 power source of the ESP32 developer board to power my sensors. If my ESP32 goes to deep sleep, the sensors are still power sourced. What is best practice in this case? Using one of the GPIOs as a switch between the VCC/GND and the sensor to cut off power before deep sleep? Or better to use a transistor instead of using the GPIO pin? I assume the sensor is tollerant to these power offs
- Some libraries are working async. This means I can request a messurement and I will get a callback as soon as the results are ready. Is there any possibility that a callback can survive a deep sleep? Otherwise I have to busy wait until the callback has arrived to initiate the deep sleep afterwards.
- How long should the ESP32 sleep that it's worth? Or to rephrase the question, is there a difference in sleeping for 1s and doing something for 0.1 and sleeping for 20s and doing something for 2s. It's the same ratio but propably not the same power consumption overall.
- Some actors / sensors have a (session) state to work properly. Going to deep sleep means the variables and instances gets killed. Is it allowed to attribute all these with "RTC_DATA_ATTR"? Or is there a high potential, that the internal state gets invalid/corrupted? Especially sensors which needs to run for at least several seconds to minutes to produce proper results, are affected by this.
- A lot of libraries use millis() internaly. As far as I have observed, millis() starts from 0 after a wakup. Is there a way to persist this? As a temporary solution, I save the current value to RTC memory and restore it as an offset. To work with I have a helper function which sum up the "millis()" + "offset" + "sleep duration" to have more or less good estimation of the real value. What is the proposed and bullet proofed solution for this? Mainly I use this to check whether a certain job has to be done after wake up or can be done later (like a polling interval).
- Is it useful to combine deep sleep functionality with multitasking to reduce the woken up time by doing stuff in parallel or at least use the waiting time meaningful? How to synchronize the main thread to the others until the ESP32 can sleep again? In Java and other languages there is somethin like CoundDownLatches. This could be easily adapted with the few options ESP32 provides.
- If the main thread initializes communication ports/protocols like HardwareSerial, I2C, OneWire with xyz.begin(), is it allowed to use this ressource from another task (maybe core) as long as I can guarantee that there is no mutual access to the same ressource by using a Mutex? Or is it just allowed, that only the creating (main) task should use the ressource? In that case I need to maintain a flag which the creating task can check and do somethin with it.
Kind regards
Danny