2024-11-03 13:29:43 +00:00
|
|
|
# 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"]
|
2024-11-03 15:54:59 +00:00
|
|
|
print("remoteName", remoteName)
|
2024-11-03 13:29:43 +00:00
|
|
|
|
|
|
|
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
|
2024-11-03 15:54:59 +00:00
|
|
|
print("Scan")
|
2024-11-03 13:29:43 +00:00
|
|
|
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
|
2024-11-03 15:54:59 +00:00
|
|
|
print("Connecting")
|
2024-11-03 13:29:43 +00:00
|
|
|
wlan.connect(ssid, config["wifis"][ssid])
|
|
|
|
break
|
|
|
|
|
|
|
|
gc.collect()
|