khoih-prog / NTPClient_Generic

Library for NTPClient to connect to an NTP time-server. Get time from a NTP time-server and keep it in sync. For ESP8266/ESP32, nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, etc. besides AVR, using ESP WiFi, WiFiNINA, Ethernet W5x00, ENC28J60, LAN8742A, ESP8266/ESP32 AT-command WiFi.
MIT License
28 stars 12 forks source link

Time jumping back when connection is lost and forward when up #9

Closed iBuilderBY closed 1 year ago

iBuilderBY commented 1 year ago

Hello! I've got the problem and I know when it happen. It's happen when the connection is lost - time jump back on NTP_UPDATE_INTERVAL_MS. When connection is up - time jump forward. Steps:

  1. config system and start + connected to NTP server.
  2. lost conection.
  3. after NTP_UPDATE_INTERVAL_MS time's up - system time jump back on NTP_UPDATE_INTERVAL_MS value.
  4. connection is up - time jump forward.
khoih-prog commented 1 year ago

Hi @iBuilderBY

Thanks to your detailed bug report I now can duplicate and certainly will nail down and fix this elusive bug.

It seems that some old packets, still in buffer, create the false update after disconnected.

Best Regards,

===

Starting NTPClient_Generic_Issue using NTPClient_Generic
...
[NTP] Update from NTP Server
00:00:01
[NTP] Update from NTP Server
[NTP] numBytesRead (48) = 48
[NTP] isValid: leapIndicator (!=3) = 0 , version (>=1) = 4
[NTP] stratum (1 <= stratum <= 15) = 1 , refTimeInt (!= 0) = 3875454547
[NTP] refTimeFrac (!= 0) = 2963664409
19:09:22
[NTP] Update from NTP Server
19:09:32
[NTP] Update from NTP Server
19:09:42
[NTP] Update from NTP Server
19:09:52
[NTP] Update from NTP Server
19:10:02
Changed, WiFiStatus = Disconnected   <== Disconnected
[NTP] Update from NTP Server
19:10:12
[NTP] Update from NTP Server         <======== Bad old packet updates the wrong time after disconnected NTP_UPDATE_INTERVAL_MS
[NTP] numBytesRead (48) = 48
[NTP] isValid: leapIndicator (!=3) = 0 , version (>=1) = 4
[NTP] stratum (1 <= stratum <= 15) = 1 , refTimeInt (!= 0) = 3875454547
[NTP] refTimeFrac (!= 0) = 2963664409
19:09:32
[NTP] Update from NTP Server
19:09:42
[NTP] Update from NTP Server
19:09:52
[NTP] Update from NTP Server
19:10:02
[NTP] Update from NTP Server
19:10:12
[NTP] Update from NTP Server
19:10:22
[NTP] Update from NTP Server
19:10:32
[NTP] Update from NTP Server
19:10:42
Changed, WiFiStatus = Disconnected
[NTP] Update from NTP Server
19:10:52
Changed, WiFiStatus = Connected
[NTP] Update from NTP Server
[NTP] numBytesRead (48) = 48
[NTP] isValid: leapIndicator (!=3) = 0 , version (>=1) = 4
[NTP] stratum (1 <= stratum <= 15) = 3 , refTimeInt (!= 0) = 3875454691
[NTP] refTimeFrac (!= 0) = 607159331
19:11:52
[NTP] Update from NTP Server
19:12:02
[NTP] Update from NTP Server
19:12:12
khoih-prog commented 1 year ago

Hi @iBuilderBY

Found out the bug and will publish a release within hour. I now have to flush the packets in queue.

BR,


The new debug terminal

18:53:24.512 -> Starting NTPClient_Generic_Issue using NTPClient_Generic
18:53:25.142 -> ...
18:53:26.203 -> ********UPDATED********
18:53:26.203 -> 18:53:26
18:53:36.191 -> ********UPDATED********
18:53:36.191 -> 18:53:36
18:53:46.182 -> ********UPDATED********
18:53:46.182 -> 18:53:46
18:53:56.204 -> ********UPDATED********
18:53:56.204 -> 18:53:56
18:54:06.193 -> ********UPDATED********
18:54:06.193 -> 18:54:06
18:54:16.181 -> ********UPDATED********
18:54:16.181 -> 18:54:16
18:54:26.229 -> ********UPDATED********
18:54:26.229 -> 18:54:26
18:54:36.219 -> ********UPDATED********
18:54:36.219 -> 18:54:36
18:54:46.209 -> ********UPDATED********
18:54:46.209 -> 18:54:46
18:54:56.231 -> ********UPDATED********
18:54:56.231 -> 18:54:56
18:55:06.220 -> ********UPDATED********
18:55:06.220 -> 18:55:06
18:55:06.220 -> Changed, WiFiStatus = lost WiFi SSID
18:55:16.208 -> ********UPDATED********
18:55:16.208 -> 18:55:16
18:55:27.227 -> ********UPDATED********
18:55:27.227 -> 18:55:27
18:55:38.248 -> ********UPDATED********
18:55:38.248 -> 18:55:38
18:55:49.265 -> ********UPDATED********
18:55:49.265 -> 18:55:49
18:55:59.318 -> ********UPDATED********
18:55:59.318 -> 18:55:59
18:55:59.318 -> Changed, WiFiStatus = Connected
18:56:09.307 -> ********UPDATED********
18:56:09.307 -> 18:56:09
khoih-prog commented 1 year ago

Hi @iBuilderBY

The new NTPClient_Generic v3.7.5 has just been released. Your contribution is noted in Contributions and Thanks.

Please have more tests to see if there is still any bug, then make new good issue report ;=}}

Best Regards,


Releases v3.7.5

  1. Fix bug causing time jumping back or forth when network has problem. Check Time jumping back when connection is lost and forward when up #9 and Time jumping back 9 minutes #1
  2. Modify example ESP_NTPClient_Advanced to demo the better way to use the library, without using long delay()
  3. Add support to RP2040W boards using CYW43439 WiFi
  4. Optimize and clean up
  5. Modify platformio.ini and library.json to reduce error with PIO
  6. Update Packages' Patches