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

Commit 5560492

Browse files
authored
Merge pull request #24 from hmueller01/Captive_Portal
Captive portal
2 parents 2244ae0 + 465c011 commit 5560492

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

src/ESP_WiFiManager_Lite.h

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@
163163

164164
#endif
165165

166+
#define DNS_PORT 53
167+
166168
#include <DNSServer.h>
167169
#include <memory>
168170
#undef min
@@ -533,6 +535,11 @@ class ESP_WiFiManager_Lite
533535

534536
~ESP_WiFiManager_Lite()
535537
{
538+
if (dnsServer)
539+
{
540+
delete dnsServer;
541+
}
542+
536543
if (server)
537544
{
538545
delete server;
@@ -817,7 +824,7 @@ class ESP_WiFiManager_Lite
817824

818825
#if USING_MRD
819826
//// New MRD ////
820-
// Call the mulyi reset detector loop method every so often,
827+
// Call the multi reset detector loop method every so often,
821828
// so that it can recognise when the timeout expires.
822829
// You can also call mrd.stop() when you wish to no longer
823830
// consider the next reset as a multi reset.
@@ -833,6 +840,11 @@ class ESP_WiFiManager_Lite
833840
//// New DRD ////
834841
#endif
835842

843+
if (configuration_mode && dnsServer)
844+
{
845+
dnsServer->processNextRequest();
846+
}
847+
836848
if ( !configuration_mode && (curMillis > checkstatus_timeout) )
837849
{
838850
if (WiFi.status() == WL_CONNECTED)
@@ -944,12 +956,27 @@ class ESP_WiFiManager_Lite
944956
}
945957
else if (configuration_mode)
946958
{
959+
// WiFi is connected and we are in configuration_mode
947960
configuration_mode = false;
948961
ESP_WML_LOGINFO(F("run: got WiFi back"));
949962
#if USE_LED_BUILTIN
950963
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
951964
digitalWrite(LED_BUILTIN, LED_OFF);
952965
#endif
966+
967+
if (dnsServer)
968+
{
969+
dnsServer->stop();
970+
delete dnsServer;
971+
dnsServer = nullptr;
972+
}
973+
974+
if (server)
975+
{
976+
server->stop();
977+
delete server;
978+
server = nullptr;
979+
}
953980
}
954981
}
955982

@@ -1257,13 +1284,15 @@ class ESP_WiFiManager_Lite
12571284

12581285
//KH, for ESP32
12591286
#ifdef ESP8266
1260-
ESP8266WebServer *server = NULL;
1287+
ESP8266WebServer *server = nullptr;
12611288
ESP8266WiFiMulti wifiMulti;
12621289
#else //ESP32
1263-
WebServer *server = NULL;
1290+
WebServer *server = nullptr;
12641291
WiFiMulti wifiMulti;
12651292
#endif
12661293

1294+
DNSServer *dnsServer = nullptr;
1295+
12671296
bool configuration_mode = false;
12681297

12691298
unsigned long configTimeout;
@@ -2968,9 +2997,6 @@ class ESP_WiFiManager_Lite
29682997

29692998
delay(100); // ref: https://github.com/espressif/arduino-esp32/issues/985#issuecomment-359157428
29702999

2971-
// Move up for ESP8266
2972-
//WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0));
2973-
29743000
if (!server)
29753001
{
29763002
#if ESP8266
@@ -2980,10 +3006,20 @@ class ESP_WiFiManager_Lite
29803006
#endif
29813007
}
29823008

2983-
//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
2984-
if (server)
3009+
if (!dnsServer)
29853010
{
2986-
server->on("/", [this]()
3011+
dnsServer = new DNSServer();
3012+
}
3013+
3014+
//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
3015+
if (server && dnsServer)
3016+
{
3017+
// CaptivePortal
3018+
// if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS requests
3019+
dnsServer->start(DNS_PORT, "*", portal_apIP);
3020+
3021+
// reply to all requests with same HTML
3022+
server->onNotFound([this]()
29873023
{
29883024
handleRequest();
29893025
});

0 commit comments

Comments
 (0)