0% found this document useful (0 votes)
184 views15 pages

Arduino Ethernet Shield Tutorial

The document provides instructions for using an Arduino Ethernet Shield to connect an Arduino board to the internet. It allows the Arduino to send and receive data from anywhere with an internet connection. The summary describes how to set up the shield by plugging it into the Arduino board and how it opens up possibilities to control projects remotely from a website or based on events on the internet.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
184 views15 pages

Arduino Ethernet Shield Tutorial

The document provides instructions for using an Arduino Ethernet Shield to connect an Arduino board to the internet. It allows the Arduino to send and receive data from anywhere with an internet connection. The summary describes how to set up the shield by plugging it into the Arduino board and how it opens up possibilities to control projects remotely from a website or based on events on the internet.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

instructables

Arduino Ethernet Shield Tutorial

by randofo

The Arduino Ethernet Shield allows you to easily remotely from a website, or ring a bell every time you
connect your Arduino to the internet. This shield get a new twitter message. This shield opens up
enables your Arduino to send and receive data from endless amounts of possibility by allowing you to
anywhere in the world with an internet connection. connect your project to the internet in no-time at.
You can use it to do fun stu like control robots

Step 1: Setup

Setting it up is as simple as plugging the header pins *I have not personally con rmed this. Buyer beware!
from the shield into your Arduino.
(Note that some of the links on this page are a liate
Note that the Ethernet Shield used here used to be links. This does not change the cost of the item for
sold at RadioShack. However, since RadioShack is no you. I reinvest whatever proceeds I receive into
more, the shield is harder to come by. This generic making new projects. If you would like any
ethernet shield available on Amazon should also suggestions for alternative suppliers, please let me
work.* It is meant to be used with the Arduino Uno know.)
Rev. 3 boards (or later). It has too many pins to plug
into earlier version Arduino boards.

Arduino Ethernet Shield Tutorial: Page 1


Step 2: Shield Features

The Ethernet Shield is based upon the W51000 chip, the use of an external SD library, which does not come
which has an internal 16K bu er. It has a connection bundled with the software. Using the SD card is not
speed of up to 10/100Mb. This is not the fastest covered in this Instructable. However, it is covered in
connection around, but is also nothing to turn your the Step 8 of the Wireless SD card instructable.
nose up at.
The board also has space for the addition of a Power
It relies on the Arduino Ethernet library, which comes over Ethernet (PoE) module, which allows you to
bundled with the development environment. power your Arduino over an Ethernet connection.

There is also an on-board micro SD slot which enables For a full technical overview, see the o cial Ethernet
you to store a heck-of-a-lot of data, and serve up Shield page.
entire websites using just your Arduino. This requires

Arduino Ethernet Shield Tutorial: Page 2


Step 3: Get Started

Plug the Arduino into your computer's USB port, and File --> Examples --> Ethernet --> DhcpAddressPrinter
the Ethernet shield into your router (or direct internet
connection). Once open, you may need to change the Mac address.
On newer versions of the Ethernet shield, you should
Next, open the Arduino development environment. I see this address on a sticker attached to the board. If
highly recommend upgrading to Arduino 1.0 or later you are missing a sticker, simply making up a unique
(if you have not done so already). This version of the mac address should work. If you are using multiple
software has built in DHCP support, and does not shields, make sure each has a unique mac address.
require manually con guring an IP address.
Once the mac address is properly con gured, upload
To gure out what IP address has been assigned to the sketch to your Arduino, and open the serial
your board, open the DhcpAddressPrinter sketch. This monitor. It should print out the IP address in use.
can be found at:

Step 4: Server

You can use the Arduino Ethernet shield as a web server to load an HTML page or function as a chat server. You can
also parse requests sent by a client, such as a web browser. The following two examples show how to use it to serve
HTML pages, and parse URL strings.

One important thing to keep in mind is that you will have to enter your Arduino's IP address in both of the
examples below in order for them to work.

