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


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

說明

WiFi.config() 允許您配置靜態 IP 地址以及更改WiFi 防護罩上的 DNS、網關和子網地址。

與 WiFi.begin() 自動配置 WiFi shield 以使用 DHCP 不同,WiFi.config() 允許您手動設置 shield 的網絡地址。

在 WiFi.begin() 之前調用 WiFi.config() 會強製 begin() 使用 config() 中指定的網絡地址配置 WiFi 屏蔽。

您可以在 WiFi.begin() 之後調用 WiFi.config(),但在默認 DHCP 模式下,屏蔽將使用 begin() 進行初始化。一旦調用config() 方法,它將根據請求更改網絡地址。

用法

WiFi.config(ip);
WiFi.config(ip, dns);
WiFi.config(ip, dns, gateway);
WiFi.config(ip, dns, gateway, subnet);

參數

ip:設備的IP地址(4字節數組)

dns:DNS 服務器的地址。

gateway:網絡網關的 IP 地址(4 字節數組)。可選:默認為最後一個八位組設置為 1 的設備 IP 地址

subnet:網絡的子網掩碼(4 個字節的數組)。可選:默認為 255.255.255.0

返回

示例

This example shows how to set the static IP address, 192.168.0.177, of the LAN network to the WiFi shield:

#include <SPI.h>
#include <WiFi.h>

// the IP address for the shield:
IPAddress ip(192, 168, 0, 177);    

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
  }

  WiFi.config(ip);

  // 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:
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop () {}

相關用法


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