Using local time with Pico W
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Using local time with Pico W
Very useful. I'd missed that.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Using local time with Pico W
Hi,
Should I change anything to the micropython-samples/ntptime/ntptime.py code now that the datetime with timezone support was added to the micropython-lib ?
Thank you
Should I change anything to the micropython-samples/ntptime/ntptime.py code now that the datetime with timezone support was added to the micropython-lib ?
Thank you
Re: Using local time with Pico W
I 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
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
Re: Using local time with Pico W
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
Re: Using local time with Pico W
Yes 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 :
main.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))
Re: Using local time with Pico W
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))
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Using local time with Pico W
The ntptime function has a short timeout period of one second (for obvious reasons). Sometimes timeouts occur and the function returns a ridiculous result. See my solution which fixes this and other problems. My version returns 0 on error and the caller should check for this.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
- Hasenradball
- Posts: 15
- Joined: Tue May 21, 2019 12:52 pm
- Location: Germany
- Contact:
Re: Using local time with Pico W
Hi Together,
I would remark that this description in the Doc is a little bit missleading. Because on the Pico W the RTC works with the epoch since 1970.
Maybe a small redesign of this description would be perfect, my opinion.
A other point is that I want to aks what the actual status is about:
time.gmtime()
time.localtime()
Is there actually a Timezone information available in micropython?
If not Iam right that these two functions leads to the same result in time structure?
Thanks
Frank
I would remark that this description in the Doc is a little bit missleading. Because on the Pico W the RTC works with the epoch since 1970.
Maybe a small redesign of this description would be perfect, my opinion.
A other point is that I want to aks what the actual status is about:
time.gmtime()
time.localtime()
Is there actually a Timezone information available in micropython?
If not Iam right that these two functions leads to the same result in time structure?
Thanks
Frank