Connect With WEP

Demonstrates how to connect to a network that is encrypted with WEP.

This example shows you how to connect to a WEP 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.

WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need the SSID, the key, and key number.

WiFiShield bb

image developed using Fritzing. For more circuit examples, see the Fritzing project page

In the above image, the Arduino would be stacked below the WiFi shield.

Code

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

Last revision 2018/08/23 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.