2020class WifiService ():
2121
2222 wifi_busy = False # crude lock on wifi
23+ access_points = {}
2324
2425 @staticmethod
2526 def connect ():
27+ wlan = network .WLAN (network .STA_IF )
28+ wlan .active (False ) # restart WiFi hardware in case it's in a bad state
29+ wlan .active (True )
2630 networks = wlan .scan ()
2731 for n in networks :
2832 ssid = n [0 ].decode ()
2933 print (f"auto_connect: checking ssid '{ ssid } '" )
30- if ssid in access_points :
31- password = access_points .get (ssid ).get ("password" )
34+ if ssid in WifiService . access_points :
35+ password = WifiService . access_points .get (ssid ).get ("password" )
3236 print (f"auto_connect: attempting to connect to saved network { ssid } with password { password } " )
33- if attempt_connecting (ssid ,password ):
37+ if WifiService . attempt_connecting (ssid ,password ):
3438 print (f"auto_connect: Connected to { ssid } " )
3539 return True
3640 else :
@@ -44,6 +48,7 @@ def connect():
4448 def attempt_connecting (ssid ,password ):
4549 print (f"auto_connect.py attempt_connecting: Attempting to connect to SSID: { ssid } " )
4650 try :
51+ wlan = network .WLAN (network .STA_IF )
4752 wlan .connect (ssid ,password )
4853 for i in range (10 ):
4954 if wlan .isconnected ():
@@ -66,8 +71,8 @@ def auto_connect():
6671 print ("auto_connect thread running" )
6772
6873 # load config:
69- access_points = mpos .config .SharedPreferences ("com.micropythonos.system.wifiservice" ).get_dict ("access_points" )
70- if not len (access_points ):
74+ WifiService . access_points = mpos .config .SharedPreferences ("com.micropythonos.system.wifiservice" ).get_dict ("access_points" )
75+ if not len (WifiService . access_points ):
7176 print ("WifiService.py: not access points configured, exiting..." )
7277 return
7378
@@ -78,13 +83,11 @@ def auto_connect():
7883 time .sleep (10 )
7984 print ("auto_connect: wifi connect simulation done" )
8085 else :
81- wlan = network .WLAN (network .STA_IF )
82- wlan .active (False ) # restart WiFi hardware in case it's in a bad state
83- wlan .active (True )
84- if connect ():
86+ if WifiService .connect ():
8587 print ("WifiService.py managed to connect." )
8688 else :
8789 print ("WifiService.py did not manage to connect." )
90+ wlan = network .WLAN (network .STA_IF )
8891 wlan .active (False ) # disable to conserve power
8992 WifiService .wifi_busy = False
9093
0 commit comments