From 4e6c31537ca9f9f0b3223c7d3aec2eb45a4b549b Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Fri, 6 Jun 2025 21:58:40 +0200 Subject: [PATCH] sync time after successful wifi connection --- .../apps/com.micropythonos.wifi/assets/wifi.py | 3 +++ .../builtin/system/WifiService.py | 16 +++------------- internal_filesystem/lib/mpos/time.py | 13 ++++++++++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py b/internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py index 0301b019..a1821743 100644 --- a/internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py +++ b/internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py @@ -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 diff --git a/internal_filesystem/builtin/system/WifiService.py b/internal_filesystem/builtin/system/WifiService.py index 862969b1..035c229c 100644 --- a/internal_filesystem/builtin/system/WifiService.py +++ b/internal_filesystem/builtin/system/WifiService.py @@ -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...") diff --git a/internal_filesystem/lib/mpos/time.py b/internal_filesystem/lib/mpos/time.py index 72b354d9..24a7c016 100644 --- a/internal_filesystem/lib/mpos/time.py +++ b/internal_filesystem/lib/mpos/time.py @@ -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)