From a2880a4c17038b4ff3dfa083e7af7c7bbdba914a Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Wed, 4 Jun 2025 17:39:34 +0300 Subject: [PATCH] feat(ap): Add support for DHCP Captive Portal (opt 114) (#11412) * feat(ap): Add support for DHCP Captive Portal (opt 114) * feat(ap): No need to guard the function * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- .../examples/CaptivePortal/CaptivePortal.ino | 5 ++- libraries/WiFi/src/AP.cpp | 39 +++++++++++++++++++ libraries/WiFi/src/WiFiAP.h | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino b/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino index d956dc14a..7759aa09a 100644 --- a/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino +++ b/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino @@ -34,8 +34,9 @@ void handleNotFound() { void setup() { Serial.begin(115200); - WiFi.mode(WIFI_AP); - WiFi.softAP("ESP32-DNSServer"); + WiFi.AP.begin(); + WiFi.AP.create("ESP32-DNSServer"); + WiFi.AP.enableDhcpCaptivePortal(); // by default DNSServer is started serving any "*" domain name. It will reply // AccessPoint's IP to all DNS request (this is required for Captive Portal detection) diff --git a/libraries/WiFi/src/AP.cpp b/libraries/WiFi/src/AP.cpp index 0e7839764..a649c3898 100644 --- a/libraries/WiFi/src/AP.cpp +++ b/libraries/WiFi/src/AP.cpp @@ -305,6 +305,45 @@ bool APClass::enableNAPT(bool enable) { return true; } +bool APClass::enableDhcpCaptivePortal() { + esp_err_t err = ESP_OK; + static char captiveportal_uri[32] = { + 0, + }; + + if (!started()) { + log_e("AP must be first started to enable DHCP Captive Portal"); + return false; + } + + // Create Captive Portal URL: http://192.168.0.4 + strcpy(captiveportal_uri, "http://"); + strcat(captiveportal_uri, String(localIP()).c_str()); + + // Stop DHCPS + err = esp_netif_dhcps_stop(_esp_netif); + if (err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) { + log_e("DHCPS Stop Failed! 0x%04x: %s", err, esp_err_to_name(err)); + return false; + } + + // Enable DHCP Captive Portal + err = esp_netif_dhcps_option(_esp_netif, ESP_NETIF_OP_SET, ESP_NETIF_CAPTIVEPORTAL_URI, captiveportal_uri, strlen(captiveportal_uri)); + if (err) { + log_e("Could not set enable DHCP Captive Portal! 0x%x: %s", err, esp_err_to_name(err)); + return false; + } + + // Start DHCPS + err = esp_netif_dhcps_start(_esp_netif); + if (err) { + log_e("DHCPS Start Failed! 0x%04x: %s", err, esp_err_to_name(err)); + return false; + } + + return true; +} + String APClass::SSID(void) const { if (!started()) { return String(); diff --git a/libraries/WiFi/src/WiFiAP.h b/libraries/WiFi/src/WiFiAP.h index 540ec87f4..2b7ce4698 100644 --- a/libraries/WiFi/src/WiFiAP.h +++ b/libraries/WiFi/src/WiFiAP.h @@ -53,6 +53,7 @@ public: bool bandwidth(wifi_bandwidth_t bandwidth); bool enableNAPT(bool enable = true); + bool enableDhcpCaptivePortal(); String SSID(void) const; uint8_t stationCount();