Skip to content

Commit 573d08c

Browse files
committed
Merge branch 'master' of github.com:arduino/Arduino into LUFA_bootloader
2 parents 4fb17c4 + a69bd00 commit 573d08c

File tree

1 file changed

+110
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)