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

WiFi Web Client Repeating

Set up your WiFi Shield to repeatedly make HTTP calls to a server.

This example shows you how to make repeated HTTP requests using a WiFi shield. It connects to http://www.arduino.cc/latest.txt. The content of the page is viewable 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 Repeating Wifi Web Client
4
5 This sketch connects to a a web server and makes a request
6
7 using an Arduino Wifi shield.
8
9 Circuit:
10
11 * WiFi shield attached to pins SPI pins and pin 7
12
13 created 23 April 2012
14
15 modified 31 May 2012
16
17 by Tom Igoe
18
19 modified 13 Jan 2014
20
21 by Federico Vanzati
22
23 https://docs.arduino.cc/library-examples/wifi-library/WiFiWebClientRepeating
24
25 This code is in the public domain.
26
27 */
28
29#include <SPI.h>
30#include <WiFi.h>
31
32char ssid[] = "yourNetwork"; // your network SSID (name)
33char pass[] = "secretPassword"; // your network password
34int keyIndex = 0; // your network key Index number (needed only for WEP)
35
36int status = WL_IDLE_STATUS;
37
38// Initialize the Wifi client library
39
40WiFiClient client;
41
42// server address:
43char server[] = "www.arduino.cc";
44//IPAddress server(64,131,82,241);
45
46unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
47
48const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
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 // you're connected now, so print out the status:
101
102 printWifiStatus();
103}
104
105void loop() {
106
107 // if there's incoming data from the net connection.
108
109 // send it out the serial port. This is for debugging
110
111 // purposes only:
112
113 while (client.available()) {
114
115 char c = client.read();
116
117 Serial.write(c);
118
119 }
120
121 // if ten seconds have passed since your last connection,
122
123 // then connect again and send data:
124
125 if (millis() - lastConnectionTime > postingInterval) {
126
127 httpRequest();
128
129 }
130
131}
132
133// this method makes a HTTP connection to the server:
134void httpRequest() {
135
136 // close any connection before send a new request.
137
138 // This will free the socket on the WiFi shield
139
140 client.stop();
141
142 // if there's a successful connection:
143
144 if (client.connect(server, 80)) {
145
146 Serial.println("connecting...");
147
148 // send the HTTP PUT request:
149
150 client.println("GET /latest.txt HTTP/1.1");
151
152 client.println("Host: www.arduino.cc");
153
154 client.println("User-Agent: ArduinoWiFi/1.1");
155
156 client.println("Connection: close");
157
158 client.println();
159
160 // note the time that the connection was made:
161
162 lastConnectionTime = millis();
163
164 } else {
165
166 // if you couldn't make a connection:
167
168 Serial.println("connection failed");
169
170 }
171}
172
173void printWifiStatus() {
174
175 // print the SSID of the network you're attached to:
176
177 Serial.print("SSID: ");
178
179 Serial.println(WiFi.SSID());
180
181 // print your WiFi shield's IP address:
182
183 IPAddress ip = WiFi.localIP();
184
185 Serial.print("IP Address: ");
186
187 Serial.println(ip);
188
189 // print the received signal strength:
190
191 long rssi = WiFi.RSSI();
192
193 Serial.print("signal strength (RSSI):");
194
195 Serial.print(rssi);
196
197 Serial.println(" dBm");
198}

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.