10
0
Fork 0
sumorobot-firmware/boot.py

34 lines
871 B
Python
Raw Normal View History

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
print("Press Ctrl-C to stop boot script...")
sleep(0.2)
# read the config file
config = ujson.loads(open("config.json", "r").read())
2018-01-10 21:14:30 +00:00
# connect to WiFi
2018-01-10 21:14:30 +00:00
wlan = network.WLAN(network.STA_IF)
# activate the WiFi interface
2018-01-10 21:14:30 +00:00
wlan.active(True)
# if not already connected
2018-01-10 21:14:30 +00:00
if not wlan.isconnected():
# scan for WiFi networks
2018-01-10 21:14:30 +00:00
networks = wlan.scan()
# go trough all scanned WiFi networks
2018-01-10 21:14:30 +00:00
for network in networks:
# extract the networks SSID
2018-01-10 21:14:30 +00:00
ssid = network[0].decode("utf-8")
# check if the SSID is in the config file
if ssid in config["wifis"].keys():
2018-01-10 21:14:30 +00:00
print("connecting to: " + ssid)
# 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()