100 lines
3.3 KiB
Python
100 lines
3.3 KiB
Python
# This file is executed on every boot (including wake-boot from deepsleep)
|
|
import time
|
|
import sys
|
|
import board_config
|
|
import wifi_config
|
|
|
|
try:
|
|
import network
|
|
has_network = True
|
|
except ImportError:
|
|
has_network = False
|
|
|
|
def connect_wifi():
|
|
if sys.platform == 'rp2':
|
|
# Skip display and connection setup on RP2 (no network/Wi-Fi hardware)
|
|
time.sleep(1.5)
|
|
return
|
|
|
|
# Use the pre-initialized display from board_config
|
|
display = board_config.display_instance
|
|
if display:
|
|
try:
|
|
display.clear(0)
|
|
board_name = "ESP32-S3 " + str(board_config.BOARD_TYPE)
|
|
display.text(board_name, 10, 10, 1)
|
|
display.line(10, 20, display.width - 10, 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 setup 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:
|
|
try:
|
|
display.text(".", dot_x, 70, 1)
|
|
dot_x += 10
|
|
display.show()
|
|
except:
|
|
pass
|
|
|
|
if wlan.isconnected():
|
|
ip = wlan.ifconfig()[0]
|
|
print(f"Wi-Fi Connected! IP Address: {ip}")
|
|
if display:
|
|
try:
|
|
display.clear(0)
|
|
board_name = "ESP32-S3 " + str(board_config.BOARD_TYPE)
|
|
display.text(board_name, 10, 10, 1)
|
|
display.line(10, 20, display.width - 10, 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)
|
|
except:
|
|
pass
|
|
else:
|
|
print("Wi-Fi Connection Failed.")
|
|
if display:
|
|
try:
|
|
display.clear(0)
|
|
board_name = "ESP32-S3 " + str(board_config.BOARD_TYPE)
|
|
display.text(board_name, 10, 10, 1)
|
|
display.line(10, 20, display.width - 10, 20, 1)
|
|
display.text("Wi-Fi Status: 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)
|
|
except:
|
|
pass
|
|
|
|
connect_wifi()
|