Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Repeating Web client
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.
This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.
* Ethernet shield attached to pins 10, 11, 12, 13
created 19 Apr 2012
by Tom Igoe
modified 21 Jan 2014
by Federico Vanzati
This code is in the public domain.
#include <SPI.h>
#include <Ethernet.h>
int LED = 2;
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(10, 0, 0, 2);
// fill in your Domain Name Server address here:
IPAddress myDns(8, 8, 8, 8);
// initialize the library instance:
EthernetClient client;
//char server[] = "";
IPAddress server(127,0,0,1);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
void setup() {
pinMode(LED, OUTPUT);  
digitalWrite(LED, LOW); //program starts with the LED turned off
// start serial port:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
// give the ethernet module time to boot up:
// 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: ");
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 =;
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
// if there's a successful connection:
if (client.connect(server, 3000)) {
// send the HTTP GET request:
client.println("GET /path_to_url HTTP/1.1");
client.println("Connection: close");
digitalWrite(LED, LOW);
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
digitalWrite(LED, HIGH);
Serial.println("connection failed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment