I have seen code to sync an RTC with an NTP server every 10 seconds using long delays in the Loop section. I have also seen code that syncs up the RTC only once in the Setup section and not again in the Loop section. I would like to use some code to sync up the RTC once a day (perhaps at 2:00am) and also perform other tasks without using long delays.
You will need a way to connect the Arduino to the Internet. There are several ways to do this and several libraries for each method.
You will need to write, or borrow source from, a NTP client, or sNTP over UDP is likely accurate enough, and easier to implement.
To do things without delay, please refer to the threads/tutorials pinned to the top of this forum as well as the 5001 other threads where the same question is asked and answered, as well as many well written tutorials on the subject.
Bugman1400:
I have seen code to sync an RTC with an NTP server every 10 seconds using long delays in the Loop section.
That sounds pretty dumb, and is more likely just an exercise in reading the time over the internet, i.e. the RTC is redundant.
I have also seen code that syncs up the RTC only once in the Setup section and not again in the Loop section.
Sounds only slightly better. The NTP is used to set the RTC, which is then left to its own devices.
What you propose is better than both. You might not really need an RTC, in this instance, Arduino can get along without one. You set the initial time in the Setup as above, and update as you describe, which i assume is a quiet period. As above, there are plenty of examples around, including clocks with no hardware RTC. If you don't already have an Arduino, you might consider a Node-MCU or similar instead, which is particularly well-suited to this sort of thing, as it has built-in WiFi.
I should’ve been more clear in my original post. I already have an Uno w/ an Ethernet2 shield and an RTC and have the loop code working. I’ve increased the delay from 10 seconds to 100 to reduce hammering the NTP server. The reason for the RTC is in case I lose Comm to the NTP server. I would just like to update (set) the RTC once a day at 2am.
I would just like to update (set) the RTC once a day at 2am.
So, what is holding you back? Ask the RTC what time it is. If it says that it is 2:00:00, ask the NTP server what time it is, and update the RTC (if needed).
I had to do something like that. The problem is to do it once only. I don't know if the below is elegant, and I'm rough with ifs and equals.
void Setup(){
readRTC();
flag=2; // set 0200hrs flag
void loop(){
readRTC();
if (hour==flag)// 0200hrs?
{
setRTC();
flag=1; //not the next time round, do it once
}
if (hour==3) // safe to reset?
{
flag=2; //reset
If the RTC is keeping real time of day, won't it only ever equal 0200 once per day? (assuming he's keeping UTC, not some local time with daylight savings ambiguity, for which the solution IS to keep system time in sync with UTC and output an offset where needed).
Perehama:
sntp and ntp, time.h and time_t all have a way of distinguishing the start of 2:00 am from the remainder of the minute.
You actually have to USE those means. You can't just expect libraries to do that sort of thing automatically.
Besides, it would only be 2:00:00 AM for one second, and it would probably take longer than that to get the time from the the NTP server and process it. Even if it didn't, and you reset the clock more than once during that second, so what?
You guys are making this way harder than it needs to be. Who cares at what exact time the RTC is updated from NTP? Just use a millis() timer to do the update every N milliseconds. That way you can set the update period anywhere from 1 millisecond to 49 days. Done.
The "Time" library (TimeLib.h) has a function "setSyncProvider()". The example "TimeNTP" shows how to set the sync provider to an NTP server.
It also has a function "setSyncInterval(time_t interval); // set the number of seconds between re-sync". Set the interval to 24 hours (24L * 60UL * 60UL or daysToTime_t(1)). Use setTime() at 2AM to synchronize the daily interval with 2AM.
Nick_Pyner:
That's what I do, I think it's the simplest but I just use it to create a new filename. No RTC libraries are involved anywhere.
I have a function called systemConditioning() that runs once every 24h.
It creates a new file name as well among other things. The actual file name is the date. ie 21-08-18.txt
But I also sync with NTP using setSyncProvider()
What John is proposing is a nicer, proper solution I guess...
I’ve poured over the TimeLib & Time libraries and think I understand the setSyncProvider routine but, how would the setSyncInterval routine be used to sync the RTC to the NTP server once a day at 2am?
Bugman1400:
I’ve poured over the TimeLib & Time libraries and think I understand the setSyncProvider routine but, how would the setSyncInterval routine be used to sync the RTC to the NTP server once a day at 2am?
Start with the example File->Examples->Time->TimeNTP that I pointed to.
In the function getNtpTime() convert the NTP time to time_t: