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.