http://docs.micropython.org/en/latest/e ... -clock-rtc
So here is how to do it:
Code: Select all
ntptime.settime() # set the rtc datetime from the remote server
This whole thing is under ESP8266:
Code: Select all
def time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1B
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
Traceback (most recent call last):
File "main.py", line 1, in <module>
File "logger.py", line 130, in <module>
File "ntptime.py", line 34, in settime
File "ntptime.py", line 25, in time
OSError: [Errno 116] ETIMEDOUT
Line 25 is s.recv(48). I guess the magic number of 1 second didn't cut it. I wonder if I should just handle the exception or increase 1 second to some other magic number instead.
Also, I would like to convert this datetime tuple into timestamp. I tried a few ways but could get it right.
I know utime.mktime() should be the right tool but it seems to only create a time stamp based off 2000, not 1970.
So I came up with this:
Code: Select all
0x100000000-a1970+time.mktime(time.localtime())
Code: Select all
a1970=time.mktime((1970,1,1,0,0,0,0,0))
So is there any more proper way to get a unix time stamp? Thanks