sumorobot-firmware/boot.py

54 lines
1.3 KiB
Python
Raw Normal View History

# Import functions from time library
from utime import ticks_us, sleep_us, sleep_ms
2018-01-10 21:14:30 +00:00
# Give time to cancel boot script
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
# Import libraries
import os
import ujson
import network
import _thread
import uwebsockets
from machine import Timer, reset
from hal import *
# Loading libraries takes ca 400ms
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)
timer.init(period=2000, mode=Timer.PERIODIC, callback=sumorobot.toggle_led)
# Connected Wi-Fi SSID
ssid = None
2018-07-11 19:13:51 +00:00
# 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
temp_ssid = network[0].decode("utf-8")
2018-07-11 19:13:51 +00:00
# Check if the SSID is in the config file
if temp_ssid in config["wifis"].keys():
ssid = temp_ssid
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()