2018-01-12 09:23:12 +00:00
|
|
|
import ujson
|
|
|
|
import network
|
2018-01-10 21:14:30 +00:00
|
|
|
from hal import *
|
|
|
|
from time import sleep
|
|
|
|
|
2018-05-22 18:42:11 +00:00
|
|
|
#print("Press Ctrl-C to stop boot script...")
|
2018-01-10 21:14:30 +00:00
|
|
|
sleep(0.2)
|
|
|
|
|
2018-01-13 15:13:49 +00:00
|
|
|
# open and parse the config file
|
|
|
|
config = None
|
|
|
|
with open("config.json", "r") as config_file:
|
|
|
|
config = ujson.load(config_file)
|
2018-01-10 21:14:30 +00:00
|
|
|
|
2018-01-12 09:09:01 +00:00
|
|
|
# connect to WiFi
|
2018-01-10 21:14:30 +00:00
|
|
|
wlan = network.WLAN(network.STA_IF)
|
2018-01-12 09:09:01 +00:00
|
|
|
# activate the WiFi interface
|
2018-01-10 21:14:30 +00:00
|
|
|
wlan.active(True)
|
2018-01-12 09:09:01 +00:00
|
|
|
# if not already connected
|
2018-01-10 21:14:30 +00:00
|
|
|
if not wlan.isconnected():
|
2018-01-12 09:09:01 +00:00
|
|
|
# scan for WiFi networks
|
2018-01-10 21:14:30 +00:00
|
|
|
networks = wlan.scan()
|
2018-01-12 09:09:01 +00:00
|
|
|
# go trough all scanned WiFi networks
|
2018-01-10 21:14:30 +00:00
|
|
|
for network in networks:
|
2018-01-12 09:09:01 +00:00
|
|
|
# extract the networks SSID
|
2018-01-10 21:14:30 +00:00
|
|
|
ssid = network[0].decode("utf-8")
|
2018-01-12 09:09:01 +00:00
|
|
|
# check if the SSID is in the config file
|
|
|
|
if ssid in config["wifis"].keys():
|
2018-05-22 18:42:11 +00:00
|
|
|
#print("connecting to: " + ssid)
|
2018-01-12 09:09:01 +00:00
|
|
|
# start to connect to the pre-configured network
|
2018-01-12 09:23:12 +00:00
|
|
|
wlan.connect(ssid, config["wifis"][ssid])
|
2018-01-10 21:14:30 +00:00
|
|
|
break
|
|
|
|
|
2018-01-12 09:23:12 +00:00
|
|
|
# clean up
|
2018-01-10 21:14:30 +00:00
|
|
|
import gc
|
|
|
|
gc.collect()
|