From 512cfb27ec35ef3c3e5806aa136267ab7f15664c Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 11 Feb 2026 14:37:39 +0900 Subject: [PATCH] Don't crash when no wifi network is configured --- src/main.cpp | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index da295443..c2f3569c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -116,6 +116,11 @@ bool last_tcp_online = false; bool last_lora_online = false; bool last_wifi_connected = false; +// Pending WiFi reconnect (deferred from LVGL task to main loop) +volatile bool wifi_reconnect_pending = false; +String pending_wifi_ssid; +String pending_wifi_password; + // Forward declarations void start_tcp_interface(); @@ -803,24 +808,16 @@ void setup_ui_manager() { INFO(("Brightness changed to " + String(brightness)).c_str()); }); - // Set WiFi reconnect callback + // Set WiFi reconnect callback (deferred to main loop to avoid blocking LVGL task) settings->set_wifi_reconnect_callback([](const String& ssid, const String& password) { - INFO(("Reconnecting WiFi to: " + ssid).c_str()); - WiFi.disconnect(); - delay(100); - WiFi.begin(ssid.c_str(), password.c_str()); - - // Wait for connection (with timeout) - uint32_t start = millis(); - while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) { - delay(100); - } - - if (WiFi.status() == WL_CONNECTED) { - INFO(("WiFi connected! IP: " + WiFi.localIP().toString()).c_str()); - } else { - WARNING("WiFi reconnection failed"); + if (ssid.isEmpty()) { + WARNING("WiFi reconnect skipped: SSID is empty"); + return; } + pending_wifi_ssid = ssid; + pending_wifi_password = password; + wifi_reconnect_pending = true; + INFO(("WiFi reconnect queued for: " + ssid).c_str()); }); // Set save callback (update app_settings and apply) @@ -1196,6 +1193,28 @@ void loop() { // Handle LVGL rendering (must be called frequently for smooth UI) UI::LVGL::LVGLInit::task_handler(); + // Handle deferred WiFi reconnect (from LVGL task) + if (wifi_reconnect_pending) { + wifi_reconnect_pending = false; + INFO(("Reconnecting WiFi to: " + pending_wifi_ssid).c_str()); + WiFi.disconnect(); + delay(100); + WiFi.begin(pending_wifi_ssid.c_str(), pending_wifi_password.c_str()); + + uint32_t start = millis(); + while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) { + delay(100); + } + + if (WiFi.status() == WL_CONNECTED) { + INFO(("WiFi connected! IP: " + WiFi.localIP().toString()).c_str()); + } else { + WARNING("WiFi reconnection failed"); + } + pending_wifi_ssid = ""; + pending_wifi_password = ""; + } + // Process Reticulum reticulum->loop();