sync time after successful wifi connection

This commit is contained in:
Thomas Farstrike
2025-06-06 21:58:40 +02:00
parent cec93bbf86
commit 4e6c31537c
3 changed files with 18 additions and 14 deletions
@@ -199,6 +199,9 @@ class WiFi(Activity):
print(f"Connecting to {ssid} got result: {result}")
last_tried_ssid = ssid
last_tried_result = result
# also do a time sync, otherwise some apps (Nostr Wallet Connect) won't work:
if have_network and wlan.isconnected():
mpos.time.sync_time()
self.busy_connecting=False
if self.keep_running:
# Schedule UI updates because different thread
@@ -6,7 +6,9 @@
import ujson
import os
import time
import mpos.config
import mpos.time
def auto_connect():
networks = wlan.scan()
@@ -26,18 +28,6 @@ def auto_connect():
print("auto_connect: no known networks connected")
return False
def sync_time():
import ntptime
print("Synchronizing clock...")
# Set the NTP server and sync time
ntptime.host = 'pool.ntp.org' # Set NTP server
try:
print('Syncing time with', ntptime.host)
ntptime.settime() # Fetch and set time (in UTC)
print('Time synced successfully')
except Exception as e:
print('Failed to sync time:', e)
def attempt_connecting(ssid,password):
print(f"auto_connect.py attempt_connecting: Attempting to connect to SSID: {ssid}")
try:
@@ -45,7 +35,7 @@ def attempt_connecting(ssid,password):
for i in range(10):
if wlan.isconnected():
print(f"auto_connect.py attempt_connecting: Connected to {ssid} after {i+1} seconds")
sync_time()
mpos.time.sync_time()
return True
elif not wlan.active(): # wificonf app or others might stop the wifi, no point in continuing then
print("auto_connect.py attempt_connecting: Someone disabled wifi, bailing out...")
+12 -1
View File
@@ -7,4 +7,15 @@ def epoch_seconds():
return time.time() + 946684800
else:
return round(time.time())
def sync_time():
import ntptime
print("Synchronizing clock...")
# Set the NTP server and sync time
ntptime.host = 'pool.ntp.org' # Set NTP server
try:
print('Syncing time with', ntptime.host)
ntptime.settime() # Fetch and set time (in UTC)
print('Time synced successfully')
except Exception as e:
print('Failed to sync time:', e)