“WiFi.config()”的版本间的差异

来自YFRobotwiki
跳转至: 导航搜索
(以“:WiFi {| border="0" cellpadding="20" width="100%" |- |width="100%" valign="top" align="left"| <font color="olivedrab" size="+3">'''config()'''</font> ...”为内容创建页面)
 
 
(未显示1个用户的1个中间版本)
第5行: 第5行:
 
|width="100%" valign="top" align="left"|
 
|width="100%" valign="top" align="left"|
 
<font color="olivedrab" size="+3">'''config()'''</font>
 
<font color="olivedrab" size="+3">'''config()'''</font>
 
  
  
 
<font color="orange" size="+1">'''描述'''</font>
 
<font color="orange" size="+1">'''描述'''</font>
  
WiFi.config() allows you to configure a static IP address as well as change the DNS, gateway, and subnet addresses on the WiFi shield.
 
  
Unlike WiFi.begin() which automatically configures the WiFi shield to use DHCP, WiFi.config() allows you to manually set the network address of the shield.
+
<span style="background:lightblue">WiFi.config()</span> allows you to configure a static IP address as well as change the DNS, gateway, and subnet addresses on the WiFi shield.
 +
 
 +
 
 +
Unlike <span style="background:lightblue">WiFi.begin()</span> which automatically configures the WiFi shield to use DHCP, <span style="background:lightblue">WiFi.config()</span> allows you to manually set the network address of the shield.
 +
 
 +
 
 +
Calling <span style="background:lightblue">WiFi.config()</span> before <span style="background:lightblue">WiFi.begin()</span> forces <span style="background:lightblue">begin()</span> to configure the WiFi shield with the network addresses specified in <span style="background:lightblue">config()</span>.
 +
 
  
Calling WiFi.config() before WiFi.begin() forces begin() to configure the WiFi shield with the network addresses specified in config().
+
You can call <span style="background:lightblue">WiFi.config()</span> after <span style="background:lightblue">WiFi.begin()</span>, but the shield will initialize with <span style="background:lightblue">begin()</span> in the default DHCP mode. Once the <span style="background:lightblue">config()</span> method is called, it will change the network address as requested.
  
You can call WiFi.config() after WiFi.begin(), but the shield will initialize with begin() in the default DHCP mode. Once the config() method is called, it will change the network address as requested.
 
  
 
<font color="orange" size="+1">'''语法'''</font>
 
<font color="orange" size="+1">'''语法'''</font>
第31行: 第35行:
 
<font color="orange" size="+1">'''参数'''</font>
 
<font color="orange" size="+1">'''参数'''</font>
  
ip: the IP address of the device (array of 4 bytes)
+
'''ip''': the IP address of the device (array of 4 bytes)
  
dns: the address for a DNS server.
+
'''dns''': the address for a DNS server.
  
gateway: the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1
+
'''gateway''': the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1
  
subnet: the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0
+
'''subnet''': the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0
  
  

2016年4月20日 (三) 08:55的最后版本

WiFi

config()


描述


WiFi.config() allows you to configure a static IP address as well as change the DNS, gateway, and subnet addresses on the WiFi shield.


Unlike WiFi.begin() which automatically configures the WiFi shield to use DHCP, WiFi.config() allows you to manually set the network address of the shield.


Calling WiFi.config() before WiFi.begin() forces begin() to configure the WiFi shield with the network addresses specified in config().


You can call WiFi.config() after WiFi.begin(), but the shield will initialize with begin() in the default DHCP mode. Once the config() method is called, it will change the network address as requested.


语法

WiFi.config(ip);

WiFi.config(ip, dns);

WiFi.config(ip, dns, gateway);

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


参数

ip: the IP address of the device (array of 4 bytes)

dns: the address for a DNS server.

gateway: the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1

subnet: the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0


返回值

Nothing


示例

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 () {}

返回WiFi库

返回Arduino库菜单

返回首页

更多建议和问题欢迎反馈至 YFRobot论坛