Updates for reliable NTP

This commit is contained in:
iosullivan committed 2026-07-19 08:13:05 +08:00
1 parent 536b076f39
commit e11aa92692
1 file changed
+9 -9
+9 -9
View File
@@ -15,16 +15,10 @@ def time_from_iso(datetime_str):
time_u = mktime((yyyy, mm, dd, h, m, s, 0, 0))
return time_u
def connect_wifi():
# Create network in station mode, activate it and connect
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASS)
def reconnect_wifi():
while not wlan.isconnected():
try:
wlan.connect(ssid, password)
wlan.connect(SSID, PASS)
max_wait = 10
while max_wait > 0:
if wlan.isconnected():
@@ -47,7 +41,11 @@ def init_LEDs():
led.off()
# Connect Wifi
connect_wifi()
# Create network in station mode, activate it and connect
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
reconnect_wifi()
# Synchronize time of RTC with NTP
last_ntp_sync = -3700 # Init as more than an hour ago
@@ -209,6 +207,7 @@ init_LEDs()
while(True):
# Synchronise NTP time if hasn't been done in 1 hour
if (time() - last_ntp_sync) > 3600:
reconnect_wifi()
try:
ntptime.settime()
print("Time synchronized successfully")
@@ -218,6 +217,7 @@ while(True):
# Have we queried API in the query_period seconds?
if (time() - last_api_query) > query_period:
reconnect_wifi()
# Query updated API
trains = (requests.get("https://lapi.transitchicago.com/api/1.0/ttpositions.aspx?key="+APIKEY+"&rt=Blue&outputType=JSON")).json()['ctatt']