WiFi Web Client

Connect to a remote webserver.

This example shows you how to make a HTTP request using a WiFi shield. It returns a Google search for the term "Arduino". The results of this search are viewable as HTML through your Arduino Software (IDE) serial window.

This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly.

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.

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 board would be stacked below the WiFi shield.

Code

1/*
2
3 Web client
4
5 This sketch connects to a website (http://www.google.com)
6
7 using a WiFi shield.
8
9 This example is written for a network using WPA encryption. For
10
11 WEP or WPA, change the Wifi.begin() call accordingly.
12
13 This example is written for a network using WPA encryption. For
14
15 WEP or WPA, change the Wifi.begin() call accordingly.
16
17 Circuit:
18
19 * WiFi shield attached
20
21 created 13 July 2010
22
23 by dlf (Metodo2 srl)
24
25 modified 31 May 2012
26
27 by Tom Igoe
28
29 */
30
31#include <SPI.h>
32#include <WiFi.h>
33
34char ssid[] = "yourNetwork"; // your network SSID (name)
35char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
36int keyIndex = 0; // your network key Index number (needed only for WEP)
37
38int status = WL_IDLE_STATUS;
39// if you don't want to use DNS (and reduce your sketch size)
40// use the numeric IP instead of the name for the server:
41//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
42char server[] = "www.google.com"; // name address for Google (using DNS)
43
44// Initialize the Ethernet client library
45// with the IP address and port of the server
46// that you want to connect to (port 80 is default for HTTP):
47
48WiFiClient client;
49
50void setup() {
51
52 //Initialize serial and wait for port to open:
53
54 Serial.begin(9600);
55
56 while (!Serial) {
57
58 ; // wait for serial port to connect. Needed for native USB port only
59
60 }
61
62 // check for the presence of the shield:
63
64 if (WiFi.status() == WL_NO_SHIELD) {
65
66 Serial.println("WiFi shield not present");
67
68 // don't continue:
69
70 while (true);
71
72 }
73
74 String fv = WiFi.firmwareVersion();
75
76 if (fv != "1.1.0") {
77
78 Serial.println("Please upgrade the firmware");
79
80 }
81
82 // attempt to connect to Wifi network:
83
84 while (status != WL_CONNECTED) {
85
86 Serial.print("Attempting to connect to SSID: ");
87
88 Serial.println(ssid);
89
90 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
91
92 status = WiFi.begin(ssid, pass);
93
94 // wait 10 seconds for connection:
95
96 delay(10000);
97
98 }
99
100 Serial.println("Connected to wifi");
101
102 printWifiStatus();
103
104 Serial.println("\nStarting connection to server...");
105
106 // if you get a connection, report back via serial:
107
108 if (client.connect(server, 80)) {
109
110 Serial.println("connected to server");
111
112 // Make a HTTP request:
113
114 client.println("GET /search?q=arduino HTTP/1.1");
115
116 client.println("Host: www.google.com");
117
118 client.println("Connection: close");
119
120 client.println();
121
122 }
123}
124
125void loop() {
126
127 // if there are incoming bytes available
128
129 // from the server, read them and print them:
130
131 while (client.available()) {
132
133 char c = client.read();
134
135 Serial.write(c);
136
137 }
138
139 // if the server's disconnected, stop the client:
140
141 if (!client.connected()) {
142
143 Serial.println();
144
145 Serial.println("disconnecting from server.");
146
147 client.stop();
148
149 // do nothing forevermore:
150
151 while (true);
152
153 }
154}
155
156void printWifiStatus() {
157
158 // print the SSID of the network you're attached to:
159
160 Serial.print("SSID: ");
161
162 Serial.println(WiFi.SSID());
163
164 // print your WiFi shield's IP address:
165
166 IPAddress ip = WiFi.localIP();
167
168 Serial.print("IP Address: ");
169
170 Serial.println(ip);
171
172 // print the received signal strength:
173
174 long rssi = WiFi.RSSI();
175
176 Serial.print("signal strength (RSSI):");
177
178 Serial.print(rssi);
179
180 Serial.println(" dBm");
181}

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.