Some experiments with captive portal

This commit is contained in:
Gunnar Skjold 2022-12-09 18:24:43 +01:00
parent 6c3ddc57b5
commit b2de6472cf
3 changed files with 27 additions and 2 deletions

View File

@ -106,6 +106,7 @@ private:
void factoryResetPost();
void notFound();
void redirectToMain();
void robotstxt();
};

View File

@ -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);
}

View File

@ -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);