You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Fix WifiService
This commit is contained in:
@@ -20,17 +20,21 @@ except Exception as e:
|
||||
class WifiService():
|
||||
|
||||
wifi_busy = False # crude lock on wifi
|
||||
access_points = {}
|
||||
|
||||
@staticmethod
|
||||
def connect():
|
||||
wlan=network.WLAN(network.STA_IF)
|
||||
wlan.active(False) # restart WiFi hardware in case it's in a bad state
|
||||
wlan.active(True)
|
||||
networks = wlan.scan()
|
||||
for n in networks:
|
||||
ssid = n[0].decode()
|
||||
print(f"auto_connect: checking ssid '{ssid}'")
|
||||
if ssid in access_points:
|
||||
password = access_points.get(ssid).get("password")
|
||||
if ssid in WifiService.access_points:
|
||||
password = WifiService.access_points.get(ssid).get("password")
|
||||
print(f"auto_connect: attempting to connect to saved network {ssid} with password {password}")
|
||||
if attempt_connecting(ssid,password):
|
||||
if WifiService.attempt_connecting(ssid,password):
|
||||
print(f"auto_connect: Connected to {ssid}")
|
||||
return True
|
||||
else:
|
||||
@@ -44,6 +48,7 @@ class WifiService():
|
||||
def attempt_connecting(ssid,password):
|
||||
print(f"auto_connect.py attempt_connecting: Attempting to connect to SSID: {ssid}")
|
||||
try:
|
||||
wlan=network.WLAN(network.STA_IF)
|
||||
wlan.connect(ssid,password)
|
||||
for i in range(10):
|
||||
if wlan.isconnected():
|
||||
@@ -66,8 +71,8 @@ class WifiService():
|
||||
print("auto_connect thread running")
|
||||
|
||||
# load config:
|
||||
access_points = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").get_dict("access_points")
|
||||
if not len(access_points):
|
||||
WifiService.access_points = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").get_dict("access_points")
|
||||
if not len(WifiService.access_points):
|
||||
print("WifiService.py: not access points configured, exiting...")
|
||||
return
|
||||
|
||||
@@ -78,13 +83,11 @@ class WifiService():
|
||||
time.sleep(10)
|
||||
print("auto_connect: wifi connect simulation done")
|
||||
else:
|
||||
wlan=network.WLAN(network.STA_IF)
|
||||
wlan.active(False) # restart WiFi hardware in case it's in a bad state
|
||||
wlan.active(True)
|
||||
if connect():
|
||||
if WifiService.connect():
|
||||
print("WifiService.py managed to connect.")
|
||||
else:
|
||||
print("WifiService.py did not manage to connect.")
|
||||
wlan=network.WLAN(network.STA_IF)
|
||||
wlan.active(False) # disable to conserve power
|
||||
WifiService.wifi_busy = False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user