sumorobot-firmware/boot.py

62 lines
1.6 KiB
Python

import ujson
import network
from hal import *
from utime import sleep_ms
from machine import Timer, Pin
import ubinascii
print("Press Ctrl-C to stop boot script...")
sleep_ms(200)
#Pin(25, Pin.OUT).value(0)
# Open and parse the config file
with open("config.json", "r") as config_file:
config = ujson.load(config_file)
# if not config["sumo_id"]:
# config["sumo_id"] = ubinascii.hexlify(network.WLAN().config('mac'),':').decode().replace(":","")[6:]
# with open("config.part", "w") as config_file:
# config_file.write(ujson.dumps(config))
# os.rename("config.part", "config.json")
config["sumo_id"] = ubinascii.hexlify(network.WLAN().config('mac'),':').decode().replace(":","")[6:]
sleep_ms(500)
robotName = "Sumo-"+config["sumo_id"]
# 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
wlan = network.WLAN(network.STA_IF)
# Activate the WiFi interface
wlan.active(True)
wlan.config(dhcp_hostname=robotName)
# If not already connected
if not wlan.isconnected():
# Scan for WiFi networks
networks = wlan.scan()
# Go trough all scanned WiFi networks
for network in networks:
# Extract the networks SSID
ssid = network[0].decode("utf-8")
# Check if the SSID is in the config file
if ssid in config["wifis"].keys():
# Start to connect to the pre-configured network
wlan.connect(ssid, config["wifis"][ssid])
break
# Clean up
import gc
gc.collect()