ESP32 - NTP Based Clock - Synch Question

I've been building ucontroller projects for many years, but have only used simple 8 bit chips such as the AVR and related Arduino... Recently I have been expanding horizons to the ESP32 world using M5Stack components.

I'm sure that I am not the only maker who likes to build clocks - so of course my first ESP32 project will be a clock that takes advantage of WiFi and NTP synchronization. I've been doing a lot of research and searched the forums for my particular question, so apologies if this has been asked before (I really tried !)...

I've got the basics working - WiFi connected and NTP synch. My project includes a seconds display and will trigger a relay at some scheduled times during the day - so far, so good. I am using the standard Wifi.h and Time.h libraries as discussed in this video and this github.

I understand that these libraries will automatically resynch with NTP servers every hour without user intervention... As there is no 'background' task running, I assume that resynch is triggered when time() is called in my code. OK.

Question - based on initial observations, this 'resynch' is obtained rather quickly, but as with any network related topic I suspect there will be times when a network call to the NTP server takes a bit of time. Meanwhile, I'd like my display to show seconds updates uninterrupted and most important any scheduled tasks need to be timely.

Currently, I am saving the current second to a variable and using a while() loop that calls time() until the seconds have changed, at which point I update the display etc. But what happens if an NTP update is initiated in the library code while my delay loop is running? Is it possible that I miss a few seconds dur to the NTP call? The result would not only be the display looking odd, but potentially missing an event that is scheduled to the second.

I'm not certain if I am overthinking this or if I am handling things poorly - C++ is not really my coding strength.

Here is a sample of what my main loop looks like:

void loop() {
  // getTimeReducedTraffic(3600);
  
  auto t = time(nullptr);
  auto tm = localtime(&t);  // for local timezone.
  getNTPtime(10);
  showTime(timeinfo);
  //delay(1000);
  while (LastSecond == tm->tm_sec) {
        // loop until next second
        t = time(nullptr);
        tm = localtime(&t);  // for local timezone.
    }
    LastSecond = tm->tm_sec;
}

Thanks for any tips / suggestions!

EDIT - I have considered migrating to using the onboard RTC on my module for the primary source of time and synchronizing the RTC with NTP once or twice a day. While that may allow me to move the NTP update to less important time periods (ie overnight), technically would the seconds display still be thrown off if an NTP update is triggered? I think my primary misunderstanding is exactly how the NTP updates occur.

no.
The "resync" is in a specific interval. Currently it is 180 minutes.
you can track when the resync is done because you can activate a callback function for that.

Furthermore you can adopt the update interval.

Here you find some information how to do that:

1 Like

Probably not. I don't expect Esp time keeping to drift few seconds in few hours.

1 Like

What I mean is that the NTP sync task takes time away from my code that is updating the display... So the display might 'skip' digits. Worse, if I need something to trigger at an exact second and the NTP update is running at that time I may miss it.

Perhaps my best option is to either manually call the NTP update or otherwise modify the interval so that it occurs at times that will not bother my task schedule or likely to be observable (ie overnight or early morning hours) ?

I don't think so. Probably running on different core and for sure it's not blocking Esp for few seconds.

Hmm... I'm new to the ESP32 environment. I know my controller has 2 cores, but I haven't yet investigated how to determine what runs where (if I even can). I thought everything was running as one big single task unless I coded some kind of thread control?

But if that's the case, I would be all set !

I's quite sophisticated and important tasks are done on different core from your display code. If you don't have a problem, just let it run...
Also the timekeeping is reasonable if you don't use deep sleep. Maybe 1s/day without synch.

1 Like

Is that with RTC or just main clock? I have M5Stack controllers that have RTC (DinMeter / Core 2 S3) and some that do not (Fire).

Thanks for the insight into the tasks / cores... The ESP32 is new territory for me :wink:

EDIT - I am also considering using a GPS module for time source... Provided I can get a signal, that would eliminate any network related issues and might be more reliable. Plus, I've always wanted to play with GPS.

With main timers.

1 Like

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