Note: this page refers to a product that is retired.

Connect With WPA

Demonstrates how to connect to a network that is encrypted with WPA2 Personal.

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.

WiFiShield bb

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

1/*
2
3 This example connects to an unencrypted Wifi network.
4
5 Then it prints the MAC address of the Wifi shield,
6
7 the IP address obtained, and other network details.
8
9 Circuit:
10
11 * WiFi shield attached
12
13 created 13 July 2010
14
15 by dlf (Metodo2 srl)
16
17 modified 31 May 2012
18
19 by Tom Igoe
20
21 */
22#include <SPI.h>
23#include <WiFi.h>
24
25char ssid[] = "yourNetwork"; // your network SSID (name)
26char pass[] = "secretPassword"; // your network password
27int status = WL_IDLE_STATUS; // the Wifi radio's status
28
29void setup() {
30
31 //Initialize serial and wait for port to open:
32
33 Serial.begin(9600);
34
35 while (!Serial) {
36
37 ; // wait for serial port to connect. Needed for native USB port only
38
39 }
40
41 // check for the presence of the shield:
42
43 if (WiFi.status() == WL_NO_SHIELD) {
44
45 Serial.println("WiFi shield not present");
46
47 // don't continue:
48
49 while (true);
50
51 }
52
53 String fv = WiFi.firmwareVersion();
54
55 if (fv != "1.1.0") {
56
57 Serial.println("Please upgrade the firmware");
58
59 }
60
61 // attempt to connect to Wifi network:
62
63 while (status != WL_CONNECTED) {
64
65 Serial.print("Attempting to connect to WPA SSID: ");
66
67 Serial.println(ssid);
68
69 // Connect to WPA/WPA2 network:
70
71 status = WiFi.begin(ssid, pass);
72
73 // wait 10 seconds for connection:
74
75 delay(10000);
76
77 }
78
79 // you're connected now, so print out the data:
80
81 Serial.print("You're connected to the network");
82
83 printCurrentNet();
84
85 printWifiData();
86
87}
88
89void loop() {
90
91 // check the network connection once every 10 seconds:
92
93 delay(10000);
94
95 printCurrentNet();
96}
97
98void printWifiData() {
99
100 // print your WiFi shield's IP address:
101
102 IPAddress ip = WiFi.localIP();
103
104 Serial.print("IP Address: ");
105
106 Serial.println(ip);
107
108 Serial.println(ip);
109
110 // print your MAC address:
111
112 byte mac[6];
113
114 WiFi.macAddress(mac);
115
116 Serial.print("MAC address: ");
117
118 Serial.print(mac[5], HEX);
119
120 Serial.print(":");
121
122 Serial.print(mac[4], HEX);
123
124 Serial.print(":");
125
126 Serial.print(mac[3], HEX);
127
128 Serial.print(":");
129
130 Serial.print(mac[2], HEX);
131
132 Serial.print(":");
133
134 Serial.print(mac[1], HEX);
135
136 Serial.print(":");
137
138 Serial.println(mac[0], HEX);
139
140}
141
142void printCurrentNet() {
143
144 // print the SSID of the network you're attached to:
145
146 Serial.print("SSID: ");
147
148 Serial.println(WiFi.SSID());
149
150 // print the MAC address of the router you're attached to:
151
152 byte bssid[6];
153
154 WiFi.BSSID(bssid);
155
156 Serial.print("BSSID: ");
157
158 Serial.print(bssid[5], HEX);
159
160 Serial.print(":");
161
162 Serial.print(bssid[4], HEX);
163
164 Serial.print(":");
165
166 Serial.print(bssid[3], HEX);
167
168 Serial.print(":");
169
170 Serial.print(bssid[2], HEX);
171
172 Serial.print(":");
173
174 Serial.print(bssid[1], HEX);
175
176 Serial.print(":");
177
178 Serial.println(bssid[0], HEX);
179
180 // print the received signal strength:
181
182 long rssi = WiFi.RSSI();
183
184 Serial.print("signal strength (RSSI):");
185
186 Serial.println(rssi);
187
188 // print the encryption type:
189
190 byte encryption = WiFi.encryptionType();
191
192 Serial.print("Encryption Type:");
193
194 Serial.println(encryption, HEX);
195
196 Serial.println();
197}

Last revision 2018/08/23 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.