Skip to content

Commit 4e6c315

Browse files
sync time after successful wifi connection
1 parent cec93bb commit 4e6c315

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def attempt_connecting_thread(self, ssid, password):
199199
print(f"Connecting to {ssid} got result: {result}")
200200
last_tried_ssid = ssid
201201
last_tried_result = result
202+
# also do a time sync, otherwise some apps (Nostr Wallet Connect) won't work:
203+
if have_network and wlan.isconnected():
204+
mpos.time.sync_time()
202205
self.busy_connecting=False
203206
if self.keep_running:
204207
# Schedule UI updates because different thread

internal_filesystem/builtin/system/WifiService.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import ujson
77
import os
88
import time
9+
910
import mpos.config
11+
import mpos.time
1012

1113
def auto_connect():
1214
networks = wlan.scan()
@@ -26,26 +28,14 @@ def auto_connect():
2628
print("auto_connect: no known networks connected")
2729
return False
2830

29-
def sync_time():
30-
import ntptime
31-
print("Synchronizing clock...")
32-
# Set the NTP server and sync time
33-
ntptime.host = 'pool.ntp.org' # Set NTP server
34-
try:
35-
print('Syncing time with', ntptime.host)
36-
ntptime.settime() # Fetch and set time (in UTC)
37-
print('Time synced successfully')
38-
except Exception as e:
39-
print('Failed to sync time:', e)
40-
4131
def attempt_connecting(ssid,password):
4232
print(f"auto_connect.py attempt_connecting: Attempting to connect to SSID: {ssid}")
4333
try:
4434
wlan.connect(ssid,password)
4535
for i in range(10):
4636
if wlan.isconnected():
4737
print(f"auto_connect.py attempt_connecting: Connected to {ssid} after {i+1} seconds")
48-
sync_time()
38+
mpos.time.sync_time()
4939
return True
5040
elif not wlan.active(): # wificonf app or others might stop the wifi, no point in continuing then
5141
print("auto_connect.py attempt_connecting: Someone disabled wifi, bailing out...")

internal_filesystem/lib/mpos/time.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,15 @@ def epoch_seconds():
77
return time.time() + 946684800
88
else:
99
return round(time.time())
10-
10+
11+
def sync_time():
12+
import ntptime
13+
print("Synchronizing clock...")
14+
# Set the NTP server and sync time
15+
ntptime.host = 'pool.ntp.org' # Set NTP server
16+
try:
17+
print('Syncing time with', ntptime.host)
18+
ntptime.settime() # Fetch and set time (in UTC)
19+
print('Time synced successfully')
20+
except Exception as e:
21+
print('Failed to sync time:', e)

0 commit comments

Comments
 (0)