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

Commit 5ca5519

Browse files
committed
added Captive Portal
1 parent fb35f3a commit 5ca5519

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;
@@ -811,7 +818,7 @@ class ESP_WiFiManager_Lite
811818

812819
#if USING_MRD
813820
//// New MRD ////
814-
// Call the mulyi reset detector loop method every so often,
821+
// Call the multi reset detector loop method every so often,
815822
// so that it can recognise when the timeout expires.
816823
// You can also call mrd.stop() when you wish to no longer
817824
// consider the next reset as a multi reset.
@@ -827,6 +834,11 @@ class ESP_WiFiManager_Lite
827834
//// New DRD ////
828835
#endif
829836

837+
if (configuration_mode && dnsServer)
838+
{
839+
dnsServer->processNextRequest();
840+
}
841+
830842
if ( !configuration_mode && (curMillis > checkstatus_timeout) )
831843
{
832844
if (WiFi.status() == WL_CONNECTED)
@@ -936,10 +948,25 @@ class ESP_WiFiManager_Lite
936948
}
937949
else if (configuration_mode)
938950
{
951+
// WiFi is connected and we are in configuration_mode
939952
configuration_mode = false;
940953
ESP_WML_LOGINFO(F("run: got WiFi back"));
941954
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
942955
digitalWrite(LED_BUILTIN, LED_OFF);
956+
957+
if (dnsServer)
958+
{
959+
dnsServer->stop();
960+
delete dnsServer;
961+
dnsServer = nullptr;
962+
}
963+
964+
if (server)
965+
{
966+
server->end();
967+
delete server;
968+
server = nullptr;
969+
}
943970
}
944971
}
945972

@@ -1249,13 +1276,15 @@ class ESP_WiFiManager_Lite
12491276

12501277
//KH, for ESP32
12511278
#ifdef ESP8266
1252-
ESP8266WebServer *server = NULL;
1279+
ESP8266WebServer *server = nullptr;
12531280
ESP8266WiFiMulti wifiMulti;
12541281
#else //ESP32
1255-
WebServer *server = NULL;
1282+
WebServer *server = nullptr;
12561283
WiFiMulti wifiMulti;
12571284
#endif
12581285

1286+
DNSServer *dnsServer = nullptr;
1287+
12591288
bool configuration_mode = false;
12601289

12611290
unsigned long configTimeout;
@@ -2963,9 +2992,6 @@ class ESP_WiFiManager_Lite
29632992

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

2966-
// Move up for ESP8266
2967-
//WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0));
2968-
29692995
if (!server)
29702996
{
29712997
#if ESP8266
@@ -2975,10 +3001,20 @@ class ESP_WiFiManager_Lite
29753001
#endif
29763002
}
29773003

2978-
//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
2979-
if (server)
3004+
if (!dnsServer)
29803005
{
2981-
server->on("/", [this]()
3006+
dnsServer = new DNSServer();
3007+
}
3008+
3009+
//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
3010+
if (server && dnsServer)
3011+
{
3012+
// CaptivePortal
3013+
// if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS requests
3014+
dnsServer->start(DNS_PORT, "*", portal_apIP);
3015+
3016+
// reply to all requests with same HTML
3017+
server->onNotFound([this]()
29823018
{
29833019
handleRequest();
29843020
});

0 commit comments

Comments
 (0)