Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 37 additions & 0 deletions libraries/WiFi/src/AP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,43 @@ 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