當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Arduino WiFi101 - WiFi.setDNS()用法及代碼示例

說明

WiFi.setDNS() 允許您配置 DNS(域名係統)服務器。

用法

WiFi.setDNS(dns_server1)
WiFi.setDNS(dns_server1, dns_server2)

參數

dns_server1:主DNS服務器的IP地址

dns_server2:從DNS服務器的IP地址

返回

示例

This example shows how to set the Google DNS (8.8.8.8). You can set it as an object IPAddress.

#include <SPI.h>
#include <WiFi101.h>

// the IP address for the shield:
IPAddress dns(8, 8, 8, 8);  //Google dns  

char ssid[] = "yourNetwork";    // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

void setup()
{  
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while(true);  // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // print your WiFi shield's IP address:
  WiFi.setDNS(dns);
  Serial.print("Dns configured.");
}

void loop () {
}
 

相關用法


注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 WiFi101 - WiFi.setDNS()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。