Friday, August 05, 2016

Filled Under: , ,

Web Client Repeating

Ethernet library 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 through your Arduino's serial window.


Step 1: What You Need?

1 x Arduino Board ( Arduino UNO used in this tutorial) 
1 x Arduino Ethernet Shield


Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.

The Ethernet shield allows you to connect a WizNet Ethernet controller to the Arduino boards via the SPI bus. It uses pins 10, 11, 12, and 13 for the SPI connection to the WizNet. 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.



Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.


2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.


Select the right port: Tools >> Port >> Select the port number.



3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> Ethernet >> WebClientRepeating

Click press the "upload" button (see the button with right arrow mark).


/*
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. Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 created 19 Apr 2012
by Tom Igoe
modified 21 Jan 2014
by Federico Vanzati 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
};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192, 168, 1, 177); // fill in your Domain Name Server address here:
IPAddress myDns(1, 1, 1, 1); // initialize the library instance:
EthernetClient client; char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241); 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() {
// start serial port:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
} // 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();
Serial.write(c);
} // if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
} } // 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
client.stop(); // if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
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();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}

Download:





Unknown

Author & Editor

My Robot Education Sdn. Bhd. (Robotedu.my) was founded in 2015 as the first robotics education centre in Malaysia to provide Arduino-based robotics courses for youths. Our vision is to be able to provide robotics education to every youth in Malaysia.

0 comments:

Post a Comment

 

  • Copyright © Arduino Tutorial ™ is a registered trademark.
    Designed by Templateism. Hosted on Blogger Templates.