WiFi.setDNS()
描述
WiFi.setDNS() 用来设定 DNS(Domain Name System) 伺服器。
语法
WiFi.setDNS(dns_server1)
WiFi.setDNS(dns_server1, dns_server2)
参数
dns_server1:主要的 DNS 伺服器
dns_server2:备用的 DNS 伺服器
回传
无回传值
范例
示范如何把 DNS 设定为 Google 提供的 DNS(8.8.8.8)
#include <WiFi.h>
// DNS 的 IP 位址
IPAddress dns(8, 8, 8, 8);
char ssid[] = "yourNetwork"; // 无线网路的 SSID
char pass[] = "secretPassword"; // WPA 的密码
int status = WL_IDLE_STATUS;
void setup()
{
// 初始化序列埠并等待其开启
Serial.begin(9600);
while (!Serial) {
;
}
// 检查 WiFi Shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true);
}
// 尝试连线到无线网路
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// 连线到使用 WPA/WPA2 加密的无线网路
status = WiFi.begin(ssid, pass);
// 等待 10 秒
delay(10000);
}
WiFi.setDNS(dns);
Serial.print("Dns configured.");
}
void loop () {
}
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.
