Add SSID/PWD writing from webserver

This commit is contained in:
Daniel Öster 2024-07-09 23:38:17 +03:00
parent dd7ffcb09c
commit 99fd0d46b2
7 changed files with 97 additions and 11 deletions

View file

@ -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();
}

View file

@ -1,5 +1,5 @@
#include "USER_SETTINGS.h"
#include <string>
/* 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

View file

@ -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 */

View file

@ -1,6 +1,7 @@
#include "settings_html.h"
#include <Arduino.h>
#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 += "<div style='background-color: #303E47; padding: 10px; margin-bottom: 10px;border-radius: 50px'>";
content += "<h4 style='color: white;'>SSID: <span id='SSID'>" + String(ssid.c_str()) +
" </span> <button onclick='editSSID()'>Edit</button></h4>";
content +=
"<h4 style='color: white;'>Password: ######## <span id='Password'></span> <button "
"onclick='editPassword()'>Edit</button></h4>";
// Close the block
content += "</div>";
// Start a new block with a specific background color
content += "<div style='background-color: #2D3F2F; padding: 10px; margin-bottom: 10px;border-radius: 50px'>";
// Show current settings with edit buttons and input fields
content += "<h4 style='color: white;'>Battery capacity: <span id='BATTERY_WH_MAX'>" +
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) {";

View file

@ -2,6 +2,10 @@
#define SETTINGS_H
#include <Arduino.h>
#include <string>
extern std::string ssid;
extern std::string password;
#include "../../../USER_SETTINGS.h" // Needed for WiFi ssid and password

View file

@ -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 += "<h4>SSID: " + String(ssid) + "</h4>";
content += "<h4>SSID: " + String(ssid.c_str()) + "</h4>";
if (status == WL_CONNECTED) {
content += "<h4>IP: " + WiFi.localIP().toString() + "</h4>";
// Get and display the signal strength (RSSI) and channel

View file

@ -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 <string>
extern std::string ssid;
extern std::string password;
extern const uint8_t wifi_channel;
extern const char* ssidAP;
extern const char* passwordAP;