Go Down

Topic: ESP32 - DEEP SLEEP (Read 399 times) previous topic - next topic


Jul 17, 2019, 04:31 pm Last Edit: Jul 19, 2019, 02:31 pm by Nikosant03
Hi everyone,

I am working on the ESP32 microcontroller using Arduino IDE to implement the code. I want to set a deep sleep period for the board more that 1h. I have wrote the code and it compiles fine. My problem is that when I execute and use sleep period greater than the value of long signed int (in micro seconds) it doen't work..

The reason of micro seconds is that the definition of the wake up timer must be done in micro seconds.

When I set a timer less than long signed int ( <2.147.483.647 micro seconds) it works fine. Also I tried to use the uint64_t data type but it doesn't work either.

Any suggestion?

This is the whole code in case that is helps

Code: [Select]
#include <WiFi.h>
#include <NTPClient.h> //For NTP Server
#include <WiFiUdp.h> //For NTP Server

#define uS_TO_S_FACTOR 1000000  // Conversion factor for micro seconds to seconds for deep sleep usage
RTC_DATA_ATTR int TIME_TO_SLEEP_WiFi= 10;     // Time to sleep in case of unavailable WiFi (This is the first sleep)
#define TIME_TO_SLEEP_MAX 80      // MAX Time to sleep in case of repeatable unsuccessfull connection attempts
#define TIME_TO_SLEEP_CASE1 3600 // Time to sleep (3600s = 1h) in if the time is 06:00 - 20:00 
#define TIME_TO_SLEEP_CASE2 9000 // Time to sleep (9000s = 2.5h) if time isn't 06:00 - 20:00 
#define DAY_PERIOD_START 60000  // 06:00 in the morning
#define DAY_PERIOD_END 200000  // 20:00 in the morning

const char * ssid = ***************
const char * password = *************

unsigned long currentMillis;
const unsigned long period=10000; //set the period that esp32 will try to connect with WIFI in ms

// Variables to save date and time
String formattedDate;
String timeStamp;

// Define NTP Client to get time
NTPClient timeClient(ntpUDP);

void setup() {

      currentMillis=millis(); //Assign the ms have been passed since ESP32 code start running
        esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_WiFi * uS_TO_S_FACTOR);
        Serial.println("Going to sleep now for " + String(TIME_TO_SLEEP_WiFi) + " Seconds" + " Due to WiFi unavailability");
        Serial.println("Sleep for " + String(TIME_TO_SLEEP_WiFi));
        while(TIME_TO_SLEEP_WiFi < TIME_TO_SLEEP_MAX){             // While the sleep time is less than the predefined limit keep double the sleep time. When the sleep time exceeds the limit then keep a constant sleep time equal to the limit.
          TIME_TO_SLEEP_WiFi=TIME_TO_SLEEP_WiFi*2;    // Double the sleep time

        TIME_TO_SLEEP_WiFi=TIME_TO_SLEEP_MAX;              // Set the sleep time equal to the limit
      Serial.println("Connecting to WiFi..");

  Serial.print("Connected to ");
  Serial.println(" network");
  timeClient.begin(); // Initialize a NTPClient to get time
  // Set offset time in seconds to adjust for your timezone, for example:
  // GMT +1 = 3600
  // GMT +8 = 28800
  // GMT -1 = -3600
  // GMT 0 = 0
  timeClient.setTimeOffset(10800); //Cyprus Time Zone
  Serial.println("NTP Client initialized");


void loop() {

 while(!timeClient.update()) {
  // The formattedDate comes with the following format:
  // 2018-05-28T16:00:13Z
  formattedDate = timeClient.getFormattedDate();
  Serial.print("Date: ");

  Serial.print("The time is: ");
  String timeE = formattedDate.substring(formattedDate.indexOf("T")+1,formattedDate.length()-1);

// Extract tha time part into clear integer

  int splitT = formattedDate.indexOf("T"); // The output is the number 10 , splitT=10 (Start counting from 0)
  String timeStamp1 = formattedDate.substring(splitT+1,splitT+3 ); // splitT+1 = 11, formattedDate.length()-1 = 19  --> 16:00:13Z
  String timeStamp2 = formattedDate.substring(splitT+4,splitT+6 );
  String timeStamp3 = formattedDate.substring(splitT+7,splitT+9 );  timeStamp = timeStamp1 + timeStamp2 + timeStamp3;
  int result=timeStamp.toInt();

// Check the time in order to adjust the time to sleep period

if(result >= DAY_PERIOD_START && result <= DAY_PERIOD_END) {

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_CASE1 * uS_TO_S_FACTOR);
}else {
  Serial.println("Going to sleep for 2.5h");
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_CASE2 * uS_TO_S_FACTOR);




I would try assigning unsigned long types to your constants so the compiler knows to preserve the type when they are used in an expression.

Like this:

Code: [Select]
#define uS_TO_S_FACTOR 1000000UL
#define TIME_TO_SLEEP_CASE1 3600UL
#define TIME_TO_SLEEP_CASE2 9000UL
Vacuum tube guy in a solid state world


I would try assigning unsigned long types to your constants so the compiler knows to preserve the type when they are used in an expression.
Thank you, I am gonne try it!!

Go Up