#pragma once #include #include #include #include #include "config.h" static const char CAPTIVE_PORTAL_HTML[] PROGMEM = R"rawhtml( EPaper Setup

EPaper Display Setup

)rawhtml"; static const char CAPTIVE_PORTAL_SAVED_HTML[] PROGMEM = R"rawhtml( Gespeichert

Konfiguration gespeichert!

Das Gerät startet jetzt neu...

)rawhtml"; /** * Start a captive WiFi access point for initial configuration. * Creates AP "EPaper-Setup", serves a config form on port 80. * POST /save persists the config and reboots. * This function never returns. */ inline void startCaptivePortal(Config& config) { const byte DNS_PORT = 53; DNSServer dnsServer; WebServer server(80); // ── Start AP ────────────────────────────────────────────────────────────── WiFi.mode(WIFI_AP); WiFi.softAP("EPaper-Setup"); // open network, no password IPAddress apIP = WiFi.softAPIP(); Serial.print("[Portal] AP started, IP: "); Serial.println(apIP); // ── DNS: redirect all queries to us ─────────────────────────────────────── dnsServer.start(DNS_PORT, "*", apIP); // ── Routes ──────────────────────────────────────────────────────────────── server.on("/", HTTP_GET, [&server]() { server.send_P(200, "text/html", CAPTIVE_PORTAL_HTML); }); server.on("/save", HTTP_POST, [&server, &config]() { if (server.hasArg("ssid") && server.hasArg("api_url")) { config.wifi_ssid = server.arg("ssid"); config.wifi_password = server.arg("pass"); config.api_url = server.arg("api_url"); config.display_id = server.arg("display_id").length() > 0 ? server.arg("display_id") : "1"; config.api_token = server.arg("api_token"); config.save(); server.send_P(200, "text/html", CAPTIVE_PORTAL_SAVED_HTML); delay(2000); ESP.restart(); } else { server.send(400, "text/plain", "Fehlende Felder: ssid und api_url sind Pflichtfelder."); } }); // ── Captive portal redirect (all unknown URLs → /) ──────────────────────── server.onNotFound([&server, &apIP]() { // Some OSes check a specific URL; redirect them all to our form server.sendHeader("Location", String("http://") + apIP.toString() + "/", true); server.send(302, "text/plain", ""); }); server.begin(); Serial.println("[Portal] HTTP server started"); // ── Main loop (blocks forever) ──────────────────────────────────────────── for (;;) { dnsServer.processNextRequest(); server.handleClient(); yield(); } }