Add AP plus STA WiFi mode
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <WebServer.h>
|
#include <WebServer.h>
|
||||||
|
#include <Preferences.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@@ -152,6 +153,76 @@ HardwareSerial DashboardSerial(2);
|
|||||||
|
|
||||||
String uartLineBuffer;
|
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() {
|
float calculateRuntimeHours() {
|
||||||
if (!bmsData.valid || bmsData.current >= 0) return 0;
|
if (!bmsData.valid || bmsData.current >= 0) return 0;
|
||||||
float dischargeAmps = -bmsData.current;
|
float dischargeAmps = -bmsData.current;
|
||||||
@@ -652,6 +723,42 @@ void handleDebugSerial() {
|
|||||||
return;
|
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") {
|
if (command == "factory reset") {
|
||||||
factoryResetConfig();
|
factoryResetConfig();
|
||||||
Serial.println("OK factory reset complete");
|
Serial.println("OK factory reset complete");
|
||||||
@@ -1095,15 +1202,19 @@ void setup() {
|
|||||||
initSensors();
|
initSensors();
|
||||||
initBms();
|
initBms();
|
||||||
|
|
||||||
WiFi.mode(WIFI_AP);
|
loadWifiConfig();
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_AP_STA);
|
||||||
bool apResult = WiFi.softAP("OverlandController");
|
bool apResult = WiFi.softAP("OverlandController");
|
||||||
|
|
||||||
if (apResult) {
|
if (apResult) {
|
||||||
Serial.println("AP Started");
|
Serial.println("AP Started");
|
||||||
Serial.print("IP: ");
|
Serial.print("AP IP: ");
|
||||||
Serial.println(WiFi.softAPIP());
|
Serial.println(WiFi.softAPIP());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connectStaWifi();
|
||||||
|
|
||||||
server.on("/", HTTP_GET, []() {
|
server.on("/", HTTP_GET, []() {
|
||||||
server.send_P(200, "text/html", INDEX_HTML);
|
server.send_P(200, "text/html", INDEX_HTML);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user