Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit b23116f

Browse files
committed
implemented CaptivePortal
1 parent e6d78cc commit b23116f

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/ESPAsync_WiFiManager_Lite.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
#endif
165165

166166
#define HTTP_PORT 80
167+
#define DNS_PORT 53
167168

168169
#include <DNSServer.h>
169170
#include <memory>
@@ -828,6 +829,11 @@ class ESPAsync_WiFiManager_Lite
828829
//// New DRD ////
829830
#endif
830831

832+
if ( configuration_mode && dnsServer)
833+
{
834+
dnsServer->processNextRequest();
835+
}
836+
831837
if ( !configuration_mode && (curMillis > checkstatus_timeout) )
832838
{
833839
if (WiFi.status() == WL_CONNECTED)
@@ -936,6 +942,16 @@ class ESPAsync_WiFiManager_Lite
936942
ESP_WML_LOGINFO(F("run: got WiFi back"));
937943
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
938944
digitalWrite(LED_BUILTIN, LED_OFF);
945+
if (dnsServer) {
946+
dnsServer->stop();
947+
delete dnsServer;
948+
dnsServer = nullptr;
949+
}
950+
if (server) {
951+
server->end();
952+
delete server;
953+
server = nullptr;
954+
}
939955
}
940956
}
941957

@@ -1248,6 +1264,7 @@ class ESPAsync_WiFiManager_Lite
12481264
String ipAddress = "0.0.0.0";
12491265

12501266
AsyncWebServer *server = NULL;
1267+
DNSServer *dnsServer = nullptr;
12511268

12521269
//KH, for ESP32
12531270
#ifdef ESP8266
@@ -2967,10 +2984,21 @@ class ESPAsync_WiFiManager_Lite
29672984
server = new AsyncWebServer(HTTP_PORT);
29682985
}
29692986

2987+
if (!dnsServer)
2988+
{
2989+
dnsServer = new DNSServer();
2990+
}
2991+
29702992
//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
29712993
if (server)
29722994
{
2973-
server->on("/", HTTP_GET, [this](AsyncWebServerRequest * request)
2995+
// CaptivePortal
2996+
// if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS requests
2997+
dnsServer->start(DNS_PORT, "*", portal_apIP);
2998+
//server->addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER); //only when requested from AP
2999+
// replay to all requests with same HTML
3000+
//server->on("/", HTTP_GET, [this](AsyncWebServerRequest * request)
3001+
server->onNotFound([this](AsyncWebServerRequest *request)
29743002
{
29753003
handleRequest(request);
29763004
});

0 commit comments

Comments
 (0)