ESP32 Deep Sleep, using millis in Arduino IDE, and then trying to do something in between
Posted: Mon Jul 06, 2020 8:43 am
ESP32 Deep Sleep, using millis in Arduino IDE, and then trying to do something in between
I want to be able to take 4 analog readings and 1 I2C reading and then have the ESP32 go into deep sleep mode and wake up every 20 seconds or so, but right now I am keeping it simple just to try out the code where it prints "hello world" in between the deep sleep mode.
I want to do all this using millis instead of delay to avoid using blocking code.
I have the code below but I suspect that it's not printing "hello world" because the setup code puts the device to sleep before it can print. Any idea how I can get this working so that it's able to print and then go to sleep?
I want to be able to take 4 analog readings and 1 I2C reading and then have the ESP32 go into deep sleep mode and wake up every 20 seconds or so, but right now I am keeping it simple just to try out the code where it prints "hello world" in between the deep sleep mode.
I want to do all this using millis instead of delay to avoid using blocking code.
I have the code below but I suspect that it's not printing "hello world" because the setup code puts the device to sleep before it can print. Any idea how I can get this working so that it's able to print and then go to sleep?
Code: Select all
/*
Deep Sleep Timer Wake Up Example by Pranav Cherukupalli
https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/
*/
#include "esp32-hal-cpu.h"
#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;
uint32_t previousTime = 0;
uint32_t loopInterval = 3000; //milliseconds
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
setCpuFrequencyMhz(40); //Set CPU clock to 40 MHz
Serial.print(getCpuFrequencyMhz()); //Get CPU clock
Serial.println();
//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
*/
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
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");
Serial.flush();
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() {
if ( millis() - previousTime > loopInterval )
{
previousTime = millis();
Serial.print("hello world!");
}
}
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}