Re: Using local time with Pico W
Posted: Thu Jul 28, 2022 8:41 am
Very useful. I'd missed that.
Please see the new forum at
https://forum.micropython.org/
Can you post your code?Elmidea wrote: ↑Tue Aug 02, 2022 4:35 pmI think I have found a way to make it work, but I keep getting this error (not every time):
OverflowError: overflow converting long int to machine word
I'm stuck on that, even if I try to reduce the length it doesnt seem to change the issue
Whenever I dont get overflow I get this error:
OSError: [Errno 110] ETIMEDOUT
Which indicates the ntp server doesnt respond in time right? I tried to change the wait from 1 to 10, same thing, and also other npr server adresses, same thing again...
It only works like 1 time out of 10 for some reason, no change in the code, just soft reset of the pico W.
Thanks for your help
Code: Select all
time.localtime(ntptime.time() + 946684800)
(2022, 7, 16, 17, 25, 50, 5, 197)
Code: Select all
try:
import usocket as socket
except:
import socket
try:
import ustruct as struct
except:
import struct
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 946684800
# The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org'
host = "0.fr.pool.ntp.org"
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
# There's currently no timezone support in MicroPython, and the RTC is set in UTC time.
def settime():
t = time()
import machine
import utime
tm = utime.gmtime(t)
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
Strange, what is the error?Elmidea wrote: ↑Wed Aug 03, 2022 7:38 amYes here it is, it works like 1 time out of 20 this morning... no idea why, wifi seems solid, wlan status is ok:
main.py :
ntptime.py :Code: Select all
time.localtime(ntptime.time() + 946684800) (2022, 7, 16, 17, 25, 50, 5, 197)
Code: Select all
try: import usocket as socket except: import socket try: import ustruct as struct except: import struct # (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60 NTP_DELTA = 946684800 # The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org' host = "0.fr.pool.ntp.org" 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 # There's currently no timezone support in MicroPython, and the RTC is set in UTC time. def settime(): t = time() import machine import utime tm = utime.gmtime(t) machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))