-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NTP sometimes loads a wildly incorrect time #5
Comments
Are you sure it's related to NTP? I also noticed sometimes a very weird clock value, but after some debugging, it seems the problem is the RTC. I added an |
Nope! I am not sure at all. :-) You've already done more research into the problem than I have. If the RTC is returning a bad value, I'd expect the stock firmware to have this same problem. So, that's weird. We are polling the RTC often enough, we can just throw some logic in there that says "if the RTC is zero, or has changed more than 5x the amount of time we think has passed, then don't trust it." And keep ticking based on millis(). Or, trigger an NTP sync. That's weird though. I really would like to understand why the RTC isn't trustworthy. |
As far as I can see, the RTC lib should return 0 if it can't read the time from the RTC and the TimeLib should ignore the time from RTC when it's 0. The weird thing is that the RTC time actually changes to some random value when all I have in the code is RTC.get(). So all I can think of is:
I'm trying now to add a |
No luck :( ... Trying to reduce i2c clock frequency to 25kHz. |
Do you see So far as I can remember, there isn't anything else on I2C. |
@SmittyHalibut the sync provider is set to sync only after 5 mins. I see the RTC time value getting corrupted way more often (sometimes after just a few seconds). I reduced the i2c clock at 10kHz and keeping an eye on it (doing batches of RTC.get periodically)... but it seems way more stable now. I haven't managed to get a corrupted value since.
So in normal use, I rarely see the time going wild, but the endpoint I added made it way easier to trigger a RTC value corruption ( |
I have very similar issue. However RTC is always right, NTP data is wildly incorrect. |
I'm beginning to wonder whether we should take three NTP samples over some amount of time and only use it if all three "match" (+/- the amount of time obviously). |
It just did it again! Real Local time is 16:20 (UTC +1). Serial log:
Those two queries to NTP are one after another, immediately. First one from Setup() and second one from Loop()-> SyncProvider(). |
So for me changing the i2c freq to 10kHz seems to have fixed the issue. The time was always correct in the past couple of weeks. |
@andrei-tatar Those two could be related. Slowing down I2C to 10kHz will make it take longer to send the request, which might be just the amount of time the NTP server needs to let it in again. This is pure speculation on my part though. @aly-fly I think I dug into the NTP library and noticed it has no error checking in it before, but I wouldn't bet more than a couple quid on that right now. My memory was that it just started pulling bytes out of the return packet without checking to make sure the response was a valid "here's your time" packet. Assuming that's the case, we could:
I just haven't had a chance to implement any of this error checking, been working on other projects. But I will happily review and merge a pull request submitted by someone else... ;-) |
Inside SyncProvider() I changed just this part of the code:
It is working fine for the last 20 or so reboots. Previously it has definitely failed at least once during such batch. |
I think I found the reason. arduino-libraries/NTPClient#84 (comment) |
Nice! I haven't looked at your code yet (still drinking coffee for the morning), but have you submitted a pull request to the upstream library with your fix? If not, please do. |
@andrei-tatar ignore my last post to you. I was conflating two things and my statement made no sense at all. I think we are chasing two different problems here. @aly-fly's NTP problem which hopefully they've found and fixed, and the RTC problem you're talking about. I'm surprised the 10kHz clock rate fixes it. The RTC should be way more reliable than that. But there's no denying results. I'm just surprised, that's all. The person with one clock always knows what time it is. The person with two clocks is never quite sure. We have three clocks:
Some logic like the above. |
I sent a Pull request that incorporates all my changes regarding NTP. Since implementation I didn't see any glitches in time synchronization. |
Brilliant. If your PR doesn't get accepted quickly, do me a favor and submit a PR against this repo that updates the documentation to mention your fork. Have it say to use your fork until the upstream accepts the PR. |
Gah! Assuming that's not already in here: #21 |
Correction: Looks like your changes are in the local NTP Client code. I've merged your PR. I'm going to resolve this issue. To anyone else who has problems with time after using this code, please open a new ticket. |
My guess is that we're getting an error packet, or not flushing buffers fully, or something. I'm pretty sure the NTPClient code is pretty trusting in the data it gets from the network and will happily parse something else (eg: an error) as a timestamp and return it.
Anyway. Detect this sort of failure if possible and retry.
The text was updated successfully, but these errors were encountered: