Ubidots Community

Sleep mode for esp32

Hi, I used the code of deep sleep in example code of the esp32 and I need to update on it. I need when the esp32 wake up for 5 seconds it printing some statement(My idea is keep printing the statement for five minutes), Is this code correct? or how can correct it, please?

#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;
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;
  }
}

void printing(){
  Serial.println("While esp32 is wake up, I'll printing Hi");
}
void Deep_Sleep(){
 
  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32
  print_wakeup_reason();
  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");

  printing();
  Serial.println("Going to sleep now");
//  delay(1000);
  Serial.flush(); 
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}
void setup(){
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor
  Deep_Sleep();
}
void loop(){
  //This is not going to be called
}

Hello, @mela

Thanks for reaching out,

I suggest you check out Espresiff’s documentation pertaining to the ESP32 Sleep Mode which you can find here. Also there are some examples found on the web which follow the documentation and you may find useful, for example this one.

Bear in mind that we at Ubidots don’t provide custom code for specific use cases.

Regards,

-Santiago