2017-06-29 09:31:39 +02:00
|
|
|
/*
|
|
|
|
Simple Deep Sleep with Timer Wake Up
|
|
|
|
=====================================
|
|
|
|
ESP32 offers a deep sleep mode for effective power
|
|
|
|
saving as power is an important factor for IoT
|
|
|
|
applications. In this mode CPUs, most of the RAM,
|
|
|
|
and all the digital peripherals which are clocked
|
|
|
|
from APB_CLK are powered off. The only parts of
|
|
|
|
the chip which can still be powered on are:
|
|
|
|
RTC controller, RTC peripherals ,and RTC memories
|
|
|
|
|
|
|
|
This code displays the most basic deep sleep with
|
|
|
|
a timer to wake it up and how to store data in
|
|
|
|
RTC memory to use it over reboots
|
|
|
|
|
|
|
|
This code is under Public Domain License.
|
|
|
|
|
|
|
|
Author:
|
|
|
|
Pranav Cherukupalli <cherukupallip@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
|
|
|
|
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
|
|
|
|
|
|
|
|
RTC_DATA_ATTR int bootCount = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Method to print the reason by which ESP32
|
|
|
|
has been awaken from sleep
|
|
|
|
*/
|
|
|
|
void print_wakeup_reason(){
|
2017-09-12 10:09:59 +02:00
|
|
|
esp_sleep_wakeup_cause_t wakeup_reason;
|
2017-06-29 09:31:39 +02:00
|
|
|
|
2017-09-12 10:09:59 +02:00
|
|
|
wakeup_reason = esp_sleep_get_wakeup_cause();
|
2017-06-29 09:31:39 +02:00
|
|
|
|
|
|
|
switch(wakeup_reason)
|
|
|
|
{
|
|
|
|
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
|
|
|
|
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
|
|
|
|
case 3 : Serial.println("Wakeup caused by timer"); break;
|
|
|
|
case 4 : Serial.println("Wakeup caused by touchpad"); break;
|
|
|
|
case 5 : Serial.println("Wakeup caused by ULP program"); break;
|
2018-09-17 21:31:24 +02:00
|
|
|
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
|
2017-06-29 09:31:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup(){
|
|
|
|
Serial.begin(115200);
|
|
|
|
delay(1000); //Take some time to open up the Serial Monitor
|
|
|
|
|
|
|
|
//Increment boot number and print it every reboot
|
|
|
|
++bootCount;
|
|
|
|
Serial.println("Boot number: " + String(bootCount));
|
|
|
|
|
|
|
|
//Print the wakeup reason for ESP32
|
|
|
|
print_wakeup_reason();
|
|
|
|
|
|
|
|
/*
|
|
|
|
First we configure the wake up source
|
|
|
|
We set our ESP32 to wake up every 5 seconds
|
|
|
|
*/
|
2017-09-12 10:09:59 +02:00
|
|
|
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
|
2017-06-29 09:31:39 +02:00
|
|
|
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
|
|
|
|
" Seconds");
|
|
|
|
|
|
|
|
/*
|
|
|
|
Next we decide what all peripherals to shut down/keep on
|
|
|
|
By default, ESP32 will automatically power down the peripherals
|
|
|
|
not needed by the wakeup source, but if you want to be a poweruser
|
|
|
|
this is for you. Read in detail at the API docs
|
|
|
|
http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
|
|
|
|
Left the line commented as an example of how to configure peripherals.
|
|
|
|
The line below turns off all RTC peripherals in deep sleep.
|
|
|
|
*/
|
|
|
|
//esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
|
|
|
//Serial.println("Configured all RTC Peripherals to be powered down in sleep");
|
|
|
|
|
|
|
|
/*
|
|
|
|
Now that we have setup a wake cause and if needed setup the
|
|
|
|
peripherals state in deep sleep, we can now start going to
|
|
|
|
deep sleep.
|
|
|
|
In the case that no wake up sources were provided but deep
|
|
|
|
sleep was started, it will sleep forever unless hardware
|
|
|
|
reset occurs.
|
|
|
|
*/
|
|
|
|
Serial.println("Going to sleep now");
|
2018-09-17 21:31:24 +02:00
|
|
|
Serial.flush();
|
2017-06-29 09:31:39 +02:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
Serial.println("This will never be printed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop(){
|
|
|
|
//This is not going to be called
|
|
|
|
}
|