From a3104a6047d16c50bcb204238c2368fd334e9ee9 Mon Sep 17 00:00:00 2001 From: "Adrian A. Baumann" Date: Mon, 29 Jun 2026 00:06:00 +0200 Subject: [PATCH] Add reset endpoint, button, and auto-restart on hostname change --- src/main.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 8f89161..18f5983 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -293,7 +293,8 @@ static void handleRoot() { html += "fieldset .row label{width:70px;margin-bottom:0;flex-shrink:0}"; html += "fieldset .row input{margin-bottom:0}"; html += ".btn{width:100%;margin-top:8px;padding:12px;background:#4361ee;color:#fff;border:none;border-radius:8px;font-size:15px;font-weight:600;cursor:pointer;transition:background .2s}"; - html += ".btn:hover{background:#3651d4}.mac{font-size:12px;color:#999;text-align:center;margin-top:20px}"; + html += ".btn:hover{background:#3651d4}.btn-danger{background:#dc2626}.btn-danger:hover{background:#b91c1c}"; + html += ".mac{font-size:12px;color:#999;text-align:center;margin-top:20px}"; html += ".tgl-lbl{margin-bottom:0!important;line-height:18px}.switch{position:relative;display:inline-block;width:32px;height:18px;margin-left:auto;flex-shrink:0}fieldset .row label.switch{width:32px}"; html += ".switch input{opacity:0;width:0;height:0;margin-bottom:0}.slid{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background:#ccc;border-radius:18px;transition:.3s}"; html += ".slid::before{content:\"\";position:absolute;height:14px;width:14px;left:2px;top:2px;background:#fff;border-radius:50%;transition:.3s;box-shadow:0 1px 3px rgba(0,0,0,.2)}"; @@ -307,16 +308,29 @@ static void handleRoot() { html += authSection; html += mqttSection; html += ""; + html += "
"; + html += "
"; html += "
" + WiFi.macAddress() + "
"; html += ""; server.send(200, "text/html", html); Serial.printf("[HTTP] GET / -> 200 (%u bytes)\n", html.length()); } +static void handleReset() { + if (!requireAuth()) { Serial.println("[HTTP] POST /reset -> 401"); return; } + Serial.println("[HTTP] POST /reset -> restarting"); + server.send(200, "text/plain", "Restarting..."); + delay(200); + ESP.restart(); +} + static void handleConfig() { if (!requireAuth()) { Serial.println("[HTTP] POST /config -> 401"); return; } Serial.println("[HTTP] POST /config"); + char oldHostname[32]; + strlcpy(oldHostname, hostname, sizeof(oldHostname)); + if (server.hasArg("hostname")) { String val = server.arg("hostname"); val.trim(); @@ -324,6 +338,8 @@ static void handleConfig() { val.toCharArray(hostname, sizeof(hostname)); } + bool hostnameChanged = strcmp(hostname, oldHostname) != 0; + // Persist connection fields regardless of enable checkbox so edits made while // disabling MQTT aren't discarded. if (server.hasArg("mqtt_host")) @@ -359,11 +375,18 @@ static void handleConfig() { server.sendHeader("Location", "/", true); server.send(303); + + if (hostnameChanged) { + Serial.printf("[HTTP] hostname changed %s -> %s, restarting\n", oldHostname, hostname); + delay(200); + ESP.restart(); + } } static void startServer() { server.on("/", handleRoot); server.on("/config", HTTP_POST, handleConfig); + server.on("/reset", HTTP_POST, handleReset); server.begin(); Serial.println("HTTP server started"); }