From d1e6d3dfe669796826b21b7e42c95ee32f73041f Mon Sep 17 00:00:00 2001 From: "Adrian A. Baumann" Date: Wed, 22 Apr 2026 00:09:01 +0200 Subject: [PATCH] OTA deleted from memory after done --- boot.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 boot.py diff --git a/boot.py b/boot.py new file mode 100644 index 0000000..3a81e9e --- /dev/null +++ b/boot.py @@ -0,0 +1,64 @@ +""" +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 gauge +import network +import gc +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) + utime.sleep_ms(5000) + ota._fetch_commit_sha = None + ota._fetch_manifest = None + ota._fetch_dir = None + ota._api_get = None + ota._download = None + ota.urequests = None + del ota.urequests + del ota + gc.collect() + del sys.modules["ota"] + gc.collect() + +else: + print("[boot] WiFi failed — skipping OTA, booting with existing files") + +# main.py runs automatically after boot.py + + + +