10
0
sumorobot-firmware/boot.py

43 lines
1.1 KiB
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 *
2018-07-11 19:13:51 +00:00
from utime import sleep_ms
from machine import Timer, Pin
2018-01-10 21:14:30 +00:00
2018-07-11 19:13:51 +00:00
print("Press Ctrl-C to stop boot script...")
sleep_ms(200)
2018-01-10 21:14:30 +00:00
2018-07-11 19:13:51 +00:00
# Open and parse the config file
2018-01-13 15:13:49 +00:00
with open("config.json", "r") as config_file:
config = ujson.load(config_file)
2018-01-10 21:14:30 +00:00
2018-07-11 19:13:51 +00:00
# Initialize the SumoRobot object
sumorobot = Sumorobot(config)
# Indiacte booting with blinking status LED
timer = Timer(0)
sumorobot.toggle_led()
timer.init(period=2000, mode=Timer.PERIODIC, callback=sumorobot.toggle_led)
# Connect to WiFi
2018-01-10 21:14:30 +00:00
wlan = network.WLAN(network.STA_IF)
2018-07-11 19:13:51 +00:00
# Activate the WiFi interface
2018-01-10 21:14:30 +00:00
wlan.active(True)
2018-07-11 19:13:51 +00:00
# If not already connected
2018-01-10 21:14:30 +00:00
if not wlan.isconnected():
2018-07-11 19:13:51 +00:00
# Scan for WiFi networks
2018-01-10 21:14:30 +00:00
networks = wlan.scan()
2018-07-11 19:13:51 +00:00
# Go trough all scanned WiFi networks
2018-01-10 21:14:30 +00:00
for network in networks:
2018-07-11 19:13:51 +00:00
# Extract the networks SSID
2018-01-10 21:14:30 +00:00
ssid = network[0].decode("utf-8")
2018-07-11 19:13:51 +00:00
# Check if the SSID is in the config file
if ssid in config["wifis"].keys():
2018-07-11 19:13:51 +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-07-11 19:13:51 +00:00
# Clean up
2018-01-10 21:14:30 +00:00
import gc
gc.collect()