Using local time with Pico W

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Using local time with Pico W

Post by pythoncoder » Thu Jul 28, 2022 8:41 am

Very useful. I'd missed that.
Peter Hinch
Index to my micropython libraries.

Elmidea
Posts: 4
Joined: Sat Jul 30, 2022 4:03 pm

Re: Using local time with Pico W

Post by Elmidea » Tue Aug 02, 2022 9:31 am

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

Elmidea
Posts: 4
Joined: Sat Jul 30, 2022 4:03 pm

Re: Using local time with Pico W

Post by Elmidea » Tue Aug 02, 2022 4:35 pm

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

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: Using local time with Pico W

Post by p_j » Wed Aug 03, 2022 12:29 am

Elmidea wrote:
Tue Aug 02, 2022 4:35 pm
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
Can you post your code?

Elmidea
Posts: 4
Joined: Sat Jul 30, 2022 4:03 pm

Re: Using local time with Pico W

Post by Elmidea » Wed Aug 03, 2022 7:38 am

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 :

Code: Select all

time.localtime(ntptime.time() + 946684800)
(2022, 7, 16, 17, 25, 50, 5, 197)
ntptime.py :

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))

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: Using local time with Pico W

Post by p_j » Wed Aug 03, 2022 11:18 am

Elmidea wrote:
Wed Aug 03, 2022 7:38 am
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 :

Code: Select all

time.localtime(ntptime.time() + 946684800)
(2022, 7, 16, 17, 25, 50, 5, 197)
ntptime.py :

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?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Using local time with Pico W

Post by pythoncoder » Fri Aug 05, 2022 11:03 am

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.

User avatar
Hasenradball
Posts: 15
Joined: Tue May 21, 2019 12:52 pm
Location: Germany
Contact:

Re: Using local time with Pico W

Post by Hasenradball » Tue Mar 28, 2023 7:45 am

Hi Together,

I would remark that this description in the Doc is a little bit missleading.
2023-03-28_09h36_04.png
2023-03-28_09h36_04.png (12.43 KiB) Viewed 22925 times
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

Post Reply