Skip to content

WiFi.config handle Arduino parameters ordering and auto dns,gw,mask #8892

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

Closed
Closed
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
20 changes: 20 additions & 0 deletions libraries/WiFi/src/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subne
if(!WiFi.enableSTA(true)) {
return false;
}
// handle Arduino ordering of parameters: ip, dns, gw, subnet
if (local_ip != INADDR_NONE && subnet[0] != 255) {
IPAddress tmp = dns1;
dns1 = gateway;
gateway = subnet;
subnet = (tmp != INADDR_NONE) ? tmp : IPAddress(255, 255, 255, 0);
}
err = set_esp_interface_ip(ESP_IF_WIFI_STA, local_ip, gateway, subnet);
if(err == ESP_OK){
err = set_esp_interface_dns(ESP_IF_WIFI_STA, dns1, dns2);
Expand All @@ -431,6 +438,19 @@ bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subne
return err == ESP_OK;
}

bool WiFiSTAClass::config(IPAddress local_ip, IPAddress dns) {

if (local_ip == INADDR_NONE)
return config(INADDR_NONE, INADDR_NONE, INADDR_NONE);

IPAddress gw(local_ip);
gw[3] = 1;
if (dns == INADDR_NONE) {
dns = gw;
}
return config(local_ip, gw, IPAddress(255, 255, 255, 0), dns);
}

/**
* Sets the working bandwidth of the STA mode
* @param m wifi_bandwidth_t
Expand Down
5 changes: 5 additions & 0 deletions libraries/WiFi/src/WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ class WiFiSTAClass
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin();

// also accepts Arduino ordering of parameters: ip, dns, gw, mask
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);

// two and one parameter version. 2nd parameter is DNS like in Arduino
bool config(IPAddress local_ip, IPAddress dns = (uint32_t)0x00000000);

bool setDNS(IPAddress dns1, IPAddress dns2 = (uint32_t)0x00000000); // sets DNS IP for all network interfaces

bool bandwidth(wifi_bandwidth_t bandwidth);
Expand Down