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>
This commit is contained in:
Me No Dev 2025-06-04 17:39:34 +03:00 committed by GitHub
parent b5c5655cf0
commit a2880a4c17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 2 deletions

View file

@ -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)

View file

@ -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();

View file

@ -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();