ntptime.py settime() does not cope with errors
This arose in the forum.
If an exception occurs in the time() function, msg is not updated so a value of -NTP_DELTA is returned. If this is the intended mechanism for reporting exceptions, settime() should check for this and avoid updating the RTC.
Or perhaps the finally clause in time() should re-raise the exception?
ntptime: Does not handle KoD (Kiss of Death) resonse.
A call to .time() can return a large negative number which results in a rather confusing exception in .settime(). This can occur if an application issues NTP requests too frequently: see this post. I suspect there may also be other circumstances in which an NTP query returns zero.
Another problem reported in discussions is that .time() frequently times out. Clearly the timeout must be short, but this implies that on many networks timeouts will be common.
There are a number of simple fixes. One is for .time() to return an error value in either of these cases which is tested by .settime(). The latter could return a bool indicating success or failure.
Hi Peter
I dislike the ntptime module, I think it is like "how to confuse a cat" !
Probably a lot of users get KoD because they don't realize that ntptime.time() isn't time.time() ...
I wrote my version, with more strict checking about the servers reponse.
Feel free to read and use it.
Ah, you have to remove the compatibility module and the gc_collect()
The compatibility is just to use this module also on windows / linux, defining const
Eventually I can donate it to mp ecosystem.
ntptime2.py.txt
I see now I use a variable MICRO in settime()
Just replace the code with:
and the printf() with print()
Ciao