forked from vial-microserver/vial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.py
59 lines (55 loc) · 1.8 KB
/
boot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gc
import webrepl
import network
import ujson as json
import uos as os
webrepl.start()
ap_if = network.WLAN(network.AP_IF)
ap_setup_file = "accesspoint.json"
if ap_setup_file in os.listdir():
# Start the access point.
if not ap_if.active():
ap_if.active(True)
with open(ap_setup_file, "r") as f:
access_point = json.load(f)
ap_if.config(
essid=access_point["essid"],
channel=access_point["channel"],
password=access_point["password"])
else:
if ap_if.active():
ap_if.active(False)
sta_if = network.WLAN(network.STA_IF)
sta_setup_file = "networks.json"
if sta_setup_file in os.listdir():
with open(sta_setup_file, "r") as f:
network_data = json.load(f)
if not sta_if.active():
sta_if.active(True)
available_networks = sta_if.scan()
for network in available_networks:
network_name = network[0].decode()
if not sta_if.isconnected():
for network in network_data:
if network_name == network["essid"]:
try:
network_config = network["static_ip"]
network_config = tuple(network_config)
except KeyError:
pass
else:
sta_if.ifconfig(network_config)
sta_if.connect(network_name, network["password"])
else:
break
identity_file = "identity.json"
if identity_file in os.listdir():
with open(identity_file, "r") as f:
identity = json.load(f)
location = identity.get("location")
if location is not None:
sta_if.config(dhcp_hostname=location.lower().replace(" ","_"))
else:
if sta_if.active():
sta_if.active(True)
gc.collect()