The following code changes the web page served based on a button press:
<pre>/*
Web Server Demo
thrown together by Randy Sarafan

A simple web server that changes the page that is served, triggered by a button press.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Connect a button between Pin D2 and 5V
Arduino Ethernet Shield Tutorial: Page 3
* Connect a button between Pin D2 and 5V
* Connect a 10K resistor between Pin D2 and ground

Based almost entirely upon Web Server by Tom Igoe and David Mellis

Edit history:
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe

*/

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

// Enter a MAC address and IP address for your controller below.


// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress ip(191,11,1,1); //<<< ENTER YOUR IP ADDRESS HERE!!!

// Initialize the Ethernet server library


// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

int buttonPress = 1;

void setup()
{
pinMode(2, INPUT);

// start the Ethernet connection and the server:


Ethernet.begin(mac, ip);
server.begin();
}

void loop()
{
buttonPress = digitalRead(2);
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

//serves a different version of a website depending on whether or not the button


//connected to pin 2 is pressed.
if (buttonPress == 1) {
client.println("<cke:html><cke:body bgcolor=#FFFFFF>LIGHT!</cke:body></cke:html>");
}
else if (buttonPress == 0){
client.println("<cke:html><cke:body bgcolor=#000000 text=#FFFFFF>DARK!</cke:body></cke:html>");
}

break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
Arduino Ethernet Shield Tutorial: Page 4
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}

To make this example code work, simply attach a button between pin D2 and 5V, a 10K resistor between pin D2 and
ground, and then load the IP address of your Arduino into your web browser. The page should load with a black
background. Press and hold the button, and then refresh the browser page. The site should now load with a white
background.

The following code lights up an LED depending on the URL that is sent to the Arduino:

Arduino Ethernet Shield Tutorial: Page 5


<pre>/*
Web Server Demo
thrown together by Randy Sarafan

Allows you to turn on and off an LED by entering different urls.

To turn it on:
http://your-IP-address/$1

To turn it off:
http://your-IP-address/$2

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Connect an LED to pin D2 and put it in series with a 220 ohm resistor to ground

Based almost entirely upon Web Server by Tom Igoe and David Mellis

Edit history:
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe

*/

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

boolean incoming = 0;

// Enter a MAC address and IP address for your controller below.


// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress ip(191,11,1,1); //<<< ENTER YOUR IP ADDRESS HERE!!!

// Initialize the Ethernet server library


// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
pinMode(2, OUTPUT);

// start the Ethernet connection and the server:


Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
}

void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply

//reads URL string from $ to first blank space


if(incoming && c == ' '){
incoming = 0;
}
if(c == '

To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220 ohm
resistor to ground.
Arduino Ethernet Shield Tutorial: Page 6
To turn on the LED enter this into your browser:
http://[ YO UR IP ADDRES S HERE] /$1

To turn o the LED enter this into your browser:


http://[ YO UR IP ADDRES S HERE] /$2

Note: You should obviously replace [ YO UR IP ADDRES S HERE] with your IP address.){ incoming = 1; } //Checks for
the URL string $1 or $2 if(incoming == 1){ Serial.println(c); if(c == '1'){ Serial.println("ON"); digitalWrite(2, HIGH); } if(c
== '2'){ Serial.println("OFF"); digitalWrite(2, LOW ); } }

if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on
the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the
connection: client.stop(); } }

To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220 ohm
resistor to ground.

To turn on the LED enter this into your browser:


http://[ YO UR IP ADDRES S HERE] /$1

To turn o the LED enter this into your browser:


http://[ YO UR IP ADDRES S HERE] /$2

Note: You should obviously replace [ YO UR IP ADDRES S HERE] with your IP address.

Arduino Ethernet Shield Tutorial: Page 7


Step 5: Client

You can also use the Ethernet Shield as a client. In other words, you can use it to read websites like a web browser.

Websites have a lot of text both visible and hidden, which makes programming on the client side very tricky.
Reading information from websites typically involves parsing a lot of strings. This is maddening, but worth it, if that
is what you intend to do.

I was going to write some code to read Twitter messages, but such a code already exists as an example within the
Arduino programmer. Instead, I simply modi ed it slightly to turn on an LED if a special message is read.

