|
1 | 1 | # 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. |
9 | 5 |
|
10 | 6 | import ujson |
11 | 7 | import os |
12 | 8 | 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 |
43 | 10 |
|
44 | 11 | def auto_connect(): |
45 | 12 | networks = wlan.scan() |
46 | 13 | for n in networks: |
47 | 14 | ssid = n[0].decode() |
48 | 15 | print(f"auto_connect: checking ssid '{ssid}'") |
49 | 16 | if ssid in access_points: |
50 | | - password = access_points.get(ssid) |
| 17 | + password = access_points.get(ssid).get("password") |
51 | 18 | print(f"auto_connect: attempting to connect to saved network {ssid} with password {password}") |
52 | 19 | if attempt_connecting(ssid,password): |
53 | 20 | print(f"auto_connect: Connected to {ssid}") |
@@ -92,19 +59,28 @@ def attempt_connecting(ssid,password): |
92 | 59 | return False |
93 | 60 |
|
94 | 61 |
|
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") |
97 | 73 |
|
98 | 74 | if not have_network: |
99 | | - print("auto_connect.py: no network module found, exiting...") |
| 75 | + print("WifiService.py: no network module found, exiting...") |
100 | 76 | elif len(access_points): |
101 | 77 | wlan=network.WLAN(network.STA_IF) |
102 | 78 | wlan.active(False) # restart WiFi hardware in case it's in a bad state |
103 | 79 | wlan.active(True) |
104 | 80 | if auto_connect(): |
105 | | - print("auto_connect.py managed to connect.") |
| 81 | + print("WifiService.py managed to connect.") |
106 | 82 | else: |
107 | | - print("auto_connect.py did not manage to connect.") |
| 83 | + print("WifiService.py did not manage to connect.") |
108 | 84 | wlan.active(False) # disable to conserve power |
109 | 85 | else: |
110 | | - print("auto_connect.py: not access points configured, exiting...") |
| 86 | + print("WifiService.py: not access points configured, exiting...") |
0 commit comments