Ethernet Shield Web Client Repeating

Make repeated HTTP requests.

This example shows you how to make repeated HTTP requests using an Ethernet shield. This example uses DNS, by assigning the Ethernet client with a MAC address, IP address, and DNS address. It connects to http://www.arduino.cc/latest.txt. The content of the page is viewable in the Serial Monitor.

Hardware Required

Circuit

The Ethernet shield allows you to connect a WIZNet Ethernet controller to the Arduino boards via the SPI bus. It uses the ICSP header pins and pin 10 as chip select for the SPI connection to the Ethernet controller chip. Later models of the Ethernet shield also have an SD Card on board. Digital pin 4 is used to control the chip select pin on the SD card.

The shield should be connected to a network with an Ethernet cable. You will need to change the network settings in the program to correspond to your network.

The circuit for this tutorial.
The circuit for this tutorial.

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 Ethernet shield.

Schematic

The schematic for this tutorial.
The schematic for this tutorial.

Code

1/*
2
3 Repeating Web client
4
5 This sketch connects to a a web server and makes a request
6
7 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
8
9 the Adafruit Ethernet shield, either one will work, as long as it's got
10
11 a Wiznet Ethernet module on board.
12
13 This example uses DNS, by assigning the Ethernet client with a MAC address,
14
15 IP address, and DNS address.
16
17 Circuit:
18
19 * Ethernet shield attached to pins 10, 11, 12, 13
20
21 created 19 Apr 2012
22
23 by Tom Igoe
24
25 modified 21 Jan 2014
26
27 by Federico Vanzati
28
29 http://www.arduino.cc/en/Tutorial/WebClientRepeating
30
31 This code is in the public domain.
32
33 */
34
35#include <SPI.h>
36#include <Ethernet.h>
37
38// assign a MAC address for the ethernet controller.
39// fill in your address here:
40byte mac[] = {
41
42 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
43};
44// Set the static IP address to use if the DHCP fails to assign
45
46IPAddress ip(192, 168, 0, 177);
47
48IPAddress myDns(192, 168, 0, 1);
49
50// initialize the library instance:
51
52EthernetClient client;
53
54char server[] = "www.arduino.cc"; // also change the Host line in httpRequest()
55//IPAddress server(64,131,82,241);
56
57unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
58
59const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
60
61void setup() {
62
63 // You can use Ethernet.init(pin) to configure the CS pin
64
65 //Ethernet.init(10); // Most Arduino shields
66
67 //Ethernet.init(5); // MKR ETH shield
68
69 //Ethernet.init(0); // Teensy 2.0
70
71 //Ethernet.init(20); // Teensy++ 2.0
72
73 //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
74
75 //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
76
77 // start serial port:
78
79 Serial.begin(9600);
80
81 while (!Serial) {
82
83 ; // wait for serial port to connect. Needed for native USB port only
84
85 }
86
87 // start the Ethernet connection:
88
89 Serial.println("Initialize Ethernet with DHCP:");
90
91 if (Ethernet.begin(mac) == 0) {
92
93 Serial.println("Failed to configure Ethernet using DHCP");
94
95 // Check for Ethernet hardware present
96
97 if (Ethernet.hardwareStatus() == EthernetNoHardware) {
98
99 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
100
101 while (true) {
102
103 delay(1); // do nothing, no point running without Ethernet hardware
104
105 }
106
107 }
108
109 if (Ethernet.linkStatus() == LinkOFF) {
110
111 Serial.println("Ethernet cable is not connected.");
112
113 }
114
115 // try to configure using IP address instead of DHCP:
116
117 Ethernet.begin(mac, ip, myDns);
118
119 Serial.print("My IP address: ");
120
121 Serial.println(Ethernet.localIP());
122
123 } else {
124
125 Serial.print(" DHCP assigned IP ");
126
127 Serial.println(Ethernet.localIP());
128
129 }
130
131 // give the Ethernet shield a second to initialize:
132
133 delay(1000);
134}
135
136void loop() {
137
138 // if there's incoming data from the net connection.
139
140 // send it out the serial port. This is for debugging
141
142 // purposes only:
143
144 if (client.available()) {
145
146 char c = client.read();
147
148 Serial.write(c);
149
150 }
151
152 // if ten seconds have passed since your last connection,
153
154 // then connect again and send data:
155
156 if (millis() - lastConnectionTime > postingInterval) {
157
158 httpRequest();
159
160 }
161
162}
163
164// this method makes a HTTP connection to the server:
165void httpRequest() {
166
167 // close any connection before send a new request.
168
169 // This will free the socket on the WiFi shield
170
171 client.stop();
172
173 // if there's a successful connection:
174
175 if (client.connect(server, 80)) {
176
177 Serial.println("connecting...");
178
179 // send the HTTP GET request:
180
181 client.println("GET /latest.txt HTTP/1.1");
182
183 client.println("Host: www.arduino.cc");
184
185 client.println("User-Agent: arduino-ethernet");
186
187 client.println("Connection: close");
188
189 client.println();
190
191 // note the time that the connection was made:
192
193 lastConnectionTime = millis();
194
195 } else {
196
197 // if you couldn't make a connection:
198
199 Serial.println("connection failed");
200
201 }
202}

Last revision 2018/09/07 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.