sumorobot-remote/boot.py
2024-11-03 17:54:59 +02:00

42 lines
1.1 KiB
Python

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import gc
import ujson
import network
from utime import sleep_ms
from machine import Timer, Pin
#webrepl.start()
# Open and parse the config file
with open("config.json", "r") as config_file:
config = ujson.load(config_file)
remoteName = "Remote-"+config["remote-id"]
print("remoteName", remoteName)
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.config(dhcp_hostname=remoteName)
# If not already connected
if not wlan.isconnected():
# Scan for WiFi networks
print("Scan")
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
print("Connecting")
wlan.connect(ssid, config["wifis"][ssid])
break
gc.collect()