Skip to content

Commit f770250

Browse files
Fix WifiService
1 parent 23355a1 commit f770250

File tree

2 files changed

+23
-46
lines changed

2 files changed

+23
-46
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
last_tried_ssid = ""
1414
last_tried_result = ""
1515

16+
# This is basically the wifi settings app
1617
class WiFi(Activity):
1718

1819
scan_button_scan_text = "Rescan"
@@ -63,7 +64,7 @@ def onStart(self, screen):
6364

6465
def onResume(self, screen):
6566
global access_points
66-
access_points = mpos.config.SharedPreferences("com.micropythonos.wifi").get_dict("access_points")
67+
access_points = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").get_dict("access_points")
6768
self.keep_running = True
6869
if len(self.ssids) == 0:
6970
self.start_scan_networks()
@@ -308,7 +309,7 @@ def connect_cb(self, event):
308309
print(f"connect_cb: Got password: {password}")
309310
self.setPassword(self.selected_ssid, password)
310311
print(f"connect_cb: Updated access_points: {access_points}")
311-
editor = mpos.config.SharedPreferences("com.micropythonos.wifi").edit()
312+
editor = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").edit()
312313
editor.put_dict("access_points", access_points)
313314
editor.commit()
314315
self.setResult(True, {"ssid": self.selected_ssid, "password": password})
Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
11
# Automatically connect to the WiFi, based on the saved networks
2-
3-
have_network=True
4-
try:
5-
import network
6-
except Exception as e:
7-
have_network=False
8-
print("Could not import network, have_network=False")
2+
# Manage concurrent accesses to the wifi (scan while connect, connect while scan etc)
3+
# Manage saved networks
4+
# This gets started in a new thread, does an autoconnect, and exits.
95

106
import ujson
117
import os
128
import time
13-
14-
15-
access_points={}
16-
17-
18-
def load_config():
19-
print("load_config: Checking for /data directory")
20-
try:
21-
os.stat('data')
22-
print("load_config: /data exists")
23-
except OSError:
24-
print("load_config: Creating /data directory")
25-
os.mkdir('data')
26-
print("load_config: Checking for /data/com.example.wificonf directory")
27-
try:
28-
os.stat('data/com.example.wificonf')
29-
print("load_config: /data/com.example.wificonf exists")
30-
except OSError:
31-
print("load_config: Creating /data/com.example.wificonf directory")
32-
os.mkdir('data/com.example.wificonf')
33-
print("load_config: Loading config from conf.json")
34-
try:
35-
with open('data/com.example.wificonf/conf.json','r') as f:
36-
global access_points
37-
access_points=ujson.load(f)
38-
print(f"load_config: Loaded access_points: {access_points}")
39-
except OSError:
40-
access_points={}
41-
print("load_config: No config file found, using empty access_points")
42-
9+
import mpos.config
4310

4411
def auto_connect():
4512
networks = wlan.scan()
4613
for n in networks:
4714
ssid = n[0].decode()
4815
print(f"auto_connect: checking ssid '{ssid}'")
4916
if ssid in access_points:
50-
password = access_points.get(ssid)
17+
password = access_points.get(ssid).get("password")
5118
print(f"auto_connect: attempting to connect to saved network {ssid} with password {password}")
5219
if attempt_connecting(ssid,password):
5320
print(f"auto_connect: Connected to {ssid}")
@@ -92,19 +59,28 @@ def attempt_connecting(ssid,password):
9259
return False
9360

9461

95-
print("auto_connect.py running")
96-
load_config()
62+
print("WifiService.py running")
63+
64+
have_network=True
65+
try:
66+
import network
67+
except Exception as e:
68+
have_network=False
69+
print("Could not import network, have_network=False")
70+
71+
# load config:
72+
access_points = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").get_dict("access_points")
9773

9874
if not have_network:
99-
print("auto_connect.py: no network module found, exiting...")
75+
print("WifiService.py: no network module found, exiting...")
10076
elif len(access_points):
10177
wlan=network.WLAN(network.STA_IF)
10278
wlan.active(False) # restart WiFi hardware in case it's in a bad state
10379
wlan.active(True)
10480
if auto_connect():
105-
print("auto_connect.py managed to connect.")
81+
print("WifiService.py managed to connect.")
10682
else:
107-
print("auto_connect.py did not manage to connect.")
83+
print("WifiService.py did not manage to connect.")
10884
wlan.active(False) # disable to conserve power
10985
else:
110-
print("auto_connect.py: not access points configured, exiting...")
86+
print("WifiService.py: not access points configured, exiting...")

0 commit comments

Comments
 (0)