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

Commit 326c62e

Browse files
authored
Merge pull request #22 from hmueller01/CaptivePortal
Captive portal
2 parents 86040e7 + 508a111 commit 326c62e

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Visual Studio Code settings
2+
.vscode
3+
14
# Prerequisites
25
*.d
36

library.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@
4242
"version": ">=1.2.3",
4343
"platforms": ["espressif8266", "espressif32"]
4444
},
45+
{
46+
"owner": "me-no-dev",
47+
"name": "ESPAsyncUDP",
48+
"version": "0.0.0-alpha+sha.697c75a025",
49+
"platforms": ["espressif8266"]
50+
},
51+
{
52+
"owner": "devyte",
53+
"name": "ESPAsyncDNSServer",
54+
"version": "^1.0.0",
55+
"platforms": ["espressif8266", "espressif32"]
56+
},
4557
{
4658
"owner": "khoih-prog",
4759
"name": "ESP_DoubleResetDetector",

src/ESPAsync_WiFiManager_Lite.h

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@
164164
#endif
165165

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

168-
#include <DNSServer.h>
169+
#include <ESPAsyncDNSServer.h>
169170
#include <memory>
170171
#undef min
171172
#undef max
@@ -544,6 +545,11 @@ class ESPAsync_WiFiManager_Lite
544545

545546
~ESPAsync_WiFiManager_Lite()
546547
{
548+
if (dnsServer)
549+
{
550+
delete dnsServer;
551+
}
552+
547553
if (server)
548554
{
549555
delete server;
@@ -818,7 +824,7 @@ class ESPAsync_WiFiManager_Lite
818824

819825
#if USING_MRD
820826
//// New MRD ////
821-
// Call the mulyi reset detector loop method every so often,
827+
// Call the multi reset detector loop method every so often,
822828
// so that it can recognise when the timeout expires.
823829
// You can also call mrd.stop() when you wish to no longer
824830
// consider the next reset as a multi reset.
@@ -940,12 +946,26 @@ class ESPAsync_WiFiManager_Lite
940946
}
941947
else if (configuration_mode)
942948
{
949+
// WiFi is connected and we are in configuration_mode
943950
configuration_mode = false;
944951
ESP_WML_LOGINFO(F("run: got WiFi back"));
952+
945953
#if USE_LED_BUILTIN
946954
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
947955
digitalWrite(LED_BUILTIN, LED_OFF);
948956
#endif
957+
958+
if (dnsServer) {
959+
dnsServer->stop();
960+
delete dnsServer;
961+
dnsServer = nullptr;
962+
}
963+
964+
if (server) {
965+
server->end();
966+
delete server;
967+
server = nullptr;
968+
}
949969
}
950970
}
951971

@@ -1255,7 +1275,8 @@ class ESPAsync_WiFiManager_Lite
12551275
private:
12561276
String ipAddress = "0.0.0.0";
12571277

1258-
AsyncWebServer *server = NULL;
1278+
AsyncWebServer *server = nullptr;
1279+
AsyncDNSServer *dnsServer = nullptr;
12591280

12601281
//KH, for ESP32
12611282
#ifdef ESP8266
@@ -2964,18 +2985,24 @@ class ESPAsync_WiFiManager_Lite
29642985

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

2967-
// Move up for ESP8266
2968-
//WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0));
2969-
29702988
if (!server)
29712989
{
29722990
server = new AsyncWebServer(HTTP_PORT);
29732991
}
29742992

2993+
if (!dnsServer)
2994+
{
2995+
dnsServer = new AsyncDNSServer();
2996+
}
2997+
29752998
//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
2976-
if (server)
2999+
if (server && dnsServer)
29773000
{
2978-
server->on("/", HTTP_GET, [this](AsyncWebServerRequest * request)
3001+
// CaptivePortal
3002+
// if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS requests
3003+
dnsServer->start(DNS_PORT, "*", portal_apIP);
3004+
// replay to all requests with same HTML
3005+
server->onNotFound([this](AsyncWebServerRequest *request)
29793006
{
29803007
handleRequest(request);
29813008
});

0 commit comments

Comments
 (0)