2020class WifiService ():
2121
2222wifi_busy = False # crude lock on wifi
23+ access_points = {}
2324
2425@staticmethod
2526def 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 )
2630networks = wlan .scan ()
2731for n in networks :
2832ssid = n [0 ].decode ()
2933print (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" )
3236print (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 ):
3438print (f"auto_connect: Connected to{ ssid } " )
3539return True
3640else :
@@ -44,6 +48,7 @@ def connect():
4448def attempt_connecting (ssid ,password ):
4549print (f"auto_connect.py attempt_connecting: Attempting to connect to SSID:{ ssid } " )
4650try :
51+ wlan = network .WLAN (network .STA_IF )
4752wlan .connect (ssid ,password )
4853for i in range (10 ):
4954if wlan .isconnected ():
@@ -66,8 +71,8 @@ def auto_connect():
6671print ("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 ):
7176print ("WifiService.py: not access points configured, exiting..." )
7277return
7378
@@ -78,13 +83,11 @@ def auto_connect():
7883time .sleep (10 )
7984print ("auto_connect: wifi connect simulation done" )
8085else :
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 ():
8587print ("WifiService.py managed to connect." )
8688else :
8789print ("WifiService.py did not manage to connect." )
90+ wlan = network .WLAN (network .STA_IF )
8891wlan .active (False )# disable to conserve power
8992WifiService .wifi_busy = False
9093