Skip to content

Commit eb55a19

Browse files
author
Tom Igoe
committed
Added WebClientRepeating example
1 parent 13202e4 commit eb55a19

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Repeating Web client
3+
4+
This sketch connects to a a web server and makes a request
5+
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6+
the Adafruit Ethernet shield, either one will work, as long as it's got
7+
a Wiznet Ethernet module on board.
8+
9+
This example uses the String library, which is part of the Arduino core from
10+
version 0019. It also uses DNS, by assigning the Ethernet client with a MAC address,
11+
IP address, and DNS address.
12+
13+
Circuit:
14+
* Ethernet shield attached to pins 10, 11, 12, 13
15+
16+
created 19 Apr 2012
17+
by Tom Igoe
18+
19+
http://arduino.cc/en/Tutorial/WebClientRepeating
20+
This code is in the public domain.
21+
22+
*/
23+
24+
#include <SPI.h>
25+
#include <Ethernet.h>
26+
27+
// assign a MAC address for the ethernet controller.
28+
// fill in your address here:
29+
byte mac[] = {
30+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
31+
// fill in an available IP address on your network here,
32+
// for manual configuration:
33+
IPAddress ip(10,0,0,20);
34+
35+
// fill in your Domain Name Server address here:
36+
IPAddress myDns(1,1,1,1);
37+
38+
// initialize the library instance:
39+
EthernetClient client;
40+
41+
char server[] = "www.arduino.cc";
42+
43+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
44+
boolean lastConnected = false; // state of the connection last time through the main loop
45+
const unsigned long postingInterval = 60*1000; // delay between updates, in milliseconds
46+
47+
void setup() {
48+
// start serial port:
49+
Serial.begin(9600);
50+
// give the ethernet module time to boot up:
51+
delay(1000);
52+
// start the Ethernet connection using a fixed IP address and DNS server:
53+
Ethernet.begin(mac, ip, myDns);
54+
// print the Ethernet board/shield's IP address:
55+
Serial.print("My IP address: ");
56+
Serial.println(Ethernet.localIP());
57+
}
58+
59+
void loop() {
60+
// if there's incoming data from the net connection.
61+
// send it out the serial port. This is for debugging
62+
// purposes only:
63+
if (client.available()) {
64+
char c = client.read();
65+
Serial.print(c);
66+
}
67+
68+
// if there's no net connection, but there was one last time
69+
// through the loop, then stop the client:
70+
if (!client.connected() && lastConnected) {
71+
Serial.println();
72+
Serial.println("disconnecting.");
73+
client.stop();
74+
}
75+
76+
// if you're not connected, and ten seconds have passed since
77+
// your last connection, then connect again and send data:
78+
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
79+
httpRequest();
80+
}
81+
// store the state of the connection for next time through
82+
// the loop:
83+
lastConnected = client.connected();
84+
}
85+
86+
// this method makes a HTTP connection to the server:
87+
void httpRequest() {
88+
// if there's a successful connection:
89+
if (client.connect(server, 80)) {
90+
Serial.println("connecting...");
91+
// send the HTTP PUT request:
92+
client.println("GET /latest.txt HTTP/1.1");
93+
client.println("Host: www.arduino.cc");
94+
client.println("User-Agent: arduino-ethernet");
95+
client.println("Connection: close");
96+
client.println();
97+
98+
// note the time that the connection was made:
99+
lastConnectionTime = millis();
100+
}
101+
else {
102+
// if you couldn't make a connection:
103+
Serial.println("connection failed");
104+
Serial.println("disconnecting.");
105+
client.stop();
106+
}
107+
}
108+
109+
110+
111+

0 commit comments

Comments
 (0)