ESP32 - RTC off by 2hrs when used deepsleep

Hello, I've mixed SimpleTime and DeepSleep TimerWakeUp examples and created the below program that prints time from the internal RTC every 20 secs. The time is printed correctly when I first start the ESP32, but after the first sleep cycle, the time is off by 2 hrs and stays off consistently. I couldn't figure out the problem. Can someone please help?

My Code:

#include <WiFi.h>
#include "time.h"
#include "WiFi_credentials.h"

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  20        /* Time ESP32 will go to sleep (in seconds) */

enum states_main {RST , TIME};
RTC_DATA_ATTR uint8_t state = RST;

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

void sleep(){
  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 now");
  Serial.println("---------------------------------------------");
  Serial.println(" ");
  Serial.flush();
  esp_deep_sleep_start();
}


void setup()
{
  Serial.begin(115200);
  delay(100);
  
  switch(state) {
    
    case RST:
      Serial.println("In RESET");
      //connect to WiFi
      Serial.printf("Connecting to %s ", ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
          delay(500);
          Serial.print(".");
      }
      Serial.println(" CONNECTED");
  
      //init and get the time
      configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
      printLocalTime();
    
      //disconnect WiFi as it's no longer needed
      WiFi.disconnect(true);
      WiFi.mode(WIFI_OFF);
      state = TIME;
      sleep();
    break;
    
    case TIME:
      Serial.println("In TIME");
      printLocalTime();
      sleep();
    break;
  
  }
}

void loop()
{
  
}

Output from the Serial Terminal

In RESET
Connecting to WiFi ... CONNECTED
Tuesday, August 17 2021 19:04:33
Setup ESP32 to sleep for every 20 Seconds
Going to sleep now
---------------------------------------------
 
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
In TIME
Tuesday, August 17 2021 17:04:53
Setup ESP32 to sleep for every 20 Seconds
Going to sleep now
---------------------------------------------
 
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
In TIME
Tuesday, August 17 2021 17:05:14
Setup ESP32 to sleep for every 20 Seconds
Going to sleep now
---------------------------------------------
 
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
In TIME
Tuesday, August 17 2021 17:05:34
Setup ESP32 to sleep for every 20 Seconds
Going to sleep now
---------------------------------------------
 
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
In TIME
Tuesday, August 17 2021 17:05:54
Setup ESP32 to sleep for every 20 Seconds
Going to sleep now
---------------------------------------------

As we can see the correct time was 19:04:33 but after the first deep sleep cycle, it got changed to 17:04:53 instead of 19:04:53. Can someone please tell me why this is happening?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.