From 57590981b14f3f816d7b0c7680b96e5edae7e3d3 Mon Sep 17 00:00:00 2001 From: nick Date: Thu, 4 Jun 2026 12:56:02 -0600 Subject: [PATCH] Add AP plus STA WiFi mode --- .../overland-controller.ino | 115 +++++++++++++++++- 1 file changed, 113 insertions(+), 2 deletions(-) diff --git a/firmware/esp32/overland-controller/overland-controller.ino b/firmware/esp32/overland-controller/overland-controller.ino index 9d7f0fd..8f5debf 100644 --- a/firmware/esp32/overland-controller/overland-controller.ino +++ b/firmware/esp32/overland-controller/overland-controller.ino @@ -1,5 +1,6 @@ #include #include +#include #include #include "config.h" @@ -152,6 +153,76 @@ HardwareSerial DashboardSerial(2); String uartLineBuffer; +Preferences wifiPrefs; +String staSsid = ""; +String staPassword = ""; + +void loadWifiConfig() { + wifiPrefs.begin("wifi", true); + staSsid = wifiPrefs.getString("ssid", ""); + staPassword = wifiPrefs.getString("password", ""); + wifiPrefs.end(); +} + +void saveWifiConfig() { + wifiPrefs.begin("wifi", false); + wifiPrefs.putString("ssid", staSsid); + wifiPrefs.putString("password", staPassword); + wifiPrefs.end(); +} + +void clearWifiConfig() { + wifiPrefs.begin("wifi", false); + wifiPrefs.clear(); + wifiPrefs.end(); + staSsid = ""; + staPassword = ""; +} + +void connectStaWifi() { + if (staSsid.length() == 0) { + Serial.println("STA WiFi not configured."); + return; + } + + Serial.print("Connecting STA WiFi to: "); + Serial.println(staSsid); + + WiFi.begin(staSsid.c_str(), staPassword.c_str()); + + unsigned long start = millis(); + while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) { + delay(500); + Serial.print("."); + } + + Serial.println(); + + if (WiFi.status() == WL_CONNECTED) { + Serial.println("STA WiFi connected"); + Serial.print("STA IP: "); + Serial.println(WiFi.localIP()); + } else { + Serial.println("STA WiFi failed. AP remains available."); + } +} + +void printNetworkStatus() { + Serial.println("Network:"); + Serial.print(" AP IP: "); + Serial.println(WiFi.softAPIP()); + Serial.print(" STA SSID: "); + Serial.println(staSsid.length() ? staSsid : "(not configured)"); + Serial.print(" STA Connected: "); + Serial.println(WiFi.status() == WL_CONNECTED ? "true" : "false"); + if (WiFi.status() == WL_CONNECTED) { + Serial.print(" STA IP: "); + Serial.println(WiFi.localIP()); + } +} + + + float calculateRuntimeHours() { if (!bmsData.valid || bmsData.current >= 0) return 0; float dischargeAmps = -bmsData.current; @@ -652,6 +723,42 @@ void handleDebugSerial() { return; } + if (command == "wifi status") { + printNetworkStatus(); + return; + } + + if (command.startsWith("wifi ssid ")) { + staSsid = command.substring(10); + Serial.println("OK WiFi SSID updated"); + Serial.println("Run: wifi save"); + return; + } + + if (command.startsWith("wifi pass ")) { + staPassword = command.substring(10); + Serial.println("OK WiFi password updated"); + Serial.println("Run: wifi save"); + return; + } + + if (command == "wifi save") { + saveWifiConfig(); + Serial.println("OK WiFi config saved"); + return; + } + + if (command == "wifi connect") { + connectStaWifi(); + return; + } + + if (command == "wifi clear") { + clearWifiConfig(); + Serial.println("OK WiFi config cleared"); + return; + } + if (command == "factory reset") { factoryResetConfig(); Serial.println("OK factory reset complete"); @@ -1095,15 +1202,19 @@ void setup() { initSensors(); initBms(); - WiFi.mode(WIFI_AP); + loadWifiConfig(); + + WiFi.mode(WIFI_AP_STA); bool apResult = WiFi.softAP("OverlandController"); if (apResult) { Serial.println("AP Started"); - Serial.print("IP: "); + Serial.print("AP IP: "); Serial.println(WiFi.softAPIP()); } + connectStaWifi(); + server.on("/", HTTP_GET, []() { server.send_P(200, "text/html", INDEX_HTML); });