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
#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
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
/*------------------------------------------------------------------------------------------------------*/
while(WiFi.status()!=WL_CONNECTED){
currentMillis=millis(); //Assign the ms have been passed since ESP32 code start running
if(currentMillis>period){
WiFi.disconnect();
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
esp_deep_sleep_start();
}
TIME_TO_SLEEP_WiFi=TIME_TO_SLEEP_MAX; // Set the sleep time equal to the limit
esp_deep_sleep_start();
}
Serial.println("Connecting to WiFi..");
delay(1000);
}
/*-----------------------------------------------------------------------------------------------------*/
Serial.print("Connected to ");
Serial.print(ssid);
Serial.println(" network");
Serial.println(WiFi.macAddress());
Serial.println(WiFi.RSSI());
TIME_TO_SLEEP_WiFi= 10;
/*-----------------------------------------------------------------------------------------------------*/
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");
delay(100);
}
void loop() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
formattedDate = timeClient.getFormattedDate();
Serial.print("Date: ");
Serial.println(formattedDate);
Serial.print("The time is: ");
String timeE = formattedDate.substring(formattedDate.indexOf("T")+1,formattedDate.length()-1);
Serial.println(timeE);
// 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);
esp_deep_sleep_start();
}else {
Serial.println("Going to sleep for 2.5h");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_CASE2 * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
}