Sprinkler project - NTP time sync issue

Hi everyone, I am new on the forum, but have been following and reading a lot …

I started a Sprinkler project, which is fully operational and working, but I have one small issue with the time in my project.

I use an Arduino UNO + Ethernet shield and connect to an NTP server for getting the time.
However, after a day or sometimes two, the hour is one hour behind and every day/two, it skips another hour … I cannot seem to pinpoint where in my code it goes wrong.

If anyone has ideas, let me know please. Thank you !

I’ve included some pictures of the project and the code I’m using.

Sprinklers_Final_250615_forum.ino (12.1 KB)

Is it a gradual change or does it jump an hour ?

It jumps an hour at a time, not gradual. TY for looking at it.

I recommend moving Udp.begin() call to setup. It should be called only once.

Have you tried another time server? I did have a problem similar to that due to the time server.

Hm yes another time server might be worth a shot. Maybe we wrongfully assume these things are always correct, but maybe not :)

I will also move the udp.begin part as you suggested.

Will post back with result when I had a chance to test.

Thank you !

I use DNS to get a current NTP server from pool.ntp.org. Here is a link to the test code. That way I don't have to worry much about using a faulty server or the server going offline. It gets a new server every boot. You can use the same technique if you are planning on the Arduino unit staying up for long periods of time and want it to get a current NTP server IP now and then. http://forum.arduino.cc/index.php?topic=137789.msg1038071#msg1038071

Another great suggestion, will look into that as well, TY for the tip.

KDeP: If anyone has ideas, let me know please. Thank you !

I think your NTP time syncing code is not fail safe.

You try syncing 10 tries with a 1 second delay after sending the NTP request.

But what happens if the time server is slow in answering your requests? What if none of 10 requests in a row are answered within one second? And if even not the first reqest is answered within 10 seconds?

What if the first 9 out of 10 requests are not answered at all and the 10th and last request is answered after 2 seconds?

In that case this will happen: - 9 requests time out and give no NTP time result - 10th request will also time out, while no packet received within 1 second - 1 second later the answer will be received and stay in the packet buffer - after 3600 seconds, you start 10 new requests - and now you will retrieve the answering packet that has been received 3589 seconds earlier - so after one hour you do not evaluate what was the answering package to a new request, but you evaluate the answering package to a request that was sent 3591 seconds earlier and to which you got an answering package 3589 seconds earlier.

Who attached this comment: // ******** FUNCTIONS ******** // Do not alter this function, it is used by the NTP system int getTimeAndDate()

I think it would be much better to debug buggy code than to forbid any changes on buggy code.

Even better would be to use a reliable NTP time server.

What is this for a time server? // ******** NTP Server Settings ******** IPAddress timeServer(195, 200, 224, 66);

In which country is your Arduino to be operated? Don't you know any "official and reliable" time servers in your country, that are provided by governmental or semigovernmental organizations for "public access" to NTP time?