Skip to content

Commit 7fc2caa

Browse files
authored
WiFiSTA - method setDNS as in WiFi libraries by Arduino (#9021)
https://www.arduino.cc/reference/en/libraries/wifi/wifi.setdns/
1 parent 5bd52d4 commit 7fc2caa

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,29 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress a
339339
return true;
340340
}
341341

342+
/**
343+
* Change DNS for static IP configuration
344+
* @param dns1 Static DNS server 1
345+
* @param dns2 Static DNS server 2 (optional)
346+
*/
347+
bool ESP8266WiFiSTAClass::setDNS(IPAddress dns1, IPAddress dns2) {
348+
349+
if((WiFi.getMode() & WIFI_STA) == 0)
350+
return false;
351+
352+
if(dns1.isSet()) {
353+
// Set DNS1-Server
354+
dns_setserver(0, dns1);
355+
}
356+
357+
if(dns2.isSet()) {
358+
// Set DNS2-Server
359+
dns_setserver(1, dns2);
360+
}
361+
362+
return true;
363+
}
364+
342365
/**
343366
* will force a disconnect an then start reconnecting to AP
344367
* @return ok

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class ESP8266WiFiSTAClass: public LwipIntf {
4545
//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
4646
//to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
4747
//work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
48-
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
48+
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = INADDR_ANY, IPAddress dns2 = INADDR_ANY);
49+
bool setDNS(IPAddress dns1, IPAddress dns2 = INADDR_ANY);
4950

5051
bool reconnect();
5152

0 commit comments

Comments
 (0)