Last active
March 12, 2025 10:42
-
-
Save aallan/581ecf4dc92cd53e3a415b7c33a1147c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import network | |
import socket | |
import time | |
import struct | |
from machine import Pin | |
NTP_DELTA = 2208988800 | |
host = "pool.ntp.org" | |
led = Pin("LED", Pin.OUT) | |
ssid = 'A NETWORK' | |
password = 'A PASSWORD' | |
def set_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] | |
t = val - NTP_DELTA | |
tm = time.gmtime(t) | |
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0)) | |
wlan = network.WLAN(network.STA_IF) | |
wlan.active(True) | |
wlan.connect(ssid, password) | |
max_wait = 10 | |
while max_wait > 0: | |
if wlan.status() < 0 or wlan.status() >= 3: | |
break | |
max_wait -= 1 | |
print('waiting for connection...') | |
time.sleep(1) | |
if wlan.status() != 3: | |
raise RuntimeError('network connection failed') | |
else: | |
print('connected') | |
status = wlan.ifconfig() | |
print( 'ip = ' + status[0] ) | |
led.on() | |
set_time() | |
print(time.localtime()) | |
led.off() |
@tonygo . Did you add this try: ___ except: block into your code?
(From my post/reaction: Jul 18, 2022)
Thank you. I will try that. I've written some new code of my own now that appears to be reliable.
Tony Goodhew
…________________________________
From: Paulus H.J. Schulinck ***@***.***>
Sent: Tuesday, March 11, 2025 4:51 PM
To: PaulskPt ***@***.***>
Cc: Mention ***@***.***>
Subject: Re: aallan/picow_ntp_client.py
@PaulskPt commented on this gist.
________________________________
@tonygo<https://github.com/tonygo> . Did you add this try: ___ except: block into your code?
image.png (view on web)<https://gist.github.com/user-attachments/assets/23f81f6c-3879-4983-b587-f71d0652619e>
(From my post/reaction: Jul 18, 2022)
—
Reply to this email directly, view it on GitHub<https://gist.github.com/aallan/581ecf4dc92cd53e3a415b7c33a1147c#gistcomment-5486680> or unsubscribe<https://github.com/notifications/unsubscribe-auth/AA3K7VPRM3ENXY4BQYW3H232T5ZGPBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNZTGA2TENZVU52HE2LHM5SXFJTDOJSWC5DF>.
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for sharing this code.
I've been hitting a error if I I rerun the script. I cannot see why.
I've added a little at the end but do not see why that should cause the upset. I've updated to the current latest uf2s and run on several different boards.
Extra code:
`
print(" ")
year,mon,day,h,m,s,weekday,x = time.localtime()
now = str(h)+":"+str(m)+":"+str(s)
date = str(year)+" "+months[mon]+" "+str(year)
print(now)
print(date)
print(wd[weekday])
`
ERRORs below
`
MPY: soft reboot
connected
ip = 192.168.0.79
(2025, 3, 8, 13, 40, 38, 5, 67)
13:40:38
2025 Apr 2025
Sat
MPY: soft reboot
connected
ip = 192.168.0.79
Traceback (most recent call last):
File "", line 53, in
File "", line 25, in set_time
OSError: [Errno 110] ETIMEDOUT
`
I tried this change: s.settimeout(5) but it did not help.