|
1 | 1 | # Automatically connect to the WiFi, based on the saved networks |
2 | 2 |
|
3 | | -import network |
| 3 | +have_network=True |
| 4 | +try: |
| 5 | + import network |
| 6 | +except Exception as e: |
| 7 | + have_network=False |
| 8 | + print("auto_connect.py could not import network, have_network=False") |
| 9 | + |
4 | 10 | import ujson |
5 | 11 | import os |
6 | 12 | import time |
|
12 | 18 | def load_config(): |
13 | 19 | print("load_config: Checking for /data directory") |
14 | 20 | try: |
15 | | - os.stat('/data') |
| 21 | + os.stat('data') |
16 | 22 | print("load_config: /data exists") |
17 | 23 | except OSError: |
18 | 24 | print("load_config: Creating /data directory") |
19 | | - os.mkdir('/data') |
| 25 | + os.mkdir('data') |
20 | 26 | print("load_config: Checking for /data/com.example.wificonf directory") |
21 | 27 | try: |
22 | | - os.stat('/data/com.example.wificonf') |
| 28 | + os.stat('data/com.example.wificonf') |
23 | 29 | print("load_config: /data/com.example.wificonf exists") |
24 | 30 | except OSError: |
25 | 31 | print("load_config: Creating /data/com.example.wificonf directory") |
26 | | - os.mkdir('/data/com.example.wificonf') |
| 32 | + os.mkdir('data/com.example.wificonf') |
27 | 33 | print("load_config: Loading config from conf.json") |
28 | 34 | try: |
29 | | - with open('/data/com.example.wificonf/conf.json','r') as f: |
| 35 | + with open('data/com.example.wificonf/conf.json','r') as f: |
30 | 36 | global access_points |
31 | 37 | access_points=ujson.load(f) |
32 | 38 | print(f"load_config: Loaded access_points: {access_points}") |
@@ -85,10 +91,13 @@ def attempt_connecting(ssid,password): |
85 | 91 | print(f"auto_connect.py attempt_connecting: Connection error: {e}") |
86 | 92 | return False |
87 | 93 |
|
88 | | -print("auto_connect.py running...") |
| 94 | + |
| 95 | +print("auto_connect.py running") |
89 | 96 | load_config() |
90 | 97 |
|
91 | | -if len(access_points): |
| 98 | +if not have_network: |
| 99 | + print("auto_connect.py: no network module found, exiting...") |
| 100 | +elif len(access_points): |
92 | 101 | wlan=network.WLAN(network.STA_IF) |
93 | 102 | wlan.active(False) # restart WiFi hardware in case it's in a bad state |
94 | 103 | wlan.active(True) |
|
0 commit comments