Skip to content

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

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 2 commits into from
Apr 2, 2024
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
26 changes: 26 additions & 0 deletions libraries/WiFi/src/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,35 @@ bool WiFiSTAClass::eraseAP(void) {
*/
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
{
// handle Arduino ordering of parameters: ip, dns, gw, subnet
if (local_ip.type() == IPv4 && local_ip != INADDR_NONE && subnet[0] != 255) {
IPAddress tmp = dns1;
dns1 = gateway;
gateway = subnet;
subnet = (tmp != INADDR_NONE) ? tmp : IPAddress(255, 255, 255, 0);
}

return STA.config(local_ip, gateway, subnet, dns1, dns2);
}

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

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

if (local_ip.type() != IPv4) {
return false;
}

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

/**
* Change DNS server for static IP configuration
* @param dns1 Static DNS server 1
Expand Down
4 changes: 4 additions & 0 deletions libraries/WiFi/src/WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ class WiFiSTAClass
}
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