Skip to content

feat(ap): Add support for DHCP Captive Portal (opt 114) #11412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions libraries/WiFi/src/AP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions libraries/WiFi/src/WiFiAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class APClass : public NetworkInterface {

bool bandwidth(wifi_bandwidth_t bandwidth);
bool enableNAPT(bool enable = true);
bool enableDhcpCaptivePortal();

String SSID(void) const;
uint8_t stationCount();
Expand Down
Loading