Closed PiAir closed 6 years ago
ntp_sync can return before server synchronization has occurred. You can use the function rtc.synced() to check whether the module has successfully synchronized with the ntp server.
rtc = machine.RTC() rtc.ntp_sync('pool.ntp.org', 3600)
while not rtc.synced(): time.sleep_ms(50)
print(rtc.now())
Regarding the second issue, please can you try using google's dns server 8.8.8.8 and see if it is more consistent ?
this works for me:
class TimeUtil:
global format_time
def format_time(t_tuple):
return '{0}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}'.format(*t_tuple)
from machine import RTC
import time
import socket
NTP_HOST='ntp.jitter.eu'
# work around first lookup failing
socket.getaddrinfo(NTP_HOST, 23)
rtc = RTC()
rtc.ntp_sync(NTP_HOST, 86400)
while not rtc.synced():
time.sleep_ms(100)
print("ntp: synced - " + format_time(rtc.now()))
I think this issue has been resolved, so I'll close this. If you think otherwise, feel free to reopen it and add more details!
When using a static IP address (instead of DHCP), the ntp_sync() doesn't work when using the FQDN 'pool.ntp.org' although it does work when DHCP is used or when an IP-address is used.
Also (I am adding it here in this issue because it might be caused by the same problem), socket.getaddrinfo() often returns '0.0.0.0' while using a static IP-address: