Skip to content

Instantly share code, notes, and snippets.

@aallan
Last active June 30, 2025 00:21
Show Gist options
  • Save aallan/581ecf4dc92cd53e3a415b7c33a1147c to your computer and use it in GitHub Desktop.
Save aallan/581ecf4dc92cd53e3a415b7c33a1147c to your computer and use it in GitHub Desktop.
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()
@PaulskPt
Copy link

Hi, using this script provoked the error "machine" is not defined (line 30)
I changed line 6 into: "import machine".
line 11 into led = machine.Pin("LED", machine.Pin.OUT).
Then the script ran OK.

@PaulskPt
Copy link

Modified defines for local time and local NTP host
NTP_DELTA = 2208988800 - 3600 # subtract 1 hour = 3600 seconds to get GMT + 1 for Portugal
host = "pt.pool.ntp.org" # using host from Portugal

@Duncan-Hill
Copy link

I'm getting an intermittent failure running the code :

connected
ip = 192.168.3.58
Traceback (most recent call last):
File "", line 52, in
File "", line 24, in set_time
OSError: [Errno 110] ETIMEDOUT

Error message indicates its a time out but not sure what's causing the problem.

@PaulskPt
Copy link

PaulskPt commented Jul 18, 2022

@Duncan-Hill

Hi Duncan,
I had that error also. That is why I created a try..except block. See the OSError part of it.

OSError_handling

I also created extra logic to call set_time() only at: a) start; b) once in an hour. For the rest every second a REPL print of some time.localtime() data. See the next screenshot at the change of an hour:

2022-07-18_15h59-16h00_ntp_client_output_in_PuTTY

Added variable tm_gmtoff, offset of GMT for Portugal:

gmtoff_offset_east_of_UTC_in_seconds

The original script does call set_time() just once. That is OK for an example as this is.
I have the experience that the RTC's of various microcontollers in time deviate from NTP time.
That is why I decided to call set_time() at intervals to synchronize the built-in RTC.

I am still working on the script to have it fit better to my ideas.

I have published my project on the micropython forum:
https://forum.micropython.org/viewtopic.php?f=21&t=12761&p=69363&hilit=ntp#p69363

Paulus

@BinTechLLC
Copy link

Modified defines for local time and local NTP host NTP_DELTA = 2208988800 - 3600 # subtract 1 hour = 3600 seconds to get GMT + 1 for Portugal host = "pt.pool.ntp.org" # using host from Portugal

This fixed it for me. Thank you! I had to do "+ 21_600" to get to North America Central Time. Thank you again!

@PaulskPt
Copy link

You're welcome

@P135928
Copy link

P135928 commented Oct 22, 2024

You guys have any idea on how to use this on an SSD1306 OLED screen? Nevermind figured it out!!

@tonygo
Copy link

tonygo commented Mar 8, 2025

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

%Run -c $EDITOR_CONTENT

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.

@PaulskPt
Copy link

@tonygo . Did you add this try: ___ except: block into your code?

image

(From my post/reaction: Jul 18, 2022)

@tonygo
Copy link

tonygo commented Mar 12, 2025 via email

@pie-man
Copy link

pie-man commented Jun 30, 2025

@tonygo . Did you add this try: ___ except: block into your code?

image

(From my post/reaction: Jul 18, 2022)

When I try this, I get an error on the line val=struct.unpack(...)
local variable referenced before assignment.
Presumably, msg is not set if the exception happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment