2012-04-19 21:02:12 +02:00
|
|
|
/*
|
|
|
|
Repeating Web client
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-04-19 21:02:12 +02:00
|
|
|
This sketch connects to a a web server and makes a request
|
|
|
|
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
|
|
|
|
the Adafruit Ethernet shield, either one will work, as long as it's got
|
|
|
|
a Wiznet Ethernet module on board.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-04-19 21:03:23 +02:00
|
|
|
This example uses DNS, by assigning the Ethernet client with a MAC address,
|
2012-04-19 21:02:12 +02:00
|
|
|
IP address, and DNS address.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-04-19 21:02:12 +02:00
|
|
|
Circuit:
|
|
|
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-04-19 21:02:12 +02:00
|
|
|
created 19 Apr 2012
|
|
|
|
by Tom Igoe
|
2014-01-21 20:38:52 +01:00
|
|
|
modified 21 Jan 2014
|
|
|
|
by Federico Vanzati
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/WebClientRepeating
|
2012-04-19 21:02:12 +02:00
|
|
|
This code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-04-19 21:02:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <Ethernet.h>
|
|
|
|
|
|
|
|
// assign a MAC address for the ethernet controller.
|
|
|
|
// fill in your address here:
|
2013-10-21 09:58:40 +02:00
|
|
|
byte mac[] = {
|
|
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
|
|
|
};
|
2012-04-19 21:02:12 +02:00
|
|
|
// fill in an available IP address on your network here,
|
|
|
|
// for manual configuration:
|
2014-01-21 20:38:52 +01:00
|
|
|
IPAddress ip(192, 168, 1, 177);
|
2012-04-19 21:02:12 +02:00
|
|
|
|
|
|
|
// fill in your Domain Name Server address here:
|
2013-10-21 09:58:40 +02:00
|
|
|
IPAddress myDns(1, 1, 1, 1);
|
2012-04-19 21:02:12 +02:00
|
|
|
|
|
|
|
// initialize the library instance:
|
|
|
|
EthernetClient client;
|
|
|
|
|
|
|
|
char server[] = "www.arduino.cc";
|
2014-01-21 20:38:52 +01:00
|
|
|
//IPAddress server(64,131,82,241);
|
2012-04-19 21:02:12 +02:00
|
|
|
|
2012-10-18 19:03:25 +02:00
|
|
|
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
2014-01-21 20:38:52 +01:00
|
|
|
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
|
2013-10-21 09:58:40 +02:00
|
|
|
// the "L" is needed to use long type numbers
|
2012-04-19 21:02:12 +02:00
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// start serial port:
|
|
|
|
Serial.begin(9600);
|
2014-01-21 20:38:52 +01:00
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2014-01-21 20:38:52 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 21:02:12 +02:00
|
|
|
// give the ethernet module time to boot up:
|
|
|
|
delay(1000);
|
|
|
|
// start the Ethernet connection using a fixed IP address and DNS server:
|
|
|
|
Ethernet.begin(mac, ip, myDns);
|
|
|
|
// print the Ethernet board/shield's IP address:
|
|
|
|
Serial.print("My IP address: ");
|
|
|
|
Serial.println(Ethernet.localIP());
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// if there's incoming data from the net connection.
|
|
|
|
// send it out the serial port. This is for debugging
|
|
|
|
// purposes only:
|
|
|
|
if (client.available()) {
|
|
|
|
char c = client.read();
|
2014-01-21 20:38:52 +01:00
|
|
|
Serial.write(c);
|
2012-04-19 21:02:12 +02:00
|
|
|
}
|
|
|
|
|
2014-01-21 20:38:52 +01:00
|
|
|
// if ten seconds have passed since your last connection,
|
|
|
|
// then connect again and send data:
|
|
|
|
if (millis() - lastConnectionTime > postingInterval) {
|
2012-04-19 21:02:12 +02:00
|
|
|
httpRequest();
|
|
|
|
}
|
2014-01-21 20:38:52 +01:00
|
|
|
|
2012-04-19 21:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// this method makes a HTTP connection to the server:
|
|
|
|
void httpRequest() {
|
2014-01-21 20:38:52 +01:00
|
|
|
// close any connection before send a new request.
|
|
|
|
// This will free the socket on the WiFi shield
|
|
|
|
client.stop();
|
|
|
|
|
2012-04-19 21:02:12 +02:00
|
|
|
// if there's a successful connection:
|
|
|
|
if (client.connect(server, 80)) {
|
|
|
|
Serial.println("connecting...");
|
2015-12-19 14:10:24 +01:00
|
|
|
// send the HTTP GET request:
|
2012-04-19 21:02:12 +02:00
|
|
|
client.println("GET /latest.txt HTTP/1.1");
|
|
|
|
client.println("Host: www.arduino.cc");
|
|
|
|
client.println("User-Agent: arduino-ethernet");
|
|
|
|
client.println("Connection: close");
|
|
|
|
client.println();
|
|
|
|
|
|
|
|
// note the time that the connection was made:
|
|
|
|
lastConnectionTime = millis();
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2012-04-19 21:02:12 +02:00
|
|
|
// if you couldn't make a connection:
|
|
|
|
Serial.println("connection failed");
|
|
|
|
}
|
|
|
|
}
|