Time sync with time.nist.gov failing (mostly) with web server in sketch

I have a large sketch on a Mega 2560 that tracks the time, reads sensor data, and writes a timestamp and data to an SD card and a web server. All worked fine for months–until I changed from using a fixed NIST ip address for syncing the time to using time.nist.gov.

The time will sync with time.nist.gov at startup (usually after a couple tries), but later it starts failing. I’ve replicated the problem by combining the TimeNTP and WebServer example sketches (attached due to space limitation), modified to count and display the number of sync successes and failures.

The sketch can be compiled using a fixed ip for the time server, or time.nist.gov, and the SD and web server functionality can be turned on or off by commenting out the appropriate “defines” at the beginning of the sketch.

Here are the sync failure rates I get with time.nist.gov after around 100 - 200 sync attempts:
1.5% with both web server and sd logging turned off (after two failures at startup)
4% with web server off and sd logging on (after two failures at startup)
85% with web server on and sd logging off;
98% with web server on and sd logging on
Syncing with an ip address for the time server, I get a 0% failure rate in all cases. But I’d rather use the time.nist.gov url to ensure that there is always a time server available.

Is there something in my code causing the high sync failure rate with time.nist.gov, particularly with the web server on? Or is this a hardware/firmware issue? Both my Mega 2560 and Ethernet shield (Wiznet 5100) are clones. I am using IDE 1.6.12. The DNS server is my router, set by default, which uses dynamic DNS.

Thanks for any thoughts anyone has on this.

TimeNTPLogSDweb2.ino (9.01 KB)

ditch the String Class

String getDateTime(long UnixFormat) {

use the F macro on ALL of your string constants...

Serial.print("Ethernet shield IP is ");
  Serial.println(Ethernet.localIP());
  Serial.print("Selected time server is ");
  Serial.println(timeServer);
  delay(500);

#ifdef sdLog
  // initialize SD card
  Serial.print(F("Initializing SD card..."));

if a string is constant, make it const

const char* const fileName = "synclog.csv";

cpl99:
...until I changed from using a fixed NIST ip address for syncing the time to using time.nist.gov...

In addition to using a stratum 1 server you are loading a specific stratum 1 server. Why would you do that?

Allow me to introduce you to...
http://support.ntp.org/bin/view/Servers/RulesOfEngagement

Thanks for any thoughts anyone has on this.

You are using a server (group) that is heavily loaded. Don't. Use the pool.

Well, because just about all the example sketched in the IDE do exactly that. And apparently NIST doesn't have a problem with it. It practically encourages people to use their stratum 1 servers to sync their computer clocks. See: www.nist.gov/pml/time-and-frequency-division/services/internet-time-service-its

In any case, I'd be more than happy to use pool.ntp.org for syncing the time. But that doesn't work any better than time.nist.gov does in the attached sketch.

So unless I can figure out why syncing to a time server url fails so often in my sketches (and I really doubt it has anything to do with the strings), I am going to stick with what works. :slight_smile: