I setup a demo-code that does sync with NTP on first boot
then goes into deepsleep and then does a
Wifi.reconnect()
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
#include <WiFi.h>
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
unsigned long myGotoDeepSleepTimer;
const char *ssid = "";
const char *password = "";
const char* ntpServer = "fritz.box";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 7200;
#include <time.h> // time() ctime()
time_t now; // this is the epoch
tm myTimeInfo; // the structure tm holds time information in a more convient way
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 showTime() {
time(&now); // read the current time
localtime_r(&now, &myTimeInfo); // update the structure tm with the current time
Serial.print("year:");
Serial.print(myTimeInfo.tm_year + 1900); // years since 1900
Serial.print("\tmonth:");
Serial.print(myTimeInfo.tm_mon + 1); // January = 0 (!)
Serial.print("\tday:");
Serial.print(myTimeInfo.tm_mday); // day of month
Serial.print("\thour:");
Serial.print(myTimeInfo.tm_hour); // hours since midnight 0-23
Serial.print("\tmin:");
Serial.print(myTimeInfo.tm_min); // minutes after the hour 0-59
Serial.print("\tsec:");
Serial.print(myTimeInfo.tm_sec); // seconds after the minute 0-61*
Serial.print("\twday");
Serial.print(myTimeInfo.tm_wday); // days since Sunday 0-6
if (myTimeInfo.tm_isdst == 1) // Daylight Saving Time flag
Serial.print("\tDST");
else
Serial.print("\tstandard");
Serial.println();
}
void scanWiFis() {
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": #");
Serial.print(WiFi.SSID(i));
Serial.print("# (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("");
}
void connectToWifi() {
Serial.print("Connecting to #");
Serial.print(ssid);
Serial.println("#");
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
yield();
BlinkHeartBeatLED(OnBoard_LED, 333);
delay(332);
Serial.print(".");
}
Serial.print("\n connected.");
Serial.println(WiFi.localIP() );
}
void synchroniseWith_NTP_Time() {
Serial.print("synchroniseWith_NTP_Time configTime uses ntpServer ");
Serial.println(ntpServer);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.print("synchronising time");
while (myTimeInfo.tm_year + 1900 < 2000 ) {
yield();
time(&now); // read the current time
localtime_r(&now, &myTimeInfo);
BlinkHeartBeatLED(OnBoard_LED, 100);
delay(100);
showTime();
Serial.print(".");
}
Serial.print("\n time synchronsized \n");
showTime();
}
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println(__FILE__);
Serial.print( F(" compiled ") );
Serial.print(__DATE__);
Serial.print( F(" ") );
Serial.println(__TIME__);
}
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\n Setup-Start \n");
PrintFileNameDateTime();
//scanWiFis();
if (bootCount == 0) {
connectToWifi();
}
else {
Serial.print("bootcount=");
Serial.print(bootCount);
Serial.println(" reconnect() to WiFi");
WiFi.reconnect();
Serial.println(" reconnect() done");
}
synchroniseWith_NTP_Time();
++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");
Serial.println("Going to sleep in 10 seconds");
myGotoDeepSleepTimer = millis();
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 100);
if ( TimePeriodIsOver(MyTestTimer, 1000) ) {
showTime();
}
if ( TimePeriodIsOver(myGotoDeepSleepTimer, 10000) ) {
Serial.print("sleeping now for ");
Serial.print(TIME_TO_SLEEP);
Serial.println(" seconds");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
}
And this is what I get in the serial monitor.
The reconnect is very fast and the time is still in sync.
I use my own router as the NTP-server
20:26:35.816 ->
20:26:35.816 -> Setup-Start
20:26:35.816 ->
20:26:35.816 -> Code running comes from file
20:26:35.816 -> F:\myData\Arduino\Sketchbook\ESP32-Get-NTP-Time-Deep-Sleep-001\ESP32-Get-NTP-Time-Deep-Sleep-001.ino
20:26:35.816 -> compiled Apr 16 2023 20:25:59
20:26:35.816 -> Connecting to #FRITZ!Box 4060 TR#
20:26:36.306 -> ....
20:26:37.285 -> connected.192.168.178.51
20:26:37.285 -> synchroniseWith_NTP_Time configTime uses ntpServer fritz.box
20:26:37.319 -> synchronising timeyear:1970 month:1 day:1 hour:0 min:0 sec:2 wday4 standard
20:26:37.390 -> .year:1970 month:1 day:1 hour:0 min:0 sec:2 wday4 standard
20:26:37.495 -> .year:1970 month:1 day:1 hour:0 min:0 sec:2 wday4 standard
20:26:37.600 -> .year:1970 month:1 day:1 hour:0 min:0 sec:2 wday4 standard
20:26:37.705 -> .year:1970 month:1 day:1 hour:0 min:0 sec:2 wday4 standard
20:26:37.811 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:37.914 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:37.986 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.091 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.197 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.302 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.406 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.510 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.617 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.688 -> .year:1970 month:1 day:1 hour:0 min:0 sec:3 wday4 standard
20:26:38.793 -> .year:1970 month:1 day:1 hour:0 min:0 sec:4 wday4 standard
20:26:38.895 -> .year:1970 month:1 day:1 hour:0 min:0 sec:4 wday4 standard
20:26:39.000 -> .year:1970 month:1 day:1 hour:0 min:0 sec:4 wday4 standard
20:26:39.105 -> .year:1970 month:1 day:1 hour:0 min:0 sec:4 wday4 standard
20:26:39.208 -> .year:1970 month:1 day:1 hour:0 min:0 sec:4 wday4 standard
20:26:39.313 -> .year:1970 month:1 day:1 hour:0 min:0 sec:4 wday4 standard
20:26:39.416 -> .year:1970 month:1 day:1 hour:0 min:0 sec:4 wday4 standard
20:26:39.487 -> .year:2023 month:4 day:16 hour:20 min:26 sec:38 wday0 DST
20:26:39.593 -> .
20:26:39.593 -> time synchronsized
20:26:39.593 -> year:2023 month:4 day:16 hour:20 min:26 sec:38 wday0 DST
20:26:39.593 -> Boot number: 1
20:26:39.629 -> Wakeup was not caused by deep sleep: 0
20:26:39.629 -> Setup ESP32 to sleep for every 60 Seconds
20:26:39.629 -> Going to sleep in 10 seconds
20:26:39.629 -> year:2023 month:4 day:16 hour:20 min:26 sec:38 wday0 DST
20:26:40.605 -> year:2023 month:4 day:16 hour:20 min:26 sec:39 wday0 DST
20:26:41.619 -> year:2023 month:4 day:16 hour:20 min:26 sec:40 wday0 DST
20:26:42.600 -> year:2023 month:4 day:16 hour:20 min:26 sec:41 wday0 DST
20:26:43.625 -> year:2023 month:4 day:16 hour:20 min:26 sec:42 wday0 DST
20:26:44.607 -> year:2023 month:4 day:16 hour:20 min:26 sec:43 wday0 DST
20:26:45.615 -> year:2023 month:4 day:16 hour:20 min:26 sec:44 wday0 DST
20:26:46.629 -> year:2023 month:4 day:16 hour:20 min:26 sec:45 wday0 DST
20:26:47.609 -> year:2023 month:4 day:16 hour:20 min:26 sec:46 wday0 DST
20:26:48.621 -> year:2023 month:4 day:16 hour:20 min:26 sec:47 wday0 DST
20:26:49.603 -> year:2023 month:4 day:16 hour:20 min:26 sec:48 wday0 DST
20:26:49.638 -> sleeping now for 60 seconds
20:27:49.173 -> ets Jul 29 2019 12:21:46
20:27:49.173 ->
20:27:49.173 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
20:27:49.173 -> configsip: 0, SPIWP:0xee
20:27:49.173 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
20:27:49.173 -> mode:DIO, clock div:1
20:27:49.173 -> load:0x3fff0030,len:1344
20:27:49.173 -> load:0x40078000,len:13924
20:27:49.173 -> ho 0 tail 12 room 4
20:27:49.173 -> load:0x40080400,len:3600
20:27:49.173 -> entry 0x400805f0
20:27:50.234 ->
20:27:50.234 -> Setup-Start
20:27:50.234 ->
20:27:50.234 -> Code running comes from file
20:27:50.234 -> F:\myData\Arduino\Sketchbook\ESP32-Get-NTP-Time-Deep-Sleep-001\ESP32-Get-NTP-Time-Deep-Sleep-001.ino
20:27:50.234 -> compiled Apr 16 2023 20:25:59
20:27:50.234 -> bootcount=1 reconnect() to WiFi
20:27:50.234 -> reconnect() done
20:27:50.234 -> synchroniseWith_NTP_Time configTime uses ntpServer fritz.box
20:27:50.234 -> synchronising timeyear:2023 month:4 day:16 hour:20 min:27 sec:49 wday0 DST
20:27:50.341 -> .
20:27:50.341 -> time synchronsized
20:27:50.341 -> year:2023 month:4 day:16 hour:20 min:27 sec:49 wday0 DST
20:27:50.376 -> Boot number: 2
20:27:50.376 -> Wakeup caused by timer
20:27:50.376 -> Setup ESP32 to sleep for every 60 Seconds
20:27:50.376 -> Going to sleep in 10 seconds
20:27:50.376 -> year:2023 month:4 day:16 hour:20 min:27 sec:49 wday0 DST
20:27:51.358 -> year:2023 month:4 day:16 hour:20 min:27 sec:50 wday0 DST
20:27:52.342 -> year:2023 month:4 day:16 hour:20 min:27 sec:51 wday0 DST
20:27:53.363 -> year:2023 month:4 day:16 hour:20 min:27 sec:52 wday0 DST
20:27:54.348 -> year:2023 month:4 day:16 hour:20 min:27 sec:53 wday0 DST
20:27:55.363 -> year:2023 month:4 day:16 hour:20 min:27 sec:54 wday0 DST
20:27:56.341 -> year:2023 month:4 day:16 hour:20 min:27 sec:55 wday0 DST
20:27:57.361 -> year:2023 month:4 day:16 hour:20 min:27 sec:56 wday0 DST
20:27:58.372 -> year:2023 month:4 day:16 hour:20 min:27 sec:57 wday0 DST
20:27:59.350 -> year:2023 month:4 day:16 hour:20 min:27 sec:58 wday0 DST
20:28:00.361 -> year:2023 month:4 day:16 hour:20 min:27 sec:59 wday0 DST
20:28:00.361 -> sleeping now for 60 seconds
20:29:00.019 -> ets Jul 29 2019 12:21:46
20:29:00.019 ->
20:29:00.019 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
20:29:00.019 -> configsip: 0, SPIWP:0xee
20:29:00.019 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
20:29:00.055 -> mode:DIO, clock div:1
20:29:00.055 -> load:0x3fff0030,len:1344
20:29:00.055 -> load:0x40078000,len:13924
20:29:00.055 -> ho 0 tail 12 room 4
20:29:00.055 -> load:0x40080400,len:3600
20:29:00.055 -> entry 0x400805f0
20:29:01.097 ->
20:29:01.097 -> Setup-Start
20:29:01.097 ->
20:29:01.097 -> Code running comes from file
20:29:01.097 -> F:\myData\Arduino\Sketchbook\ESP32-Get-NTP-Time-Deep-Sleep-001\ESP32-Get-NTP-Time-Deep-Sleep-001.ino
20:29:01.097 -> compiled Apr 16 2023 20:25:59
20:29:01.097 -> bootcount=2 reconnect() to WiFi
20:29:01.097 -> reconnect() done
20:29:01.097 -> synchroniseWith_NTP_Time configTime uses ntpServer fritz.box
20:29:01.097 -> synchronising timeyear:2023 month:4 day:16 hour:20 min:29 sec:1 wday0 DST
20:29:01.200 -> .
20:29:01.200 -> time synchronsized
20:29:01.200 -> year:2023 month:4 day:16 hour:20 min:29 sec:1 wday0 DST
20:29:01.235 -> Boot number: 3
20:29:01.235 -> Wakeup caused by timer
20:29:01.235 -> Setup ESP32 to sleep for every 60 Seconds
20:29:01.235 -> Going to sleep in 10 seconds
20:29:01.235 -> year:2023 month:4 day:16 hour:20 min:29 sec:1 wday0 DST
20:29:02.216 -> year:2023 month:4 day:16 hour:20 min:29 sec:2 wday0 DST
20:29:03.228 -> year:2023 month:4 day:16 hour:20 min:29 sec:3 wday0 DST
20:29:04.212 -> year:2023 month:4 day:16 hour:20 min:29 sec:4 wday0 DST
20:29:05.233 -> year:2023 month:4 day:16 hour:20 min:29 sec:5 wday0 DST
20:29:06.214 -> year:2023 month:4 day:16 hour:20 min:29 sec:6 wday0 DST
20:29:07.225 -> year:2023 month:4 day:16 hour:20 min:29 sec:7 wday0 DST
20:29:08.205 -> year:2023 month:4 day:16 hour:20 min:29 sec:8 wday0 DST
20:29:09.223 -> year:2023 month:4 day:16 hour:20 min:29 sec:9 wday0 DST
20:29:10.202 -> year:2023 month:4 day:16 hour:20 min:29 sec:10 wday0 DST
20:29:11.221 -> year:2023 month:4 day:16 hour:20 min:29 sec:11 wday0 DST
20:29:11.221 -> sleeping now for 60 seconds
best regards Stefan