diff --git a/Software/Software.ino b/Software/Software.ino index 440378aa..ce863ef3 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -320,6 +320,22 @@ void init_stored_settings() { settings.clear(); // If this clear function is executed, no settings will be read from storage #endif + Serial.println("SSID before: "); + Serial.println(String(ssid.c_str())); + + char tempSSIDstring[63]; // Allocate buffer with sufficient size + size_t lengthSSID = settings.getString("SSID", tempSSIDstring, sizeof(tempSSIDstring)); + if (lengthSSID > 0) { // Successfully read the string from memory. Set it to SSID! + ssid = tempSSIDstring; + } else { // Reading from settings failed. Do nothing with SSID. Raise event? + } + char tempPasswordString[63]; // Allocate buffer with sufficient size + size_t lengthPassword = settings.getString("PASSWORD", tempPasswordString, sizeof(tempPasswordString)); + if (lengthPassword > 7) { // Successfully read the string from memory. Set it to password! + password = tempPasswordString; + } else { // Reading from settings failed. Do nothing with SSID. Raise event? + } + static uint32_t temp = 0; temp = settings.getUInt("BATTERY_WH_MAX", false); if (temp != 0) { @@ -733,6 +749,8 @@ void init_serialDataLink() { void storeSettings() { settings.begin("batterySettings", false); + settings.putString("SSID", String(ssid.c_str())); + settings.putString("PASSWORD", String(password.c_str())); settings.putUInt("BATTERY_WH_MAX", datalayer.battery.info.total_capacity_Wh); settings.putUInt("MAXPERCENTAGE", datalayer.battery.settings.max_percentage / 10); // Divide by 10 for backwards compatibility @@ -741,7 +759,6 @@ void storeSettings() { settings.putUInt("MAXCHARGEAMP", datalayer.battery.info.max_charge_amp_dA); settings.putUInt("MAXDISCHARGEAMP", datalayer.battery.info.max_discharge_amp_dA); settings.putBool("USE_SCALED_SOC", datalayer.battery.settings.soc_scaling_active); - settings.end(); } diff --git a/Software/USER_SETTINGS.cpp b/Software/USER_SETTINGS.cpp index c35fee69..d3491836 100644 --- a/Software/USER_SETTINGS.cpp +++ b/Software/USER_SETTINGS.cpp @@ -1,5 +1,5 @@ #include "USER_SETTINGS.h" - +#include /* This file contains all the battery settings and limits */ /* They can be defined here, or later on in the WebUI */ @@ -14,8 +14,8 @@ volatile float CHARGER_END_A = 1.0; // Current at which charging is consid #ifdef WEBSERVER volatile uint8_t AccessPointEnabled = true; //Set to either true or false incase you want the board to enable a direct wifi access point -const char* ssid = "REPLACE_WITH_YOUR_SSID"; // Maximum of 63 characters; -const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Minimum of 8 characters; +std::string ssid = "REPLACE_WITH_YOUR_SSID"; // Maximum of 63 characters; +std::string password = "REPLACE_WITH_YOUR_PASSWORD"; // Minimum of 8 characters; const char* ssidAP = "Battery Emulator"; // Maximum of 63 characters; const char* passwordAP = "123456789"; // Minimum of 8 characters; set to NULL if you want the access point to be open const uint8_t wifi_channel = 0; // set to 0 for automatic channel selection diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index 565fae9e..559d0f91 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -52,7 +52,7 @@ //#define SERIAL_LINK_RECEIVER //Enable this line to receive battery data over RS485 pins from another Lilygo (This LilyGo interfaces with inverter) //#define SERIAL_LINK_TRANSMITTER //Enable this line to send battery data over RS485 pins to another Lilygo (This LilyGo interfaces with battery) #define WEBSERVER //Enable this line to enable WiFi, and to run the webserver. See USER_SETTINGS.cpp for the Wifi settings. -//#define LOAD_SAVED_SETTINGS_ON_BOOT //Enable this line to read settings stored via the webserver on boot (overrides any battery settings set in USER_SETTINGS.cpp) +#define LOAD_SAVED_SETTINGS_ON_BOOT //Enable this line to read settings stored via the webserver on boot (overrides Wifi/battery settings set below) //#define FUNCTION_TIME_MEASUREMENT // Enable this to record execution times and present them in the web UI (WARNING, raises CPU load, do not use for production) /* MQTT options */ diff --git a/Software/src/devboard/webserver/settings_html.cpp b/Software/src/devboard/webserver/settings_html.cpp index c2f0ed62..75308427 100644 --- a/Software/src/devboard/webserver/settings_html.cpp +++ b/Software/src/devboard/webserver/settings_html.cpp @@ -1,6 +1,7 @@ #include "settings_html.h" #include #include "../../datalayer/datalayer.h" +#include "webserver.h" String settings_processor(const String& var) { if (var == "X") { @@ -13,6 +14,18 @@ String settings_processor(const String& var) { // Start a new block with a specific background color content += "
"; + content += "

SSID: " + String(ssid.c_str()) + + "

"; + content += + "

Password: ########

"; + + // Close the block + content += "
"; + + // Start a new block with a specific background color + content += "
"; + // Show current settings with edit buttons and input fields content += "

Battery capacity: " + String(datalayer.battery.info.total_capacity_Wh) + @@ -87,6 +100,26 @@ String settings_processor(const String& var) { content += "function editError() {"; content += " alert('Invalid input');"; content += "}"; + content += "function editSSID() {"; + content += "var value = prompt('Enter new SSID:');"; + content += "if (value !== null) {"; + content += " var xhr = new XMLHttpRequest();"; + content += " xhr.onload = editComplete;"; + content += " xhr.onerror = editError;"; + content += " xhr.open('GET', '/updateSSID?value=' + encodeURIComponent(value), true);"; + content += " xhr.send();"; + content += "}"; + content += "}"; + content += "function editPassword() {"; + content += "var value = prompt('Enter new password:');"; + content += "if (value !== null) {"; + content += " var xhr = new XMLHttpRequest();"; + content += " xhr.onload = editComplete;"; + content += " xhr.onerror = editError;"; + content += " xhr.open('GET', '/updatePassword?value=' + encodeURIComponent(value), true);"; + content += " xhr.send();"; + content += "}"; + content += "}"; content += "function editWh() {"; content += "var value = prompt('How much energy the battery can store. Enter new Wh value (1-120000):');"; content += "if (value !== null) {"; diff --git a/Software/src/devboard/webserver/settings_html.h b/Software/src/devboard/webserver/settings_html.h index 7ff523eb..cd5f47be 100644 --- a/Software/src/devboard/webserver/settings_html.h +++ b/Software/src/devboard/webserver/settings_html.h @@ -2,6 +2,10 @@ #define SETTINGS_H #include +#include + +extern std::string ssid; +extern std::string password; #include "../../../USER_SETTINGS.h" // Needed for WiFi ssid and password diff --git a/Software/src/devboard/webserver/webserver.cpp b/Software/src/devboard/webserver/webserver.cpp index a64ed375..ebb034d9 100644 --- a/Software/src/devboard/webserver/webserver.cpp +++ b/Software/src/devboard/webserver/webserver.cpp @@ -43,7 +43,7 @@ void init_webserver() { } else { WiFi.mode(WIFI_STA); // Only Router connection } - init_WiFi_STA(ssid, password, wifi_channel); + init_WiFi_STA(ssid.c_str(), password.c_str(), wifi_channel); String content = index_html; @@ -64,6 +64,37 @@ void init_webserver() { server.on("/events", HTTP_GET, [](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, events_processor); }); + // Route for editing SSID + server.on("/updateSSID", HTTP_GET, [](AsyncWebServerRequest* request) { + if (request->hasParam("value")) { + String value = request->getParam("value")->value(); + if (value.length() <= 63) { // Check if SSID is within the allowable length + ssid = value.c_str(); + storeSettings(); + request->send(200, "text/plain", "Updated successfully"); + } else { + request->send(400, "text/plain", "SSID must be 63 characters or less"); + } + } else { + request->send(400, "text/plain", "Bad Request"); + } + }); + // Route for editing Password + server.on("/updatePassword", HTTP_GET, [](AsyncWebServerRequest* request) { + if (request->hasParam("value")) { + String value = request->getParam("value")->value(); + if (value.length() > 8) { // Check if password is within the allowable length + password = value.c_str(); + storeSettings(); + request->send(200, "text/plain", "Updated successfully"); + } else { + request->send(400, "text/plain", "Password must be atleast 8 characters"); + } + } else { + request->send(400, "text/plain", "Bad Request"); + } + }); + // Route for editing Wh server.on("/updateBatterySize", HTTP_GET, [](AsyncWebServerRequest* request) { if (request->hasParam("value")) { @@ -302,7 +333,7 @@ void wifi_monitor() { Serial.println(getConnectResultString(status)); #endif if (wifi_state == INIT) { //we haven't been connected yet, try the init logic - init_WiFi_STA(ssid, password, wifi_channel); + init_WiFi_STA(ssid.c_str(), password.c_str(), wifi_channel); } else { //we were connected before, try the reconnect logic if (currentMillis - last_wifi_attempt_time > wifi_reconnect_interval) { last_wifi_attempt_time = currentMillis; @@ -319,7 +350,7 @@ void wifi_monitor() { wifi_reconnect_interval = DEFAULT_WIFI_RECONNECT_INTERVAL; // Print local IP address and start web server #ifdef DEBUG_VIA_USB - Serial.print("Connected to WiFi network: " + String(ssid)); + Serial.print("Connected to WiFi network: " + String(ssid.c_str())); Serial.print(" IP address: " + WiFi.localIP().toString()); Serial.print(" Signal Strength: " + String(WiFi.RSSI()) + " dBm"); Serial.println(" Channel: " + String(WiFi.channel())); @@ -404,7 +435,7 @@ String processor(const String& var) { wl_status_t status = WiFi.status(); // Display ssid of network connected to and, if connected to the WiFi, its own IP - content += "

SSID: " + String(ssid) + "

"; + content += "

SSID: " + String(ssid.c_str()) + "

"; if (status == WL_CONNECTED) { content += "

IP: " + WiFi.localIP().toString() + "

"; // Get and display the signal strength (RSSI) and channel diff --git a/Software/src/devboard/webserver/webserver.h b/Software/src/devboard/webserver/webserver.h index fda8c2cf..93d17fde 100644 --- a/Software/src/devboard/webserver/webserver.h +++ b/Software/src/devboard/webserver/webserver.h @@ -18,8 +18,9 @@ extern const char* version_number; // The current software version, shown on webserver -extern const char* ssid; -extern const char* password; +#include +extern std::string ssid; +extern std::string password; extern const uint8_t wifi_channel; extern const char* ssidAP; extern const char* passwordAP;