The WiFi class contains Wi-Fi specific functions such as initializing the network settings as well as connecting to & disconnecting from a network.
WiFi.begin()Initializes the WiFi library's network settings and provides the current status.
WiFi.begin(ssid);
WiFi.begin(ssid, pass);
WiFi.begin(ssid, keyIndex, key);
ssid: the SSID (Service Set Identifier) is the name of the WiFi network you want to connect to.
keyIndex: WEP encrypted networks can hold up to 4 different keys. This identifies which key you are going to use.
key: a hexadecimal string used as a security code for WEP encrypted networks.
pass: WPA encrypted networks use a password in the form of a string for security.
WL_CONNECTED when connected to a network WL_IDLE_STATUS when not connected to a network, but powered on
#include <WiFi.h>
//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";
void setup()
{
WiFi.begin(ssid, pass);
}
void loop () {}
WiFi.disconnect()Disconnects the WiFi shield from the current network.
WiFi.disconnect();
none
nothing
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.setDNS()WiFi.setDNS() allows you to configure the DNS (Domain Name System) server.
WiFi.setDNS(dns_server1)
WiFi.setDNS(dns_server1, dns_server2)
dns_server1: the IP address of the primary DNS server
dns_server2: the IP address of the secondary DNS server
Nothing
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 <WiFi.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 () {
}
WiFi.SSID()Gets the SSID of the current network
WiFi.SSID();
WiFi.SSID(wifiAccessPoint)
wifiAccessPoint: specifies from which network to get the information
A string containing the SSID the WiFi shield is currently connected to.
string containing name of network requested.
#include <SPI.h>
#include <WiFi.h>
//SSID of your network
char ssid[] = "yourNetwork";
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup()
{
// initialize serial:
Serial.begin(9600);
// scan for existing networks:
Serial.println("Scanning available networks...");
scanNetworks();
// attempt to connect using WEP encryption:
Serial.println("Attempting to connect to open network...");
status = WiFi.begin(ssid);
Serial.print("SSID: ");
Serial.println(ssid);
}
void loop () {}
void scanNetworks() {
// scan for nearby networks:
Serial.println("** Scan Networks **");
byte numSsid = WiFi.scanNetworks();
// print the list of networks seen:
Serial.print("SSID List:");
Serial.println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet<numSsid; thisNet++) {
Serial.print(thisNet);
Serial.print(") Network: ");
Serial.println(WiFi.SSID(thisNet));
}
}
WiFi.BSSID()Gets the MAC address of the routher you are connected to
WiFi.BSSID(bssid);
bssid : 6 byte array
A byte array containing the MAC address of the router the WiFi shield is currently connected to.
#include <WiFi.h>
//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";
void setup()
{
WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// if you are connected, print out info about the connection:
else {
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5],HEX);
Serial.print(":");
Serial.print(bssid[4],HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);
}
}
void loop () {}
WiFi.RSSI()Gets the signal strength of the connection to the router
WiFi.RSSI();
WiFi.RSSI(wifiAccessPoint);
wifiAccessPoint: specifies from which network to get the information
long : the current RSSI /Received Signal Strength in dBm
#include <SPI.h>
#include <WiFi.h>
//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";
void setup()
{
WiFi.begin(ssid, pass);
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// if you are connected, print out info about the connection:
else {
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("RSSI:");
Serial.println(rssi);
}
}
void loop () {}
WiFi.encryptionType()Gets the encryption type of the current network
WiFi.encryptionType();
WiFi.encryptionType(wifiAccessPoint);
wifiAccessPoint: specifies which network to get information from
byte : value represents the type of encryption
TKIP (WPA) = 2 WEP = 5 CCMP (WPA) = 4 NONE = 7 AUTO = 8
#include <SPI.h>
#include <WiFi.h>
//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";
void setup()
{
WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// if you are connected, print out info about the connection:
else {
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption,HEX);
}
}
void loop () {}
WiFi.scanNetworks()Scans for available WiFi networks and returns the discovered number
WiFi.scanNetworks();
none
byte : number of discovered networks
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // the name of your network
void setup() {
Serial.begin(9600);
int status = WiFi.begin(ssid);
if (status != WL_CONNECTED) {
Serial.println("Couldn't get a WiFi connection");
while (true);
}
else {
// if you are connected, scan for available WiFi networks and print the number discovered:
Serial.println("** Scan Networks **");
byte numSsid = WiFi.scanNetworks();
Serial.print("Number of available WiFi networks discovered:");
Serial.println(numSsid);
}
}
void loop() {}
WiFi.status()Return the connection status.
WiFi.status();
none
WL_CONNECTED: assigned when connected to a WiFi network; WL_NO_SHIELD: assigned when no WiFi shield is present; WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED); WL_NO_SSID_AVAIL: assigned when no SSID are available; WL_SCAN_COMPLETED: assigned when the scan networks is completed; WL_CONNECT_FAILED: assigned when the connection fails for all the attempts; WL_CONNECTION_LOST: assigned when the connection is lost; WL_DISCONNECTED: assigned when disconnected from a network;
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key
int keyIndex = 0; // your network key Index number
int status = WL_IDLE_STATUS; // the Wifi radio's 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");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, keyIndex, key);
// wait 10 seconds for connection:
delay(10000);
}
// once you are connected :
Serial.print("You're connected to the network");
}
void loop() {
// check the network status connection once every 10 seconds:
delay(10000);
Serial.println(WiFi.status());
}
WiFi.getSocket()gets the first socket available
WiFi.getSocket();
none
int : the first socket available
WiFi.macAddress()Gets the MAC Address of your WiFi shield
WiFi.macAddress(mac);
mac: a 6 byte array to hold the MAC address
byte array : 6 bytes representing the MAC address of your shield
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // the name of your network
int status = WL_IDLE_STATUS; // the Wifi radio's status
byte mac[6]; // the MAC address of your Wifi shield
void setup()
{
Serial.begin(9600);
status = WiFi.begin(ssid);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// if you are connected, print your MAC address:
else {
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}
}
void loop () {}