esp8266/modules/ntptime.py: Always close socket, and set day-of-week.

Fixes issue #5189.
This commit is contained in:
Damien George 2019-10-10 18:05:56 +11:00
parent 580a2656d1
commit 79ab82ea77
1 changed files with 7 additions and 6 deletions

View File

@ -17,10 +17,12 @@ def time():
NTP_QUERY[0] = 0x1b NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1] addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1) try:
res = s.sendto(NTP_QUERY, addr) s.settimeout(1)
msg = s.recv(48) res = s.sendto(NTP_QUERY, addr)
s.close() msg = s.recv(48)
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0] val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA return val - NTP_DELTA
@ -31,5 +33,4 @@ def settime():
import machine import machine
import utime import utime
tm = utime.localtime(t) tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,) machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
machine.RTC().datetime(tm)