From b2de6472cf4c07d480cb342f735866c4ca601f0e Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Fri, 9 Dec 2022 18:24:43 +0100 Subject: [PATCH] Some experiments with captive portal --- lib/SvelteUi/include/AmsWebServer.h | 1 + lib/SvelteUi/src/AmsWebServer.cpp | 15 +++++++++++++-- src/AmsToMqttBridge.ino | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/SvelteUi/include/AmsWebServer.h b/lib/SvelteUi/include/AmsWebServer.h index b7e708ca..fb747065 100644 --- a/lib/SvelteUi/include/AmsWebServer.h +++ b/lib/SvelteUi/include/AmsWebServer.h @@ -106,6 +106,7 @@ private: void factoryResetPost(); void notFound(); + void redirectToMain(); void robotstxt(); }; diff --git a/lib/SvelteUi/src/AmsWebServer.cpp b/lib/SvelteUi/src/AmsWebServer.cpp index cb848c12..a17573cb 100644 --- a/lib/SvelteUi/src/AmsWebServer.cpp +++ b/lib/SvelteUi/src/AmsWebServer.cpp @@ -48,7 +48,6 @@ void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, Meter this->ds = ds; this->ea = ea; - server.on(F("/"), HTTP_GET, std::bind(&AmsWebServer::indexHtml, this)); snprintf_P(buf, 32, PSTR("/index-%s.js"), VERSION); server.on(buf, HTTP_GET, std::bind(&AmsWebServer::indexJs, this)); @@ -91,7 +90,14 @@ void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, Meter server.on(F("/mqtt-key"), HTTP_POST, std::bind(&AmsWebServer::firmwarePost, this), std::bind(&AmsWebServer::mqttKeyUpload, this)); server.on(F("/configfile"), HTTP_POST, std::bind(&AmsWebServer::firmwarePost, this), std::bind(&AmsWebServer::configFileUpload, this)); - server.on(F("/configfile.cfg"),HTTP_GET, std::bind(&AmsWebServer::configFileDownload, this)); + server.on(F("/configfile.cfg"), HTTP_GET, std::bind(&AmsWebServer::configFileDownload, this)); + + /* These trigger captive portal. Only problem is that after you have "signed in", the portal is closed and the user has no idea how to reach the device + server.on(F("/generate_204"), HTTP_GET, std::bind(&AmsWebServer::redirectToMain, this)); // Android captive portal check: http://connectivitycheck.gstatic.com/generate_204 + server.on(F("/ncsi.txt"), HTTP_GET, std::bind(&AmsWebServer::redirectToMain, this)); // Microsoft connectivity check: http://www.msftncsi.com/ncsi.txt + server.on(F("/fwlink"), HTTP_GET, std::bind(&AmsWebServer::redirectToMain, this)); // Microsoft connectivity check + server.on(F("/library/test/success.html"), HTTP_GET, std::bind(&AmsWebServer::redirectToMain, this)); // Apple connectivity check: http://www.apple.com/library/test/success.html + */ server.onNotFound(std::bind(&AmsWebServer::notFound, this)); @@ -1988,3 +1994,8 @@ void AmsWebServer::configFileUpload() { server.send(303); } } + +void AmsWebServer::redirectToMain() { + server.sendHeader("Location","/"); + server.send(302); +} \ No newline at end of file diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 23b645db..8b180345 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -755,6 +755,19 @@ void swapWifiMode() { if (mode != WIFI_AP || !config.hasConfig()) { if(Debug.isActive(RemoteDebug::INFO)) debugI("Swapping to AP mode"); + + //wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, 0); // Disable default gw + + /* Example code to set captive portal option in DHCP + auto& server = WiFi.softAPDhcpServer(); + server.onSendOptions([](const DhcpServer& server, auto& options) { + // Captive Portal URI + const IPAddress gateway = netif_ip4_addr(server.getNetif()); + const String captive = F("http://") + gateway.toString(); + options.add(114, captive.c_str(), captive.length()); + }); + */ + WiFi.softAP(PSTR("AMS2MQTT")); WiFi.mode(WIFI_AP);