diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 6c07e3a1..93f68e16 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(); }