WiFi Reference
#include <WiFI.h>
WiFi - WiFi.
begin()
Description
Initializes the WiFi library’s network settings and provides the current
status.
Syntax
WiFi.begin();
WiFi.begin( ssid );
WiFi.begin( ssid, pass );
WiFi.begin( ssid, keyIndex, key );
Parameters
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.
Returns
WL_CONNECTED when connected to a network WL_IDLE_STATUS when not connected
to a network, but powered on
Example
/*F********************************************************************
*
**********************************************************************/
#include
char ssid[] = "yourNetwork"; // SSID OF NETWORK
char pass[] = "secretPassword"; // PASSWORD OF WPA NETWORK
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
WiFi.begin( ssid, pass );
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{}
WiFi - WiFi.disconnect()
Description
Disconnects the WiFi shield from the current network.
Syntax
WiFi.disconnect();
Parameters
none
Returns
nothing
WiFi - WiFi.config()
Description
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.
Syntax
WiFi.config( ip );
WiFi.config( ip, dns );
WiFi.config( ip, dns, gateway );
WiFi.config( ip, dns, gateway, subnet );
Parameters
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
Returns
Nothing
Example
This example shows how to set the static IP address, 192.168.0.177, of the
LAN network to the WiFi shield:
/*F********************************************************************
*
**********************************************************************/
#include
#include
IPAddress ip(192, 168, 0, 177); // IP ADDRESS FOR SHIELD
char ssid[] = "yourNetwork"; // NETWORK SSID (NAME)
char pass[] = "secretPassword"; // NETWORK PASSWORD (FOR WPA, OR AS KEY FOR WEP)
int status = WL_IDLE_STATUS;
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( BAUD ); // INITIALIZE SERIAL AND WAIT FOR PORT TO OPEN
while( !Serial )
{
; // WAIT FOR SERIAL PORT TO CONNECT. NEEDED FOR lEONARDO ONLY
}
if( WiFi.status() == WL_NO_SHIELD) // CHECK FOR PRESENCE OF SHIELD
{
Serial.println( "WiFi shield not present");
while( true ); // DON'T CONTINUE
}
WiFi.config( ip );
while( status != WL_CONNECTED ) // ATTEMPT TO CONNECT TO Wifi NETWORK
{
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 );
delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION
}
Serial.print( "IP Address: " ); // PRINT YOUR WiFi SHIELD'S IP ADDRESS
Serial.println( WiFi.localIP() );
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{}
WiFi -
WiFi.SSID()
Description
Gets the SSID of the current network
Syntax
WiFi.SSID();
WiFi.SSID( wifiAccessPoint )
Parameters
wifiAccessPoint: specifies from which network to get the information
Returns
A string containing SSID WiFi shield is currently connected to.
string containing name of network requested.
Example:
/*F********************************************************************
*
**********************************************************************/
#include
#include
//SSID of your network
char ssid[] = "yourNetwork";
int status = WL_IDLE_STATUS; // the Wifi radio's status
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
// INITIALIZE SERIAL
Serial.begin( BAUD );
Serial.println( "Scanning available networks..." );
scanNetworks(); // SCAN FOR EXISTING NETWORKS
Serial.println( "Attempting to connect to open network...");
status = WiFi.begin( ssid ); // ATTEMPT TO CONNECT USING WEP ENCRYPTION
Serial.print( "SSID: " );
Serial.println( ssid );
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{}
/*F********************************************************************
*
**********************************************************************/
void
scanNetworks()
{
// SCAN FOR NEARBY NETWORKS:
Serial.println( "** Scan Networks **" );
byte numSsid = WiFi.scanNetworks();
Serial.print( "SSID List:" ); // PRINT LIST OF NETWORKS SEEN
Serial.println( numSsid );
for( int thisNet = 0; thisNet
WiFi - WiFi.BSSID()
Description
Gets MAC address of the routher you are connected to
Syntax
WiFi.BSSID( bssid );
Parameters
bssid : 6 byte array
Returns
A byte array containing the MAC address of the router the WiFi shield is
currently connected to.
Example
/*F********************************************************************
*
**********************************************************************/
#include
// network SSID
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";
/*F********************************************************************
*
**********************************************************************/
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
{
byte bssid[6]; // PRINT MAC ADDRESS OF ROUTER YOU'RE ATTACHED TO
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);
}
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{}
WiFi - WiFi.