From 590368734d73fb40c3685e4c4e59c7631ad51eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=5FTeixeira=5F1998?= <47570249+AndreTeixeira1998@users.noreply.github.com> Date: Fri, 3 Sep 2021 15:01:13 +0100 Subject: [PATCH 1/2] added Captive Portal functionality In this commit I changed the httpsetup and httploop functions and added webHandleCaptivePortalWifiConfig to send the wifi config page when a device connects to the softAP. This will simplify the innitial wifi setup. The usercan just scan the QR code and a webpage will automatically appear without the need to enter the IP address. --- src/sys/svc/hasp_http.cpp | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 6c07e3a1..150cfb0c 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -20,6 +20,8 @@ #if HASP_USE_HTTP > 0 #include "sys/net/hasp_network.h" +#include + // #ifdef USE_CONFIG_OVERRIDE // #include "user_config_override.h" // #endif @@ -59,6 +61,11 @@ File fsUploadFile; //////////////////////////////////////////////////////////////////////////////////////////////////// bool webServerStarted = false; +//Captive Portal definitions +DNSServer dnsServer; +IPAddress apIP(192, 168, 4, 1); +const byte DNS_PORT = 53; + // bool httpEnable = true; // uint16_t httpPort = 80; // char httpUser[32] = ""; @@ -1412,6 +1419,50 @@ void webHandleWifiConfig() #endif webSendFooter(); } + +//I'm not an experienced programmer, this was the only way I managed to get it to work.. +void webHandleCaptivePortalWifiConfig() +{ // http://plate01/config/wifi + if(!httpIsAuthenticated(F("config/wifi"))) return; + + StaticJsonDocument<256> settings; + wifiGetConfig(settings.to()); + + String httpMessage((char*)0); + httpMessage.reserve(HTTP_PAGE_SIZE); + httpMessage += F("

"); + httpMessage += haspDevice.get_hostname(); + httpMessage += F("


"); + + httpMessage += F("
"); + httpMessage += F("WiFi SSID (required)
WiFi Password (required)

"); + +#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) + if(WiFi.getMode() == WIFI_STA) { + add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config"), F("")); + } +#endif + + webServer.send(200, "text/html", httpMessage); + //webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + //webServer.sendContent(httpMessage); +#if defined(STM32F4xx) + httpMessage = ""; +#else + httpMessage.clear(); +#endif + //webSendFooter(); +} + #endif //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -2272,6 +2323,15 @@ void httpSetup() #if HASP_USE_CONFIG > 0 if(WiFi.getMode() != WIFI_STA) { + // if DNSServer is started with "*" for domain name, it will reply with + // provided IP to all DNS request + dnsServer.start(DNS_PORT, "*", apIP); + // replay to all requests with same HTML + webServer.onNotFound([]() { + webHandleCaptivePortalWifiConfig(); + //webServer.send(200, "text/html", responseHTML); + //webServer.on(F("/"), webHandleWifiConfig); + }); webServer.on(F("/"), webHandleWifiConfig); LOG_TRACE(TAG_HTTP, F("Wifi access point")); return; @@ -2373,6 +2433,7 @@ void httpSetup() IRAM_ATTR void httpLoop(void) { // if(http_config.enable) + dnsServer.processNextRequest(); webServer.handleClient(); } From 0645dc48e9ffedd33f809588f2883b7ddd733c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=5FTeixeira=5F1998?= <47570249+AndreTeixeira1998@users.noreply.github.com> Date: Fri, 3 Sep 2021 17:09:33 +0100 Subject: [PATCH 2/2] fixed styling in captive portal --- src/sys/svc/hasp_http.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 150cfb0c..93f68e16 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -1452,15 +1452,15 @@ void webHandleCaptivePortalWifiConfig() } #endif - webServer.send(200, "text/html", httpMessage); - //webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); - //webServer.sendContent(httpMessage); + //webServer.send(200, "text/html", httpMessage); + webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webServer.sendContent(httpMessage); #if defined(STM32F4xx) httpMessage = ""; #else httpMessage.clear(); #endif - //webSendFooter(); + webSendFooter(); } #endif