NTP Update

naigy1
Posts: 3
Joined: Thu Jul 14, 2022 12:28 am

NTP Update

Postby naigy1 » Thu Jul 14, 2022 12:55 am

Hi,

I have the following code which is getting the time from NTP just fine. My project when complete with all other functions is to sleep for 20 minutes and then wake up, check sensors / perform needed actions and go back to sleep. I have had issues with the NTP time drifting a lot and want to know how I can get the time to update via a command. I am thinking on having it update after every 3rd or 4th boot.

Any help on what the command is to repoll the ntp would be appreciated. I suspect if my project didn't go to sleep it would keep more accurate time and probably poll the NTP as required but as my project goes to sleep it is probably missing these intervals. I have tried various commands in loops with some possibly being a bit successful but nothing was consistent.

Any help would be appreciated. To clarify I am doing this in Arduino IDE with ESP32 Core 2.0.4

My code is as follows

Code: Select all

#include <WiFi.h>
#include <esp_sntp.h>

#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 20    //This is 20seconds for the purposes of troubleshooting. Once working will be 20 minutes (1200)

RTC_DATA_ATTR long bootcnt = 0;
RTC_DATA_ATTR long ClockCnt = 0;

long startTime;

const char* ssid = "myssid";
const char* password = "myPassword";

const char* ntpServer = "pool.ntp.org";
const char* TZ_INFO    = "EST-10EDT-11,M10.1.0/02:00:00,M4.1.0/03:00:00";

void TimeCalcs()
{  
  struct tm time;
  configTime(0, 3600, ntpServer);
  setenv("TZ", TZ_INFO, 1);
  if(!getLocalTime(&time))
  {
    Serial.println("Could not obtain time info");
    return;
  }
  Serial.print("Current Time: "); Serial.println(asctime(&time)); 
  Serial.print("---------\n");


  if (ClockCnt > 3)
  {
    //Want code here to repoll the NTP. Have tried configTime, getLocalTime($time), sntp_init(), among others
    delay(500);
    ClockCnt = 0;    
  } else
  {
    ClockCnt++;
    Serial.println("ClockCnt: " + String(ClockCnt));
  }
}


void setup()
{
  Serial.begin(115200);
  bootcnt++;
  Serial.println("Boot Count: " + String(bootcnt));   
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

  WiFi.begin(ssid, password);
  startTime = millis();
  while (WiFi.status() != WL_CONNECTED && (millis() - startTime) <= 8000) // try for 8 seconds
  {
    delay(500);
    Serial.print(".");
  }

  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("\nWiFi failed to Connect\n");
  } else
  {
    Serial.print("\nWiFi Connected\n");
  }
  sntp_set_time_sync_notification_cb(timeSyncCallback);
  TimeCalcs();
  delay(500);
  Serial.println("Going to sleep");
  Serial.flush();

  delay(1000);      //Added as per advice on forum. With this had about 5 seconds in 12 hours
  esp_deep_sleep_start();   //Anything after this will never run   
}


void timeSyncCallback(struct timeval *tv)
{
  Serial.println("\n----Time Sync----- Time should have been verified and updated if needed");
  Serial.println(tv->tv_sec);
  Serial.println(ctime(&tv->tv_sec));
}


void loop()
{
  Serial.print("Code here should never run");
}

naigy1
Posts: 3
Joined: Thu Jul 14, 2022 12:28 am

Re: NTP Update

Postby naigy1 » Sun Jul 17, 2022 12:30 am

I have managed to get this working on a regular basis however I have discovered that except for on the cycles that it does a time sync it shows the time as UTC instead of my local time (UTC+10 / 11 in daylight savings). Any idea how I can solve this. I do need my ESP32 to go to deep sleep and also would like to not need to sync NTP on every boot. Any advice would be appreciated.

My code at current is as follows

Code: Select all


#include <WiFi.h>
#include <esp_sntp.h>
#include <time.h>

#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 90

RTC_DATA_ATTR long bootcnt = 0;
RTC_DATA_ATTR long ClockCnt = 0;

long startTime;

const char* ssid = "myssid";
const char* password = "myPassword";

const char* ntpServer = "pool.ntp.org";
const char* TZ_INFO    = "EST-10EDT-11,M10.1.0/02:00:00,M4.1.0/03:00:00";

void setup()
{
  bootcnt++;
  ClockCnt++;
  struct tm timeinfo;
  sntp_set_time_sync_notification_cb(timeSyncCallback);
  Serial.begin(115200);
  Serial.println("Boot Count: " + String(bootcnt) + ". ClockCnt: " + String(ClockCnt));   
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  WiFi.begin(ssid, password);
  startTime = millis();
  while (WiFi.status() != WL_CONNECTED && (millis() - startTime) <= 8000) // try for 8 seconds
  {
    delay(500);
    Serial.print(".");
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.print("\nWiFi Connected\n");
    sntp_stop();    
    delay(2000);
    if (bootcnt == 1)
    {
      TimeSync();
    } else
    {
      if (ClockCnt > 3)
      {
        TimeSync();
      }
    }    
  } else
  {
    if (bootcnt == 1)
    {
      Serial.print("\nWiFi failed to Connect. About to reboot\n");
      ESP.restart();
    }
  } 

  if(!getLocalTime(&timeinfo))
  {
    Serial.println("Could not obtain time info");
    return;
  }
  Serial.print("Current Time: "); Serial.println(asctime(&timeinfo)); 

  delay(5000);
  Serial.println("Going to sleep. Current ClockCnt: " + String(ClockCnt));
  Serial.flush();

  delay(1000); 
  esp_deep_sleep_start();   //Anything after this will never run   
}


void timeSyncCallback(struct timeval *tv)
{
  Serial.println("\n----Time Sync----- Time should have been verified and updated if needed");
  Serial.println(tv->tv_sec);
  Serial.println(ctime(&tv->tv_sec));
  ClockCnt = 0;
}

void TimeSync()
{
  configTime(0, 3600, ntpServer);
  delay(1000);
  setenv("TZ", TZ_INFO, 1);
  delay(1000);
  tzset();
  delay(1000);  
}

void loop()
{
  Serial.print("Code here should never run");
}



markkuk
Posts: 38
Joined: Wed Mar 27, 2019 11:50 am

Re: NTP Update

Postby markkuk » Tue Jul 19, 2022 9:02 am

Remove the setenv() and tzset() calls from timeSync() function, and place them in setup() before calling getLocalTime().

naigy1
Posts: 3
Joined: Thu Jul 14, 2022 12:28 am

Re: NTP Update

Postby naigy1 » Wed Jul 20, 2022 6:46 am

Thank you.

I had found another solution of having a configtime call with no NTP server in the setup and just having the ntp call in the called function. Your solution though is a cleaner method so will change to this. Thanks for the advice.

Who is online

Users browsing this forum: No registered users and 112 guests