47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
"""
|
|
boot.py — runs before main.py on every ESP32 boot
|
|
|
|
Connects WiFi, runs OTA update, then hands off to main.py.
|
|
Keep this file as simple as possible — it is never OTA-updated itself
|
|
(it lives outside the repo folder) so bugs here require USB to fix.
|
|
"""
|
|
|
|
import network
|
|
import utime
|
|
import sys
|
|
|
|
import ota
|
|
|
|
ota.load_config()
|
|
WIFI_SSID, WIFI_PASSWORD = ota.WIFI_SSID, ota.WIFI_PASSWORD
|
|
|
|
def _connect_wifi(timeout_s=20):
|
|
sta = network.WLAN(network.STA_IF)
|
|
sta.active(True)
|
|
sta.config(txpower=15)
|
|
if sta.isconnected():
|
|
return True
|
|
sta.connect(WIFI_SSID, WIFI_PASSWORD)
|
|
deadline = utime.time() + timeout_s
|
|
while not sta.isconnected():
|
|
if utime.time() > deadline:
|
|
return False
|
|
utime.sleep_ms(300)
|
|
return True
|
|
|
|
if WIFI_SSID is None:
|
|
print("[boot] No WiFi credentials — cannot connect, skipping OTA")
|
|
elif _connect_wifi():
|
|
ip = network.WLAN(network.STA_IF).ifconfig()[0]
|
|
print(f"[boot] WiFi connected — {ip}")
|
|
|
|
try:
|
|
ota.update()
|
|
except Exception as e:
|
|
print(f"[boot] OTA error: {e} — continuing with existing files")
|
|
sys.print_exception(e)
|
|
else:
|
|
print("[boot] WiFi failed — skipping OTA, booting with existing files")
|
|
|
|
# main.py runs automatically after boot.py
|