39 lines
1.0 KiB
Python
39 lines
1.0 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"]
|
||
|
|
||
|
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
|
||
|
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
|
||
|
|
||
|
gc.collect()
|