Connect With WPA
Demonstrates how to connect to a network that is encrypted with WPA2 Personal.
LAST REVISION: 09/14/2022, 08:09 AM
This example shows you how to connect to a WPA2 Personal encrypted 802.11b/g
network with the Arduino WiFi shield. Your Arduino Software (IDE)
serial monitor will provide information about the connection once it has
connected.
Hardware Required
Arduino WiFi Shield
Shield-compatible Arduino board
Circuit
The WiFi shield uses pins 10, 11, 12, and 13 for the SPI connection to the
HDG104 module. Digital pin 4 is used to control the chip select pin on
the SD card.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The shield will not connect to networks using WPA2 Enterprise encryption.
Image developed using Fritzing .
For more circuit examples, see the Fritzing project page
In the above image, the board would be stacked below the WiFi shield.
Code
/*H*******************************************************
This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details.
Circuit:
* WiFi shield attached created 13 July 201 by dlf (Metodo2 srl)
modified 31 May 201 by Tom Igoe
********************************************************/
#include <SPI.h>
#include <WiFi.h>
//************************* DEFINES ************************************
#define BAUD 9600
//************************* PROTOTYPES ************************************
void printCurrentNet();
void printWifiData();
//************************* VARIABLES ************************************
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's 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 NATIVE USB PORT ONLY
// CHECK FOR PRESENCE OF SHIELD
if( WiFi.status() == WL_NO_SHIELD)
{
Serial.println( "WiFi shield not present" );
while( true ); // WHOA
}
String fv = WiFi.firmwareVersion();
if( fv != "1.1.0" )
Serial.println( "Please upgrade the firmware");
while( status != WL_CONNECTED ) // ATTEMPT TO CONNECT TO Wifi NETWORK:
{
Serial.print( "Attempting to connect to WPA SSID: ");
Serial.println( ssid );
status = WiFi.begin( ssid, pass ); // CONNECT TO WPA/WPA2 NETWORK
delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION
}
Serial.print( "You're connected to the network");
printCurrentNet(); // YOU'RE CONNECTED NOW, SO PRINT OUT DATA
printWifiData();
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
delay( 10000 );
printCurrentNet(); // CHECK NETWORK CONNECTION ONCE EVERY 10 SECONDS
}
/*F********************************************************************
*
**********************************************************************/
void
printWifiData()
{
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print( "IP Address: " );
Serial.println( ip );
Serial.println( ip );
// print your MAC address:
byte mac[6];
WiFi.macAddress( mac );
Serial.print( "MAC address: " );
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 );
}
/*F********************************************************************
*
**********************************************************************/
void
printCurrentNet()
{
Serial.print( "SSID: " ); // PRINT SSID OF NETWORK YOU'RE ATTACHED TO
Serial.println( WiFi.SSID() );
byte bssid[6];
WiFi.BSSID( bssid );
Serial.print( "BSSID: " ); // PRINT YOU'RE ROUTER MAC ADDRESS
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 );
long rssi = WiFi.RSSI();
Serial.print( "signal strength ( RSSI ):" );
Serial.println( rssi ); // PRINT RECEIVED SIGNAL STRENGTH
byte encryption = WiFi.encryptionType();
Serial.print( "Encryption Type:" ); // PRINT ENCRYPTION TYPE
Serial.println( encryption, HEX );
Serial.println();
}