11appscreen = lv .screen_active ()
22
3- import network
3+ havenetwork = True
4+ try :
5+ import network
6+ except Exception as e :
7+ havenetwork = False
8+
49import ujson
510import os
611import time
2934def load_config ():
3035 print ("load_config: Checking for /data directory" )
3136 try :
32- os .stat ('/ data' )
37+ os .stat ('data' )
3338 print ("load_config: /data exists" )
3439 except OSError :
3540 print ("load_config: Creating /data directory" )
36- os .mkdir ('/ data' )
41+ os .mkdir ('data' )
3742 print ("load_config: Checking for /data/com.example.wificonf directory" )
3843 try :
39- os .stat ('/ data/com.example.wificonf' )
44+ os .stat ('data/com.example.wificonf' )
4045 print ("load_config: /data/com.example.wificonf exists" )
4146 except OSError :
4247 print ("load_config: Creating /data/com.example.wificonf directory" )
43- os .mkdir ('/ data/com.example.wificonf' )
48+ os .mkdir ('data/com.example.wificonf' )
4449 print ("load_config: Loading config from conf.json" )
4550 try :
46- with open ('/ data/com.example.wificonf/conf.json' ,'r' ) as f :
51+ with open ('data/com.example.wificonf/conf.json' ,'r' ) as f :
4752 global access_points
4853 access_points = ujson .load (f )
4954 print (f"load_config: Loaded access_points: { access_points } " )
@@ -54,7 +59,7 @@ def load_config():
5459def save_config ():
5560 print ("save_config: Saving access_points to conf.json" )
5661 try :
57- with open ('/ data/com.example.wificonf/conf.json' ,'w' ) as f :
62+ with open ('data/com.example.wificonf/conf.json' ,'w' ) as f :
5863 ujson .dump (access_points ,f )
5964 print (f"save_config: Saved access_points: { access_points } " )
6065 except OSError :
@@ -74,12 +79,15 @@ def scan_done_callback():
7479def scan_networks ():
7580 print ("scan_networks: Scanning for Wi-Fi networks" )
7681 global ssids
77- if not wlan .isconnected (): # restart WiFi hardware in case it's in a bad state
82+ if havenetwork and not wlan .isconnected (): # restart WiFi hardware in case it's in a bad state
7883 wlan .active (False )
7984 wlan .active (True )
8085 try :
81- networks = wlan .scan ()
82- ssids = list (set (n [0 ].decode () for n in networks ))
86+ if havenetwork :
87+ networks = wlan .scan ()
88+ ssids = list (set (n [0 ].decode () for n in networks ))
89+ else :
90+ ssids = ["Dummy" , "Test" , "SSIDs" ]
8391 print (f"scan_networks: Found networks: { ssids } " )
8492 except Exception as e :
8593 print (f"scan_networks: Scan failed: { e } " )
@@ -113,16 +121,19 @@ def attempt_connecting(ssid,password):
113121 print (f"attempt_connecting: Attempting to connect to SSID: { ssid } " )
114122 result = "connected"
115123 try :
116- wlan .disconnect ()
117- wlan .connect (ssid ,password )
118- for i in range (10 ):
119- if wlan .isconnected ():
120- print (f"attempt_connecting: Connected to { ssid } after { i + 1 } seconds" )
121- break
122- print (f"attempt_connecting: Waiting for connection, attempt { i + 1 } /10" )
123- time .sleep (1 )
124- if not wlan .isconnected ():
125- result = "timeout"
124+ if havenetwork :
125+ wlan .disconnect ()
126+ wlan .connect (ssid ,password )
127+ for i in range (10 ):
128+ if wlan .isconnected ():
129+ print (f"attempt_connecting: Connected to { ssid } after { i + 1 } seconds" )
130+ break
131+ print (f"attempt_connecting: Waiting for connection, attempt { i + 1 } /10" )
132+ time .sleep (1 )
133+ if not wlan .isconnected ():
134+ result = "timeout"
135+ else :
136+ print ("Warning: not trying to connect because not havenetwork" )
126137 except Exception as e :
127138 print (f"attempt_connecting: Connection error: { e } " )
128139 result = f"{ e } "
@@ -161,7 +172,7 @@ def refresh_list(tried_ssid="", result=""):
161172 print (f"refresh_list: Adding SSID: { ssid } " )
162173 button = aplist .add_button (None ,ssid )
163174 button .add_event_cb (lambda e , s = ssid : select_ssid_cb (e ,s ),lv .EVENT .CLICKED ,None )
164- if wlan .isconnected () and wlan .config ('essid' )== ssid :
175+ if havenetwork and wlan .isconnected () and wlan .config ('essid' )== ssid :
165176 status = "connected"
166177 elif tried_ssid == ssid : # implies not connected
167178 status = result
@@ -314,13 +325,11 @@ def create_ui():
314325 refresh_list ()
315326
316327
317- wlan = network .WLAN (network .STA_IF )
318- wlan .active (True )
328+ if havenetwork :
329+ wlan = network .WLAN (network .STA_IF )
330+ wlan .active (True )
319331
320332load_config ()
321333create_ui ()
322334start_scan_networks ()
323335
324- #import time
325- #while appscreen == lv.screen_active() or password_page == lv.screen_active():
326- # time.sleep_ms(100) # not too long, otherwise touch events get lost
0 commit comments