Closed RobInSaratoga closed 1 year ago
Hello,
The NTP.setMinSyncAccuracy (5000);
is in microseconds (us).
You can find it in ESPNtpClient.h, the default value is DEFAULT_MIN_SYNC_ACCURACY_US
.
It's been a few months and I have learned a few things. First, there is little need to use the TimeLib.h library, when I can just use the regular C++ functions and access the time that ESPNtpClient is updating directly. TimeLib.h is very convenient to use, it's well supported and documented - perfect for someone new to programming. But as I learned more about time.h (ctime) I realized I could get what I wanted without the complexity of getting the SDK set accurately and then trying to synchronizing it to the TimeLib.h so that I can display it. Furthermore, the ESPNtpClient library is very convenient to use with regard to wifi, and initial synchronization (it syncs several times initially, but then less often once the clock is set).
I offer my experience here to others in case it may help them.
I learned how to grab the exact "top of the minute" (okay... not EXACTLY, but really, really close) and format the output to display it to an LCD. Now I have a very, very accurate desk clock that stays in synchrony to the atomic reference on my shortwave radio. Using TimeLib.h the clock was never spot-on as the library was never intended for precision beyond one second.
Here is a snip of the relevant code.
` if (syncEventTriggered == true) { //if the time has been set, then... syncEventTriggered == false; utc = (NTP.millis() / 1000); //this makes the clock display time at the very top of each second if (utc != prevDisplay) { //this makes the clock display change only if it's different from last time prevDisplay = utc; time_t now; char Dbuff[20]; char Tbuff[20]; strftime(Dbuff, 20, "%a, %b %d, %Y", localtime(&now)); //weekday, month, date, year strftime(Tbuff, 20, "%I:%M:%S %p %Z", localtime(&now)); //hour(12):minute:second, AM/PM, time zone
// LCD output
lcd.setCursor (3, 0);
lcd.print(Tbuff);
lcd.setCursor (2, 1);
lcd.print(Dbuff);
`
Reference issue #26
I was able to stop the segfaults by changing the code in the loop to remove the delays. (I think someone else on this list mentioned a similar problem...).
Anyhow, I have the code working, partially. Thank you for the help. I have pasted my current version below. A few notes, then a question or two...
The code outputs on the serial port and the LCD screen. I added an IF statement in the loop to check that it was time (i.e. at the "top" of a new second) to print the time to the serial port. This was successful; here is some output;
You can see that the NTP.getTimeDateStringUs () print out is at (nearly) the top of the new second (or close enough for me). I would like to enable a similar condition to the update of the LCD displayed time.
(I set a pin high and then low to give me a reference pulse to measure using an oscilloscope.)
At first, the LCD display was one second behind reference time (* in this case that means listening to the shortwave broadcast of the atomic clock in the USA), and one second behind the serial output. So I added +1 to the seconds in the setTime() statement. However, after running overnight, the LCD display output and serial output were one second fast.
Can you help me understand what I am doing wrong?
My goal here is to set the time in the TimeLib at the top of the second after the NTP update has occurred. I was not successful using the "if NTP.millis() % 1000 == 0, then setTime(etc...)" approach in the event handler. I made a note in the code below. I was thinking that this condition would make the code wait until the top of the second to update the TimeLib (which can only count in whole seconds, not milliseconds or microseconds).
Can you recommend a way to do this?
Also, is this 50000 in microseconds?
Many thanks.