diff --git a/docs/api.md b/docs/api.md index ff5ac52a..334cb371 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1720,6 +1720,71 @@ void setup() void loop () {} ``` +### `WiFi.dnsIP()` + +#### Description + +Returns the DNS server IP address for the device. + + +#### Syntax + +``` +WiFi.dnsIP() +WiFi.dnsIP(n) + +``` + +#### Parameters +optional parameter n for the number of the DNS server to get the second DNS serverv + +#### Returns +- the DNS server IP address for the device (IPAddress). + +#### Example + +``` +#include + +#include "arduino_secrets.h" +char ssid[] = SECRET_SSID; +char pass[] = SECRET_PASS; + +IPAddress emptyIP; + +void setup() { + + Serial.begin(115200); + while (!Serial) {} + + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + int status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a WiFi connection"); + while(true); + } + + Serial.print("DHCP assigned DNS server: "); + IPAddress dns1 = WiFi.dnsIP(); + if (dns1 == emptyIP) { + Serial.println("not set"); + } else { + dns1.printTo(Serial); + Serial.println(); + IPAddress dns2 = WiFi.dnsIP(1); + if (dns2 != emptyIP) { + Serial.print("DNS server2: "); + dns2.printTo(Serial); + Serial.println(); + } + } + +} + +void loop() { +} +``` ### `WiFi.getTime()` #### Description diff --git a/src/WiFi.cpp b/src/WiFi.cpp index 264194e2..77e4ede7 100644 --- a/src/WiFi.cpp +++ b/src/WiFi.cpp @@ -266,6 +266,16 @@ IPAddress WiFiClass::gatewayIP() return ret; } +IPAddress WiFiClass::dnsIP(int n) +{ + if (n > 1) + return IPAddress(0,0,0,0); + IPAddress dnsip0; + IPAddress dnsip1; + WiFiDrv::getDNS(dnsip0, dnsip1); + return n ? dnsip1 : dnsip0; +} + const char* WiFiClass::SSID() { return WiFiDrv::getCurrentSSID(); diff --git a/src/WiFi.h b/src/WiFi.h index b4518df7..e6a16c1c 100644 --- a/src/WiFi.h +++ b/src/WiFi.h @@ -178,6 +178,14 @@ class WiFiClass */ IPAddress gatewayIP(); + /* + * Get the DNS server IP address. + * param n: index of the DNS server + * return: DNS server IP address value + * requires firmware version > 1.5.0 + */ + IPAddress dnsIP(int n = 0); + /* * Return the current SSID associated with the network * diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index a0920077..c16656f6 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -418,6 +418,39 @@ void WiFiDrv::getIpAddress(IPAddress& ip) ip = _gatewayIp; } + void WiFiDrv::getDNS(IPAddress& dnsip0, IPAddress& dnsip1) + { + uint8_t ip0[WL_IPV4_LENGTH] = {0}; + uint8_t ip1[WL_IPV4_LENGTH] = {0}; + + tParam params[PARAM_NUMS_2] = { {0, (char*)ip0}, {0, (char*)ip1}}; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_DNS_CONFIG_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + SpiDrv::waitResponseParams(GET_DNS_CONFIG_CMD, PARAM_NUMS_2, params); + + SpiDrv::spiSlaveDeselect(); + + dnsip0 = ip0; + dnsip1 = ip1; + } + const char* WiFiDrv::getCurrentSSID() { WAIT_FOR_SLAVE_SELECT(); diff --git a/src/utility/wifi_drv.h b/src/utility/wifi_drv.h index c20db43b..2d1872f4 100644 --- a/src/utility/wifi_drv.h +++ b/src/utility/wifi_drv.h @@ -181,6 +181,13 @@ class WiFiDrv */ static void getGatewayIP(IPAddress& ip); + /* + * Get the DNS servers IP addresses. + * + * return: copy the DNS servers IP addresses into IPAddress objects + */ + static void getDNS(IPAddress& dnsip0, IPAddress& dnsip1); + /* * Return the current SSID associated with the network * diff --git a/src/utility/wifi_spi.h b/src/utility/wifi_spi.h index a11f145e..96aa00b2 100644 --- a/src/utility/wifi_spi.h +++ b/src/utility/wifi_spi.h @@ -56,6 +56,7 @@ enum { SET_AP_PASSPHRASE_CMD = 0x19, SET_DEBUG_CMD = 0x1A, GET_TEMPERATURE_CMD = 0x1B, + GET_DNS_CONFIG_CMD = 0x1E, GET_REASON_CODE_CMD = 0x1F, GET_CONN_STATUS_CMD = 0x20,