mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 02:39:57 +02:00
Add SSID/PWD writing from webserver
This commit is contained in:
parent
dd7ffcb09c
commit
99fd0d46b2
7 changed files with 97 additions and 11 deletions
|
@ -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) {";
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue