Web Client Repeating

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 or Genuino 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 slave 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.

In the above image, the Arduino or Genuino board would be stacked below the Ethernet shield.

Schematic

Code

/*
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.

http://www.arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.

*/

#include <SPI.h>#include <Ethernet.h>

// assign a MAC address for the ethernet controller.// fill in your address here:byte mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);
IPAddress myDns(192,168,0,1);

// initialize the library instance:
EthernetClient client;

char server[]="www.arduino.cc";// also change the Host line in httpRequest()//IPAddress server(64,131,82,241);

unsignedlong lastConnectionTime =0;// last time you connected to the server, in milliseconds
const unsignedlong postingInterval =10*1000;// delay between updates, in milliseconds