Add HASP_USE_CAPTIVE_PORTAL

This commit is contained in:
fvanroie 2021-09-22 22:00:20 +02:00
parent 529f42c58a
commit c8c1a31feb
2 changed files with 24 additions and 10 deletions

View File

@ -32,7 +32,11 @@
#endif #endif
#ifndef HASP_USE_WIFI #ifndef HASP_USE_WIFI
#define HASP_USE_WIFI (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0 || HASP_USE_WIFI > 0) #define HASP_USE_WIFI (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0)
#endif
#ifndef HASP_USE_CAPTIVE_PORTAL
#define HASP_USE_CAPTIVE_PORTAL (ARDUINO_ARCH_ESP32 > 0) && (HASP_USE_WIFI > 0)
#endif #endif
#define HASP_HAS_NETWORK \ #define HASP_HAS_NETWORK \

View File

@ -20,7 +20,9 @@
#if HASP_USE_HTTP > 0 #if HASP_USE_HTTP > 0
#include "sys/net/hasp_network.h" #include "sys/net/hasp_network.h"
#if(HASP_USE_CAPTIVE_PORTAL > 0) && (HASP_USE_WIFI > 0)
#include <DNSServer.h> #include <DNSServer.h>
#endif
// #ifdef USE_CONFIG_OVERRIDE // #ifdef USE_CONFIG_OVERRIDE
// #include "user_config_override.h" // #include "user_config_override.h"
@ -61,10 +63,13 @@ File fsUploadFile;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool webServerStarted = false; bool webServerStarted = false;
//Captive Portal definitions #if(HASP_USE_CAPTIVE_PORTAL > 0) && (HASP_USE_WIFI > 0)
DNSServer dnsServer; DNSServer dnsServer;
IPAddress apIP(192, 168, 4, 1); IPAddress apIP(192, 168, 4, 1);
const byte DNS_PORT = 53; #ifndef DNS_PORT
#define DNS_PORT 53
#endif // DNS_PORT
#endif // HASP_USE_CAPTIVE_PORTAL
// bool httpEnable = true; // bool httpEnable = true;
// uint16_t httpPort = 80; // uint16_t httpPort = 80;
@ -601,7 +606,7 @@ void webHandleInfoJson()
webSendPage(haspDevice.get_hostname(), htmldata.length(), false); webSendPage(haspDevice.get_hostname(), htmldata.length(), false);
webServer.sendContent(htmldata); webServer.sendContent(htmldata);
// htmldata.clear(); // htmldata.clear();
webSendFooter(); webSendFooter();
} }
@ -1420,7 +1425,8 @@ void webHandleWifiConfig()
webSendFooter(); webSendFooter();
} }
//I'm not an experienced programmer, this was the only way I managed to get it to work.. #if HASP_USE_CAPTIVE_PORTAL > 0
// I'm not an experienced programmer, this was the only way I managed to get it to work..
void webHandleCaptivePortalWifiConfig() void webHandleCaptivePortalWifiConfig()
{ // http://plate01/config/wifi { // http://plate01/config/wifi
if(!httpIsAuthenticated(F("config/wifi"))) return; if(!httpIsAuthenticated(F("config/wifi"))) return;
@ -1452,7 +1458,7 @@ void webHandleCaptivePortalWifiConfig()
} }
#endif #endif
//webServer.send(200, "text/html", httpMessage); // webServer.send(200, "text/html", httpMessage);
webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
#if defined(STM32F4xx) #if defined(STM32F4xx)
@ -1462,8 +1468,9 @@ void webHandleCaptivePortalWifiConfig()
#endif #endif
webSendFooter(); webSendFooter();
} }
#endif // HASP_USE_CAPTIVE_PORTAL
#endif #endif // HASP_USE_WIFI
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void webHandleHttpConfig() void webHandleHttpConfig()
@ -2323,15 +2330,17 @@ void httpSetup()
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
if(WiFi.getMode() != WIFI_STA) { if(WiFi.getMode() != WIFI_STA) {
#if HASP_USE_CAPTIVE_PORTAL > 0
// if DNSServer is started with "*" for domain name, it will reply with // if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request // provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", apIP); dnsServer.start(DNS_PORT, "*", apIP);
// replay to all requests with same HTML // replay to all requests with same HTML
webServer.onNotFound([]() { webServer.onNotFound([]() {
webHandleCaptivePortalWifiConfig(); webHandleCaptivePortalWifiConfig();
//webServer.send(200, "text/html", responseHTML); // webServer.send(200, "text/html", responseHTML);
//webServer.on(F("/"), webHandleWifiConfig); // webServer.on(F("/"), webHandleWifiConfig);
}); });
#endif
webServer.on(F("/"), webHandleWifiConfig); webServer.on(F("/"), webHandleWifiConfig);
LOG_TRACE(TAG_HTTP, F("Wifi access point")); LOG_TRACE(TAG_HTTP, F("Wifi access point"));
return; return;
@ -2432,8 +2441,9 @@ void httpSetup()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
IRAM_ATTR void httpLoop(void) IRAM_ATTR void httpLoop(void)
{ {
// if(http_config.enable) #if(HASP_USE_CAPTIVE_PORTAL > 0) && (HASP_USE_WIFI > 0)
dnsServer.processNextRequest(); dnsServer.processNextRequest();
#endif
webServer.handleClient(); webServer.handleClient();
} }