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

Captive portal #24

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions src/ESP_WiFiManager_Lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@

#endif

#define DNS_PORT 53

#include <DNSServer.h>
#include <memory>
#undef min
Expand Down Expand Up @@ -533,6 +535,11 @@ class ESP_WiFiManager_Lite

~ESP_WiFiManager_Lite()
{
if (dnsServer)
{
delete dnsServer;
}

if (server)
{
delete server;
Expand Down Expand Up @@ -817,7 +824,7 @@ class ESP_WiFiManager_Lite

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

if (configuration_mode && dnsServer)
{
dnsServer->processNextRequest();
}

if ( !configuration_mode && (curMillis > checkstatus_timeout) )
{
if (WiFi.status() == WL_CONNECTED)
Expand Down Expand Up @@ -944,12 +956,27 @@ class ESP_WiFiManager_Lite
}
else if (configuration_mode)
{
// WiFi is connected and we are in configuration_mode
configuration_mode = false;
ESP_WML_LOGINFO(F("run: got WiFi back"));
#if USE_LED_BUILTIN
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
digitalWrite(LED_BUILTIN, LED_OFF);
#endif

if (dnsServer)
{
dnsServer->stop();
delete dnsServer;
dnsServer = nullptr;
}

if (server)
{
server->stop();
delete server;
server = nullptr;
}
}
}

Expand Down Expand Up @@ -1257,13 +1284,15 @@ class ESP_WiFiManager_Lite

//KH, for ESP32
#ifdef ESP8266
ESP8266WebServer *server = NULL;
ESP8266WebServer *server = nullptr;
ESP8266WiFiMulti wifiMulti;
#else //ESP32
WebServer *server = NULL;
WebServer *server = nullptr;
WiFiMulti wifiMulti;
#endif

DNSServer *dnsServer = nullptr;

bool configuration_mode = false;

unsigned long configTimeout;
Expand Down Expand Up @@ -2968,9 +2997,6 @@ class ESP_WiFiManager_Lite

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

// Move up for ESP8266
//WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0));

if (!server)
{
#if ESP8266
Expand All @@ -2980,10 +3006,20 @@ class ESP_WiFiManager_Lite
#endif
}

//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
if (server)
if (!dnsServer)
{
server->on("/", [this]()
dnsServer = new DNSServer();
}

//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
if (server && dnsServer)
{
// CaptivePortal
// if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS requests
dnsServer->start(DNS_PORT, "*", portal_apIP);

// reply to all requests with same HTML
server->onNotFound([this]()
{
handleRequest();
});
Expand Down