Skip to content

Commit 9a3b9a1

Browse files
Merge pull request #100 from oldboys92/feature_reset-password
Feature `Reset password` configuration option
2 parents e2285de + 27af3b6 commit 9a3b9a1

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

src/config/Configuration.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ tm timeinfo;
1111
// ============================================================================
1212

1313
// Data source and server settings
14+
char reset_password[33] = "admin-changeMe"; // default reset password
1415
char input_type[40];
1516
char ntp_server[40] = "de.pool.ntp.org";
1617
char timezone[64] = "CET-1CEST,M3.5.0/2,M10.5.0/3"; // Central European Time
@@ -143,6 +144,7 @@ void WifiManagerSetup() {
143144
sprintf(shelly_mac, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
144145

145146
preferences.begin("e2s_config", false);
147+
strcpy(reset_password, preferences.getString("reset_password", reset_password).c_str());
146148
strcpy(input_type, preferences.getString("input_type", input_type).c_str());
147149
strcpy(mqtt_server, preferences.getString("mqtt_server", mqtt_server).c_str());
148150
strcpy(ntp_server, preferences.getString("ntp_server", ntp_server).c_str());
@@ -167,9 +169,15 @@ void WifiManagerSetup() {
167169
strcpy(shelly_port, preferences.getString("shelly_port", shelly_port).c_str());
168170
strcpy(force_pwr_decimals, preferences.getString("force_pwr_decimals", force_pwr_decimals).c_str());
169171
strcpy(sma_id, preferences.getString("sma_id", sma_id).c_str());
170-
171-
WiFiManagerParameter custom_section1("<h3>General settings</h3>");
172-
WiFiManagerParameter custom_input_type("type", "<b>Data source</b><br><code>MQTT</code> for MQTT<br><code>HTTP</code> for generic HTTP<br><code>SMA</code> for SMA EM/HM multicast<br><code>SHRDZM</code> for SHRDZM UDP data<br><code>SUNSPEC</code> for Modbus TCP SUNSPEC data", input_type, 40);
172+
173+
const char *show_pwd_str = "<input type=\"checkbox\" onclick=\"t('%s')\">&nbsp;<label>Show Password</label><br/>";
174+
175+
WiFiManagerParameter custom_section1("<h3>General settings</h3><script>function t(s) { var x = document.getElementById(s); x.type === \"password\" ? x.type = \"text\" : x.type = \"password\"; }</script>");
176+
WiFiManagerParameter param_reset_password("reset_password", "Reset Password <span title=\"For resetting the WiFi configuration and putting the device in AP / config mode\" style=\"cursor: help;\" aria-label=\"Help\" tabindex=\"0\">(?)</span>", reset_password, 32, "type='password'");
177+
char buf_rst_pwd_show_pwd[150];
178+
sprintf(buf_rst_pwd_show_pwd, show_pwd_str, "reset_password");
179+
WiFiManagerParameter param_reset_password_show_password(buf_rst_pwd_show_pwd);
180+
WiFiManagerParameter custom_input_type("type", "<hr><b>Data source</b><br><code>MQTT</code> for MQTT<br><code>HTTP</code> for generic HTTP<br><code>SMA</code> for SMA EM/HM multicast<br><code>SHRDZM</code> for SHRDZM UDP data<br><code>SUNSPEC</code> for Modbus TCP SUNSPEC data", input_type, 40);
173181
WiFiManagerParameter custom_mqtt_server("server", "<b>Server</b><br>MQTT Server IP, query url for generic HTTP or Modbus TCP server IP for SUNSPEC", mqtt_server, 160);
174182
WiFiManagerParameter custom_mqtt_port("port", "<b>Port</b><br> for MQTT or Modbus TCP (SUNSPEC)", mqtt_port, 6);
175183
WiFiManagerParameter param_ntp_server("ntp_server", "NTP server <span title=\"for time synchronization\" style=\"cursor: help;\" aria-label=\"Help\" tabindex=\"0\">(?)</span>", ntp_server, 40);
@@ -184,8 +192,11 @@ void WifiManagerSetup() {
184192
WiFiManagerParameter custom_sma_id("sma_id", "<b>SMA serial number</b><br>optional serial number if you have more than one SMA EM/HM in your network", sma_id, 16);
185193
WiFiManagerParameter custom_section2("<hr><h3>MQTT options</h3>");
186194
WiFiManagerParameter custom_mqtt_topic("topic", "<b>MQTT Topic</b>", mqtt_topic, 90);
187-
WiFiManagerParameter custom_mqtt_user("user", "<b>MQTT user</b><br>optional", mqtt_user, 40);
188-
WiFiManagerParameter custom_mqtt_passwd("passwd", "<b>MQTT password</b><br>optional", mqtt_passwd, 40);
195+
WiFiManagerParameter custom_mqtt_user("user", "<b>MQTT user</b> (optional)", mqtt_user, 40);
196+
WiFiManagerParameter custom_mqtt_passwd("passwd", "<b>MQTT password</b> (optional)", mqtt_passwd, 40, "type='password'");
197+
char buf_mqtt_pwd_show_pwd[150];
198+
sprintf(buf_mqtt_pwd_show_pwd, show_pwd_str, "passwd");
199+
WiFiManagerParameter param_mqtt_passwd_show_password(buf_mqtt_pwd_show_pwd);
189200
WiFiManagerParameter custom_section3("<hr><h3>Modbus TCP options</h3>");
190201
WiFiManagerParameter custom_modbus_dev("modbus_dev", "<b>Modbus device ID</b><br><code>71</code> for Kostal SEM", modbus_dev, 60);
191202
WiFiManagerParameter custom_section4("<hr><h3>JSON paths for MQTT and generic HTTP</h3>");
@@ -206,6 +217,8 @@ void WifiManagerSetup() {
206217

207218
//add all your parameters here
208219
wifiManager.addParameter(&custom_section1);
220+
wifiManager.addParameter(&param_reset_password);
221+
wifiManager.addParameter(&param_reset_password_show_password);
209222
wifiManager.addParameter(&custom_input_type);
210223
wifiManager.addParameter(&custom_mqtt_server);
211224
wifiManager.addParameter(&param_ntp_server);
@@ -223,6 +236,7 @@ void WifiManagerSetup() {
223236
wifiManager.addParameter(&custom_mqtt_topic);
224237
wifiManager.addParameter(&custom_mqtt_user);
225238
wifiManager.addParameter(&custom_mqtt_passwd);
239+
wifiManager.addParameter(&param_mqtt_passwd_show_password);
226240
wifiManager.addParameter(&custom_section3);
227241
wifiManager.addParameter(&custom_modbus_dev);
228242
wifiManager.addParameter(&custom_section4);
@@ -244,6 +258,7 @@ void WifiManagerSetup() {
244258
DEBUG_SERIAL.println("connected");
245259

246260
//read updated parameters
261+
strcpy(reset_password, param_reset_password.getValue());
247262
strcpy(input_type, custom_input_type.getValue());
248263
strcpy(mqtt_server, custom_mqtt_server.getValue());
249264
strcpy(mqtt_port, custom_mqtt_port.getValue());
@@ -270,6 +285,7 @@ void WifiManagerSetup() {
270285
strcpy(sma_id, custom_sma_id.getValue());
271286

272287
DEBUG_SERIAL.println("The values in the preferences are: ");
288+
DEBUG_SERIAL.println("\treset_password: ********");
273289
DEBUG_SERIAL.println("\tinput_type : " + String(input_type));
274290
DEBUG_SERIAL.println("\tmqtt_server : " + String(mqtt_server));
275291
DEBUG_SERIAL.println("\tmqtt_port : " + String(mqtt_port));
@@ -282,7 +298,7 @@ void WifiManagerSetup() {
282298
DEBUG_SERIAL.println("\tshelly_mac : " + String(shelly_mac));
283299
DEBUG_SERIAL.println("\tmqtt_topic : " + String(mqtt_topic));
284300
DEBUG_SERIAL.println("\tmqtt_user : " + String(mqtt_user));
285-
DEBUG_SERIAL.println("\tmqtt_passwd : " + String(mqtt_passwd));
301+
DEBUG_SERIAL.println("\tmqtt_passwd : ********");
286302
DEBUG_SERIAL.println("\tmodbus_dev : " + String(modbus_dev));
287303
DEBUG_SERIAL.println("\tpower_path : " + String(power_path));
288304
DEBUG_SERIAL.println("\tpwr_export_path : " + String(pwr_export_path));
@@ -306,8 +322,7 @@ void WifiManagerSetup() {
306322
} else if (strcmp(input_type, "SUNSPEC") == 0) {
307323
dataSUNSPEC = true;
308324
DEBUG_SERIAL.println("Enabling SUNSPEC data input");
309-
}
310-
else {
325+
} else {
311326
dataMQTT = true;
312327
DEBUG_SERIAL.println("Enabling MQTT data input");
313328
}
@@ -326,6 +341,7 @@ void WifiManagerSetup() {
326341

327342
if (shouldSaveConfig) {
328343
DEBUG_SERIAL.println("saving config");
344+
preferences.putString("reset_password", reset_password);
329345
preferences.putString("input_type", input_type);
330346
preferences.putString("mqtt_server", mqtt_server);
331347
preferences.putString("mqtt_port", mqtt_port);

src/config/Configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern tm timeinfo;
4848
// ============================================================================
4949

5050
// Data source and server settings
51+
extern char reset_password[33];
5152
extern char input_type[40];
5253
extern char ntp_server[40];
5354
extern char timezone[64];

src/main.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ void setup(void) {
7575
html += ".btn-yes{background-color:#d9534f;color:white;border:none;}";
7676
html += ".btn-no{background-color:#5bc0de;color:white;border:none;}</style></head><body>";
7777
html += "<h2>Reset Configuration?</h2>";
78-
html += "<p>Are you sure you want to reset the WiFi configuration? This will clear all settings and restart the device.</p>";
79-
html += "<form method='POST' action='/reset' style='display:inline;'>";
78+
html += "<p>Are you sure you want to reset the WiFi configuration? This will clear current WiFi settings and restart the device in AP mode.</p>";
79+
html += "<form method='POST' style='display:inline;' accept-charset='UTF-8'>";
80+
if (reset_password != nullptr && strlen(reset_password) > 0) {
81+
html += "<input type='password' name='reset_password' placeholder='Enter reset password' required><br/>";
82+
}
8083
html += "<button type='submit' class='btn btn-yes'>Yes, Reset</button>";
8184
html += "</form>";
8285
html += "<a href='/' class='btn btn-no'>Cancel</a>";
@@ -85,8 +88,21 @@ void setup(void) {
8588
});
8689

8790
server.on("/reset", AsyncWebRequestMethod::HTTP_POST, [](AsyncWebServerRequest *request) {
88-
shouldResetConfig = true;
89-
request->send(200, "text/plain", "Resetting WiFi configuration, please log back into the hotspot to reconfigure...\r\n");
91+
if (reset_password != nullptr && strlen(reset_password) > 0) {
92+
if (request->hasParam("reset_password", true)) {
93+
if (String(reset_password) == request->getParam("reset_password", true)->value()) {
94+
shouldResetConfig = true;
95+
request->send(200, "text/plain", "Resetting WiFi configuration, please log back into the hotspot to reconfigure...\r\n");
96+
} else {
97+
request->send(403, "text/plain", "Unauthorized: Invalid reset password.\r\n");
98+
}
99+
} else {
100+
request->send(400, "text/plain", "Reset password missing.\r\n");
101+
}
102+
} else {
103+
shouldResetConfig = true;
104+
request->send(200, "text/plain", "Resetting WiFi configuration, please log back into the hotspot to reconfigure...\r\n");
105+
}
90106
});
91107

92108
// Shelly RPC endpoints called via HTTP GET method

0 commit comments

Comments
 (0)