|
| 1 | +/* |
| 2 | +Simple Deep Sleep with Timer Wake Up |
| 3 | +===================================== |
| 4 | +ESP32 offers a deep sleep mode for effective power |
| 5 | +saving as power is an important factor for IoT |
| 6 | +applications. In this mode CPUs, most of the RAM, |
| 7 | +and all the digital peripherals which are clocked |
| 8 | +from APB_CLK are powered off. The only parts of |
| 9 | +the chip which can still be powered on are: |
| 10 | +RTC controller, RTC peripherals ,and RTC memories |
| 11 | +
|
| 12 | +This code displays the most basic deep sleep with |
| 13 | +a timer to wake it up and how to store data in |
| 14 | +RTC memory to use it over reboots |
| 15 | +
|
| 16 | +This code is under Public Domain License. |
| 17 | +
|
| 18 | +Author: |
| 19 | +Pranav Cherukupalli <[email protected]> |
| 20 | +*/ |
| 21 | + |
| 22 | +#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ |
| 23 | +#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */ |
| 24 | + |
| 25 | +RTC_DATA_ATTR int bootCount = 0; |
| 26 | + |
| 27 | +/* |
| 28 | +Method to print the reason by which ESP32 |
| 29 | +has been awaken from sleep |
| 30 | +*/ |
| 31 | +void print_wakeup_reason(){ |
| 32 | + esp_deep_sleep_wakeup_cause_t wakeup_reason; |
| 33 | + |
| 34 | + wakeup_reason = esp_deep_sleep_get_wakeup_cause(); |
| 35 | + |
| 36 | + switch(wakeup_reason) |
| 37 | + { |
| 38 | + case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; |
| 39 | + case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; |
| 40 | + case 3 : Serial.println("Wakeup caused by timer"); break; |
| 41 | + case 4 : Serial.println("Wakeup caused by touchpad"); break; |
| 42 | + case 5 : Serial.println("Wakeup caused by ULP program"); break; |
| 43 | + default : Serial.println("Wakeup was not caused by deep sleep"); break; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void setup(){ |
| 48 | + Serial.begin(115200); |
| 49 | + delay(1000); //Take some time to open up the Serial Monitor |
| 50 | + |
| 51 | + //Increment boot number and print it every reboot |
| 52 | + ++bootCount; |
| 53 | + Serial.println("Boot number: " + String(bootCount)); |
| 54 | + |
| 55 | + //Print the wakeup reason for ESP32 |
| 56 | + print_wakeup_reason(); |
| 57 | + |
| 58 | + /* |
| 59 | + First we configure the wake up source |
| 60 | + We set our ESP32 to wake up every 5 seconds |
| 61 | + */ |
| 62 | + esp_deep_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); |
| 63 | + Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + |
| 64 | + " Seconds"); |
| 65 | + |
| 66 | + /* |
| 67 | + Next we decide what all peripherals to shut down/keep on |
| 68 | + By default, ESP32 will automatically power down the peripherals |
| 69 | + not needed by the wakeup source, but if you want to be a poweruser |
| 70 | + this is for you. Read in detail at the API docs |
| 71 | + http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html |
| 72 | + Left the line commented as an example of how to configure peripherals. |
| 73 | + The line below turns off all RTC peripherals in deep sleep. |
| 74 | + */ |
| 75 | + //esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); |
| 76 | + //Serial.println("Configured all RTC Peripherals to be powered down in sleep"); |
| 77 | + |
| 78 | + /* |
| 79 | + Now that we have setup a wake cause and if needed setup the |
| 80 | + peripherals state in deep sleep, we can now start going to |
| 81 | + deep sleep. |
| 82 | + In the case that no wake up sources were provided but deep |
| 83 | + sleep was started, it will sleep forever unless hardware |
| 84 | + reset occurs. |
| 85 | + */ |
| 86 | + Serial.println("Going to sleep now"); |
| 87 | + esp_deep_sleep_start(); |
| 88 | + Serial.println("This will never be printed"); |
| 89 | +} |
| 90 | + |
| 91 | +void loop(){ |
| 92 | + //This is not going to be called |
| 93 | +} |
0 commit comments