NTP library for WiPy

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
User avatar
andrew
Posts: 22
Joined: Sun Aug 10, 2014 9:22 am

NTP library for WiPy

Post by andrew » Sun Oct 25, 2015 9:02 am

I've hacked a first version of an NTP client library for the WiPy, adapted from this project: https://pypi.python.org/pypi/ntplib/0.3.3

Usage is as follows (don't use this code literally as is; it has a sneaky bug - see http://forum.micropython.org/viewtopic. ... 1068#p6460):

Code: Select all

import untplib

c=untplib.NTPClient()
resp=c.request('0.uk.pool.ntp.org', version=3, port=123)
print("Offset is ", resp.offset)

from machine import RTC
import time

rtc = RTC()
print("Adjusting clock by ", resp.offset, "seconds")
rtc.init(time.localtime(time.time() + resp.offset))
It doesn't deal with timezones or daylight saving time but I don't think NTP is supposed to cater for those, and anyway I'm in the UK so I don't have to worry about that until March 2016...

I've put the library and sample code here: https://github.com/andrewmk/untplib

Andrew

[[Edit: changed name of repository to untplib to avoid confusion]]
Last edited by andrew on Sat Oct 31, 2015 7:40 pm, edited 2 times in total.

patmolloy
Posts: 1
Joined: Tue Oct 13, 2015 2:23 pm

Re: NTP library for WiPy

Post by patmolloy » Sun Oct 25, 2015 9:53 am

Lovely :) Thanks!

alanb
Posts: 6
Joined: Sat Oct 17, 2015 3:39 pm

Re: NTP library for WiPy

Post by alanb » Mon Oct 26, 2015 1:45 am

Very nice, thanks!

PCA
Posts: 10
Joined: Tue Oct 27, 2015 10:20 pm

Re: NTP library for WiPy

Post by PCA » Tue Oct 27, 2015 10:37 pm

Hi Andrew,
Thanks for this job.
Unfortunately it does not work for me. In fact, it does not work the same each time it runs (I get different time.localtime() after each try). Sometimes, the time is quite near of the real time. Sometimes quite far ex:
(2015, 10, 27, 22, 9, 44, 1, 300)
(2015, 1, 1, 0, 23, 54, 3, 1)
...
No error raised. I tested different ntp servers with same result.
Have you an idea how I could debug it ?

Pascal

User avatar
andrew
Posts: 22
Joined: Sun Aug 10, 2014 9:22 am

Re: NTP library for WiPy

Post by andrew » Wed Oct 28, 2015 9:55 pm

PCA wrote:Hi Andrew,
Thanks for this job.
Unfortunately it does not work for me. In fact, it does not work the same each time it runs (I get different time.localtime() after each try). Sometimes, the time is quite near of the real time. Sometimes quite far ex:
(2015, 10, 27, 22, 9, 44, 1, 300)
(2015, 1, 1, 0, 23, 54, 3, 1)
...
No error raised. I tested different ntp servers with same result.
Have you an idea how I could debug it ?

Pascal
I debugged it with the time honoured technique of inserting print statements in the code to see what is happening at different points. First of all I'd add a "print(packed)" after line 193 in untplib.py (https://github.com/andrewmk/untplib/blo ... ib.py#L193) to see what the binary data is that's going to be sent to the server. Then I'd add a "print(response_packet)" at line 320 (https://github.com/andrewmk/untplib/blo ... ib.py#L320) to see what comes back from the server. Then take it from there.

Edited to add:
Actually it would probably be more useful to

Code: Select all

import ubinascii
and then

Code: Select all

print(ubinascii.hexlify(packed))
and

Code: Select all

print(ubinascii.hexlify(response_packet))
Andrew

PCA
Posts: 10
Joined: Tue Oct 27, 2015 10:20 pm

Re: NTP library for WiPy

Post by PCA » Sat Oct 31, 2015 6:33 pm

Hi Andrew,
I think I found my problem.
The call to RTC with empty parameters reinits the clock with a specific date and time. (line rtc = RTC())
The problem comes from the fact that the offset value has been calculated before with a possibly different value of RTC (that is the case by example if you soft reset (Ctrl D) the Wypi board - no problem in case of hard reset).
A solution is to put the RTC() before calling the NTPClient library.
For me the following lines at the end of boot.py work fine :

Code: Select all

import untplib
import time
from machine import RTC
rtc = RTC()
c=untplib.NTPClient()
resp=c.request('1.fr.pool.ntp.org', version=3, port=123)
rtc.init(time.localtime(time.time() + resp.offset))
print("After_adjust", time.localtime())
Hope this help !
Pascal

User avatar
dhylands
Posts: 3067
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: NTP library for WiPy

Post by dhylands » Sat Oct 31, 2015 6:42 pm

You could also check the reset cause and not bother reinitializing the rtc at all if the reason was SOFT_RESET. Something like:

Code: Select all

import time

if machine.reset_cause() != machine.SOFT_RESET:
    import untplib
    from machine import RTC
    rtc = RTC()
    c=untplib.NTPClient()
    resp=c.request('1.fr.pool.ntp.org', version=3, port=123)
    rtc.init(time.localtime(time.time() + resp.offset))
print("Current time", time.localtime())

User avatar
andrew
Posts: 22
Joined: Sun Aug 10, 2014 9:22 am

Re: NTP library for WiPy

Post by andrew » Sat Oct 31, 2015 7:21 pm

Ah, good point. Sorry about that, I cobbled that code example together from two different places and didn't try it out myself. Sorry you wasted time on it, but I bet you understand the code better now :D

Andrew

PCA
Posts: 10
Joined: Tue Oct 27, 2015 10:20 pm

Re: NTP library for WiPy

Post by PCA » Sat Oct 31, 2015 9:05 pm

Hi Andrew,
Absolutely right ! I never took the time to dive into NTP. Now I can say I did ;-) !

Hi Dave,
Yes it is a good fix, but the quality of the clock is rather poor on the Wipy and calling the NTP server one more time is probably a good idea ;-)

Thanks for the help

Pascal

User avatar
pythoncoder
Posts: 3242
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: NTP library for WiPy

Post by pythoncoder » Sun Nov 01, 2015 8:56 am

the quality of the clock is rather poor on the Wipy
All RTC's drift - expensive ones drift less than cheap ones ;) An option to correct for RTC drift is to reset it from NTP periodically - perhaps once a day. But when this occurs the local time is likely to suddenly jump forwards or backwards by several seconds. Depending on your code, this can be more troublesome than a gradual drift.
Peter Hinch

Post Reply