# This file is executed on every boot (including wake-boot from deepsleep) import time import sys try: import network has_network = True except ImportError: has_network = False if sys.platform == 'rp2': import st7796 as display_module else: import rlcd as display_module from machine import Pin, SPI import wifi_config def connect_wifi(): if sys.platform == 'rp2': # Skip display and connection setup on RP2 (no network/Wi-Fi hardware) # Keep the sleep window to make REPL interruption easy time.sleep(1.5) return # Initialize display to show connection progress display = None try: # ESP32-S3 configuration spi = SPI(1, baudrate=20000000, polarity=0, phase=0, sck=Pin(11), mosi=Pin(12)) display = display_module.RLCD(spi, cs=Pin(40), dc=Pin(5), rst=Pin(41)) display.clear(0) display.text("ESP32-S3-RLCD-4.2", 10, 10, 1) display.line(10, 20, 390, 20, 1) display.text("Connecting to Wi-Fi...", 10, 35, 1) display.text(f"SSID: {wifi_config.WIFI_SSID}", 10, 50, 1) display.show() except Exception as e: print("Display init failed in boot.py:", e) # Initialize Wi-Fi Station if not has_network: return try: network.hostname('esp32screen') print("Hostname set to: {}".format(network.hostname())) except Exception as he: print("Failed to set hostname: {}".format(he)) wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print(f"Connecting to network {wifi_config.WIFI_SSID}...") wlan.connect(wifi_config.WIFI_SSID, wifi_config.WIFI_PASS) # Wait up to 15 seconds for connection timeout = 15 dot_x = 10 while not wlan.isconnected() and timeout > 0: time.sleep(1) timeout -= 1 print("Connecting...") if display: display.text(".", dot_x, 70, 1) dot_x += 10 display.show() if wlan.isconnected(): ip = wlan.ifconfig()[0] print(f"Wi-Fi Connected! IP Address: {ip}") if display: display.clear(0) display.text("ESP32-S3-RLCD-4.2", 10, 10, 1) display.line(10, 20, 390, 20, 1) display.text("Wi-Fi Status: Connected!", 10, 35, 1) display.text(f"SSID: {wifi_config.WIFI_SSID}", 10, 50, 1) display.text(f"IP: {ip}", 10, 65, 1) display.text("Starting MCP Server...", 10, 90, 1) display.show() time.sleep(1.5) else: print("Wi-Fi Connection Failed.") if display: display.clear(0) display.text("ESP32-S3-RLCD-4.2", 10, 10, 1) display.line(10, 20, 390, 20, 1) display.text("Wi-Fi Status: Connection Failed!", 10, 35, 1) display.text("Check credentials in:", 10, 55, 1) display.text("wifi_config.py", 20, 70, 1) display.text("Starting offline...", 10, 95, 1) display.show() time.sleep(2) connect_wifi()