To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220 ohm
resistor to ground.

Don't forget to enter your own IP address into the code below, or it will not work.

Here is the code:


<pre>/*
Twitter Client with Strings

This sketch connects to Twitter using an Ethernet shield. It parses the XML
returned, and looks for <text>this is a tweet</text>

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 the DHCP routines in the Ethernet library which is part of the
Arduino core from version 1.0 beta 1

This example uses the String library, which is part of the Arduino core from
version 0019.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 21 May 2011


by Tom Igoe

This code is in the public domain.

Arduino Ethernet Shield Tutorial: Page 8


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

// Enter a MAC address and IP address for your controller below.


// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(191,11,1,1); //<<< ENTER YOUR IP ADDRESS HERE!!!

// initialize the library instance:


EthernetClient client;

const int requestInterval = 60000; // delay between requests

char serverName[] = "api.twitter.com"; // twitter URL

boolean requested; // whether you've made a request since connecting


long lastAttemptTime = 0; // last time you connected to the server, in milliseconds

String currentLine = ""; // string to hold the text from server


String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet

void setup() {

pinMode(2, OUTPUT);

// reserve space for the strings:


currentLine.reserve(256);
tweet.reserve(150);

// initialize serial:
Serial.begin(9600);
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Ethernet.begin(mac, ip);
}
// connect to Twitter:
connectToServer();
}

void loop()
{
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();

// add incoming byte to end of line:


currentLine += inChar;

// if you get a newline, clear the line:


if (inChar == '\n') {
currentLine = "";
}
// if the current line ends with <text>, it will
// be followed by the tweet:
if ( currentLine.endsWith("<text>")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '<') {
tweet += inChar;
}
else {
// if you got a "<" character,
Arduino Ethernet Shield Tutorial: Page 9
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);

if(tweet == ">Hello Cruel World"){


digitalWrite(2, HIGH);
Serial.println("LED ON!");
}
if(tweet != ">Hello Cruel World"){
digitalWrite(2, LOW);
Serial.println("LED OFF!");
}

// close the connection to the server:


client.stop();
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}

void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(serverName, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=RandyMcTester&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}

Presumably you are going to want to read something other than the recent post on the RandyMcTester Twitter
feed.

To read other Twitter feeds, change the following bit of text:


client.println("GET /1/statuses/user_timeline.xml?screen_name=[NE W T W IT T ER NAM E HERE] &count=1
HTTP/1.1");

Did you nd this useful, fun, or entertaining?


Follow @m a de ine upho ria to see my latest projects.

Arduino Ethernet Shield Tutorial: Page 10


https://drive.google.com/drive/folders/1iTe2oUUXrd...
I done the tutorial to toggle led with different urls

I get this message :


Failed to configure Ethernet using DHCP
any idea how to fix this thx ?
ipconfig /all gives me the physical address that i put in mac[].
it also says dhcp enabled : no
even if code is for DHCPaddressPrinter.
in case of ethernet server code :
i cant open my ip address in web browser.
when i ping it says "destination host unreachable".
Have you set up the ip of the Ethernet shield? the ip should be same range as your PC's ip like
192.168.0.2 for PC
192.168.0.10 for shield.

And also some cheap W5100 shields has a problem with a resistor array situated near the RJ45
socket. It should be 510 instead of 511 (51Ohm instead of 510ohm)
That's the case with my Ethernet shield. I do not have the right equipment to replace the
resistors....
So how do you know if your buying the good one if there is no version numbers on these board ?
Also Are you saying the ones with the Bad Resistor values are NO GOOD at all or flakey ?
Hi, This post is really informative.
I'm facing difficulty connecting arduino ethernet with PC using a switch (Dlink). When both are
connected directly(p2p) using ethernet cable, arduino device can be pinged. However, when
connected via switch - ping doesn't work, and both devices can't communicate with each other;
updated the MAC addr. several times, nothing worked. I'm stuck. Can you suggest something on
this?

Arduino Ethernet Shield Tutorial: Page 11


Hi, your problem could come from the lack of Auto MDI-X. You may have a crossover ethernet
cable instead of a straight cable. Try to change your ethernet cable. If not working, try to reboot the
switch or change the port.
I had problems running the sketch until I removed the SD card. It seems my Ethernet shield won’t
work of the SD card is plugged in.
Thanks for such a useful forum.
I have connected the eithernet shield directly to my laptop and put them in the same network, after
that I was able to switch the led on/off. but when I put my lap top and the shield to the my Home
network I can ping the shield from the laptop but I can't switch the led on/off.
from this program i cant get my ip address from ethernet shield..
i only get server is at 0.0.0.0 -> this ip address from web server
what i should do ? i am using ethernet shield HanRun HR911105A 16/02
Arduino with AJAX
https://www.youtube.com/playlist?list=PLbUAcqHuByzfm9od5kHKArjKIAtfbT0LD
It would be better if you make it in English...I don't understand anything from your video....

I HAVE RECENTLY PURCHASED AN ARDUINO KIT WITH ETHERNET SHIELD AND I AM


NOT ABLE TO UPLOAD THE PROGRAM ON THE BOARD.THE ERROR SHOWN IS :
THE I/O OPERATION HAS BEEN ABORTED BECAUSE OF EITHER A THREAD EXIT OR AN
APPLICATION REQUEST
hi,

i've got the same problem. set a fixed ip.. it doesn't solve the problem but you go further

IPAddress ip(132 ,206 ,95 ,251);


Ethernet.begin(mac, ip);
I get this message :
Failed to configure Ethernet using DHCP
any idea how to fix this ?
Pls reply asap.
Thanks, it would be a great help
Have you configured the ip address of ethernet shield?

Hey, I have been using HanRun HR911105A and when I connect it to my Laptop through ethernet
cable it doesn't detect the connection and there is no blinking of LED's on the ethernet shield as
well as on the Laptop.
Thanks in advance hopefully someone can help!!
Can i ask if whats your mac adress for your ethernet? We have a project and its the same ethernet
shield we use, but we dont know the mac address. Reply asap. Thank you
The Arduino shield and the computer both have the same RJ45 connections. That is, the send and
receive pins are on the same pin for both sender and receiver. If you connect them directly with a
cable, the send pins match up and the receive pins match up and therefore you get no
communications.
You either need an Ethernet hub/switch or a cross over cable that crosses over the Send and
Receive pins. The hub/switch is definetly the easiest way to go and you can pick them up cheaply
from amazon.

Arduino Ethernet Shield Tutorial: Page 12


Just a thought, when you normally plug your Arduino board into your computer the usb cable
supplies the power. Most ethernet does not support power over internet (POI), so unless you are
also connecting to your PC using usb at the same time you will need to supply external power to
your board.
If you already provided external power ignore this comment.
Hi ! Your
instructions are very well managed and self explanatory. But I’m
facing issues at the beginning. Whenever I’m powering up the UNO board, the
chip on the Ethernet shield is getting hit up within 2-3 secs. So, I'm unable to start the shield at all.
Please suggest / help...
Hi, am able to ping the Ethernet shield but cant get anything in browser.
What's the use of the EthernetServer Server(80)
i made an internet controlled rover using the arduino ethernet shield following
https://www.instructables.com/id/Internet-Controlle... this instructables...but it only works for my
internet connection...i cant operate it from other internet connections with the same ip address...if
anyone could help :(
Thanks Randofo
The Step 5. (Twitter Reader) section does not work. Please confirm this
https://www.instructables.com/member/randofo/
... I presume it would need a Twitter API

the twitter api updated and now you need authentication, which i have no idea how you get
authentication...
to copy the code from the text area, simply (tested on chrome) rightClick the textArea and click
inspect element, than expand the textArea element and there you have it
I am unable to copy the sample code that you provided. Is it available somewhere that it copied?
Thanks.
Yes, that's annoying -- wish the author would change that. I downloaded the PDF, cut and pasted
the text into the arduino editor. I think you might have to have a pro instructables membership to
download the PDF.
Does anyone know how to connect the radio module NRF24L01

Can we use wifi insted of CAT5 cables for wireless communication?

looks like photo is not correct. Looks like the black wire of switch is connected to ground, not 5V.
Hooked up to 5V instead and it seems to be working. Get DARK/LIGHT on website [firefox]
can you use this shield connected to your computers ethranet port

please help me, serial monitor can't be open....


it says "Board at COM49 is not available"
thanks for sharing nice idea

Arduino Ethernet Shield Tutorial: Page 13


I get this message :
Failed to configure Ethernet using DHCP
any idea how to fix this thx ?
Hi, thanks for the wonderful walk-through! However, the twitter client doesn't seems to work.. Any
idea?
Thanks ! it worked for me. Just a little reminder : to get the info on your monitor, don't forget to set it
to the same baud rate as the one set by the program (ie DhcpAddressePrinter) for the serial
connection (check the number XXX in Serial.begin(XXX)). To change the baud rate, you can use
the "check list button" at bottom right of the monitor
Thanks for the tutorial, but i have a question to the server work example.
If i would like to use other strings other than "$1" and "$2", like "$ledon" for switching on the led
connected, what adjustment need to do to the codes.
I find it difficult to solve this problem.
Didn't work with 1.0, but first time with 1.6.1

Let me first start by saying that I am a complete noob when it comes to anything Arduino. My kit
hasn't even arrived in the mail yet so please go easy on me. :)
However, I have a question regarding this project. Specifically the RJ45 Ethernet Shield. In your
example you are using the RJ45 to interact with the Arduino via a website/internet, is it possible to
use the Arduino + Shield to interact with a series of rocker switches via a 8 channel relay board? If
so, I was thinking that it would make the installation of my project really clean being able to use the
RJ45 cable between my Arduino and the switches that will be located perhaps 10-15 feet away.

Any and all help is appreciated.


This Tutorial is Fantastic and very very very very clear everything.Got this tutorial after 4 days
searching on my related topic.
Many Many Thanks Dear.
Excuse me, does anyone know how to access the ethernet shield from the outside of the local
network - that is from the internet? I guess the local dynamic IP has to be changed, right..? Where
to get that IP?
how to access the ethernet shield from the outside of the local network - that is from the internet?

You need to port forward the IP and port you are using of the ethernet shield to the internet
This website may help: http://portforward.com/
I guess the local dynamic IP has to be changed, right..?

Yes if you are port forwarding the ethernet shield should have a static local IP. You can define the
ethernet shield IP in the code.
http://arduino.cc/en/Reference/EthernetIPAddress
you can find your DNS servers, gateway, and subnet by typing ipconfig /all in a windows command
line.
Make the ip of the ethernet shield any ip not currently used on you network. An easy way do do
this is to take IP address listed in ipconfig /all and change the numbers after the last dot to 254 and
put that in the arduino code as its IP.
Where to get that IP?

If you mean your public IP you can find that by visiting


http://www.whatismyip.com/
Arduino Ethernet Shield Tutorial: Page 14
Nice thank u

thanks for your information,


secretfood.net

It's useful thank

Nice Tutorial!!
I'm a engineer in WIZnet providing W5100 to the official Arduino Ethernet Shield.
WIZnet made W5500 and WIZ550io/ioShield-A. If you are interested in W5500 and make a tutorial
of W5500, I will give a WIZ550io to you free. If you want, feel freely to contact me.
Thank you.
my ethernet shields ic gets hot will u plsplspls help me for that
am using arduino leonardo board
pls help me

regards
pyt
I have one of the Non-POE (power Over Ethernet) 5100's, and it does the same to me.. I think it's
the 3.3V regulator trying to run the single chip, which is chewing-up massive wattage.. (even if it is
being powered from the +5V regulator from the Arduino.) I've often wondered, if I could repower
this off the 3.3V off an external power supply, but someone said that's a bad idea, as the back-feed
could burn-out the regulator, and other chips switching between the 9 down to 5, and 5 down to
3.3, or even the USB to serial chip.

Arduino Ethernet Shield Tutorial: Page 15

You might also like