Last active
March 29, 2024 16:08
-
-
Save aallan/581ecf4dc92cd53e3a415b7c33a1147c to your computer and use it in GitHub Desktop.
This file contains 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This fixed it for me. Thank you! I had to do "+ 21_600" to get to North America Central Time. Thank you again!