Skip to content

Commit 6a86d00

Browse files
authored
Merge pull request #251 from JAndrassy/dns_ip
DNS server IP getter dnsIP()
2 parents 89ca316 + 7a069ef commit 6a86d00

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

docs/api.md

+65
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,71 @@ void setup()
17201720
void loop () {}
17211721
```
17221722

1723+
### `WiFi.dnsIP()`
1724+
1725+
#### Description
1726+
1727+
Returns the DNS server IP address for the device.
1728+
1729+
1730+
#### Syntax
1731+
1732+
```
1733+
WiFi.dnsIP()
1734+
WiFi.dnsIP(n)
1735+
1736+
```
1737+
1738+
#### Parameters
1739+
optional parameter n for the number of the DNS server to get the second DNS serverv
1740+
1741+
#### Returns
1742+
- the DNS server IP address for the device (IPAddress).
1743+
1744+
#### Example
1745+
1746+
```
1747+
#include <WiFiNINA.h>
1748+
1749+
#include "arduino_secrets.h"
1750+
char ssid[] = SECRET_SSID;
1751+
char pass[] = SECRET_PASS;
1752+
1753+
IPAddress emptyIP;
1754+
1755+
void setup() {
1756+
1757+
Serial.begin(115200);
1758+
while (!Serial) {}
1759+
1760+
Serial.print("Attempting to connect to SSID: ");
1761+
Serial.println(ssid);
1762+
int status = WiFi.begin(ssid, pass);
1763+
if ( status != WL_CONNECTED) {
1764+
Serial.println("Couldn't get a WiFi connection");
1765+
while(true);
1766+
}
1767+
1768+
Serial.print("DHCP assigned DNS server: ");
1769+
IPAddress dns1 = WiFi.dnsIP();
1770+
if (dns1 == emptyIP) {
1771+
Serial.println("not set");
1772+
} else {
1773+
dns1.printTo(Serial);
1774+
Serial.println();
1775+
IPAddress dns2 = WiFi.dnsIP(1);
1776+
if (dns2 != emptyIP) {
1777+
Serial.print("DNS server2: ");
1778+
dns2.printTo(Serial);
1779+
Serial.println();
1780+
}
1781+
}
1782+
1783+
}
1784+
1785+
void loop() {
1786+
}
1787+
```
17231788
### `WiFi.getTime()`
17241789

17251790
#### Description

src/WiFi.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ IPAddress WiFiClass::gatewayIP()
266266
return ret;
267267
}
268268

269+
IPAddress WiFiClass::dnsIP(int n)
270+
{
271+
if (n > 1)
272+
return IPAddress(0,0,0,0);
273+
IPAddress dnsip0;
274+
IPAddress dnsip1;
275+
WiFiDrv::getDNS(dnsip0, dnsip1);
276+
return n ? dnsip1 : dnsip0;
277+
}
278+
269279
const char* WiFiClass::SSID()
270280
{
271281
return WiFiDrv::getCurrentSSID();

src/WiFi.h

+8
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ class WiFiClass
178178
*/
179179
IPAddress gatewayIP();
180180

181+
/*
182+
* Get the DNS server IP address.
183+
* param n: index of the DNS server
184+
* return: DNS server IP address value
185+
* requires firmware version > 1.5.0
186+
*/
187+
IPAddress dnsIP(int n = 0);
188+
181189
/*
182190
* Return the current SSID associated with the network
183191
*

src/utility/wifi_drv.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,39 @@ void WiFiDrv::getIpAddress(IPAddress& ip)
418418
ip = _gatewayIp;
419419
}
420420

421+
void WiFiDrv::getDNS(IPAddress& dnsip0, IPAddress& dnsip1)
422+
{
423+
uint8_t ip0[WL_IPV4_LENGTH] = {0};
424+
uint8_t ip1[WL_IPV4_LENGTH] = {0};
425+
426+
tParam params[PARAM_NUMS_2] = { {0, (char*)ip0}, {0, (char*)ip1}};
427+
428+
WAIT_FOR_SLAVE_SELECT();
429+
430+
// Send Command
431+
SpiDrv::sendCmd(GET_DNS_CONFIG_CMD, PARAM_NUMS_1);
432+
433+
uint8_t _dummy = DUMMY_DATA;
434+
SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM);
435+
436+
// pad to multiple of 4
437+
SpiDrv::readChar();
438+
SpiDrv::readChar();
439+
440+
SpiDrv::spiSlaveDeselect();
441+
//Wait the reply elaboration
442+
SpiDrv::waitForSlaveReady();
443+
SpiDrv::spiSlaveSelect();
444+
445+
// Wait for reply
446+
SpiDrv::waitResponseParams(GET_DNS_CONFIG_CMD, PARAM_NUMS_2, params);
447+
448+
SpiDrv::spiSlaveDeselect();
449+
450+
dnsip0 = ip0;
451+
dnsip1 = ip1;
452+
}
453+
421454
const char* WiFiDrv::getCurrentSSID()
422455
{
423456
WAIT_FOR_SLAVE_SELECT();

src/utility/wifi_drv.h

+7
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ class WiFiDrv
181181
*/
182182
static void getGatewayIP(IPAddress& ip);
183183

184+
/*
185+
* Get the DNS servers IP addresses.
186+
*
187+
* return: copy the DNS servers IP addresses into IPAddress objects
188+
*/
189+
static void getDNS(IPAddress& dnsip0, IPAddress& dnsip1);
190+
184191
/*
185192
* Return the current SSID associated with the network
186193
*

src/utility/wifi_spi.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum {
5656
SET_AP_PASSPHRASE_CMD = 0x19,
5757
SET_DEBUG_CMD = 0x1A,
5858
GET_TEMPERATURE_CMD = 0x1B,
59+
GET_DNS_CONFIG_CMD = 0x1E,
5960
GET_REASON_CODE_CMD = 0x1F,
6061

6162
GET_CONN_STATUS_CMD = 0x20,

0 commit comments

Comments
 (0)