Skip to content

Commit 3c1885f

Browse files
JAndrassyme-no-dev
andauthored
WiFi.config handle Arduino parameters ordering and auto dns,gw,mask (#9425)
Co-authored-by: Me No Dev <[email protected]>
1 parent 0523b94 commit 3c1885f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

libraries/WiFi/src/WiFiSTA.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,35 @@ bool WiFiSTAClass::eraseAP(void) {
169169
*/
170170
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
171171
{
172+
// handle Arduino ordering of parameters: ip, dns, gw, subnet
173+
if (local_ip.type() == IPv4 && local_ip != INADDR_NONE && subnet[0] != 255) {
174+
IPAddress tmp = dns1;
175+
dns1 = gateway;
176+
gateway = subnet;
177+
subnet = (tmp != INADDR_NONE) ? tmp : IPAddress(255, 255, 255, 0);
178+
}
179+
172180
return STA.config(local_ip, gateway, subnet, dns1, dns2);
173181
}
174182

183+
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress dns) {
184+
185+
if (local_ip == INADDR_NONE) {
186+
return config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
187+
}
188+
189+
if (local_ip.type() != IPv4) {
190+
return false;
191+
}
192+
193+
IPAddress gw(local_ip);
194+
gw[3] = 1;
195+
if (dns == INADDR_NONE) {
196+
dns = gw;
197+
}
198+
return config(local_ip, gw, IPAddress(255, 255, 255, 0), dns);
199+
}
200+
175201
/**
176202
* Change DNS server for static IP configuration
177203
* @param dns1 Static DNS server 1

libraries/WiFi/src/WiFiSTA.h

+4
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@ class WiFiSTAClass
116116
}
117117
wl_status_t begin();
118118

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

122+
// two and one parameter version. 2nd parameter is DNS like in Arduino
123+
bool config(IPAddress local_ip, IPAddress dns = (uint32_t)0x00000000);
124+
121125
bool setDNS(IPAddress dns1, IPAddress dns2 = (uint32_t)0x00000000); // sets DNS IP for all network interfaces
122126

123127
bool bandwidth(wifi_bandwidth_t bandwidth);

0 commit comments

Comments
 (0)