Home ARDUINO How to Connect Arduino to internet with Ethernet Shield

How to Connect Arduino to internet with Ethernet Shield

Arduino Ethernet Shield allows you to easily connect your Arduino to the Internet. This shield allows your Arduino to send and receive data from anywhere in the world over an internet connection.

You can use it to remotely control interesting things like robots from the web, or turn it on each time you receive a new Twitter message. This shield allows you to connect your project to the internet at any time, opening up the myriad possibilities.

Collect Hardware

Things you need:

How to Connect Arduino to internet with Ethernet ShieldAssembling

Setting up is as easy as inserting a shield plug into your Arduino.

How to Connect Arduino to internet with Ethernet Shield

Please note that the Ethernet shield used here has been used with RadioShack. However, since RadioShack is gone, the shield is hard to pass. This universal Ethernet shield available on Amazon should also work. * It is for Arduino Uno Rev. 3 board (or later). It has too many pins to plug into earlier versions of the Arduino board.

Ethernet shield based on W51000 chip, built-in 16K buffer. It has up to 10 / 100Mb connection speed. This is not the fastest connection, but there is nothing to put on your nose.

Abstract Library

It relies on the Arduino Ethernet library bundled with the development environment.

There is also a micro-SD card slot that can store large amounts of data and use Arduino to provide the entire website. This requires the use of an external SD library that is not bundled with the software.

This guide does not cover the use of SD cards. However, this instruction has already been introduced in step 8 of Wireless SD Card.

The board also has room to add Power over Ethernet (PoE) modules, which allow you to power the Arduino over an Ethernet connection.

For a complete technical overview, see the official Ethernet Shield page.

Plug the Arduino into your computer’s USB port and plug the Ethernet shield into the router (or connect directly to the Internet).

How to Connect Arduino to internet with Ethernet Shield

Next, open the Arduino development environment. I highly recommend upgrading to Arduino 1.0 or later (if you have not already done so). This version of the software built-in DHCP support, do not need to manually configure the IP address.

To find the IP address assigned to your board, open the DhcpAddressPrinter sketch. This can be done in:

File -> Example -> Ethernet -> DhcpAddressPrinter

Once opened, you may need to change the Mac address. On the newer version of the Ethernet shield, you should see this address on the sticker attached to the motherboard. If you lack a tag, just composing a unique mac address should work. If you use multiple shields, make sure each has a unique mac address.

Once the MAC address is configured correctly, upload the sketch to your Arduino and open the serial monitor. It should print out the IP address in use.

HTML page

You can use the Arduino Ethernet Shield as a web server to load HTML pages or as a chat server. You can also parse requests sent by clients, such as web browsers. The following two examples show how to use it to provide HTML pages and parse URL strings.

One important thing to keep in mind is that you will have to enter the IP address of your Arduino in the example below so that they work.

Button Code

The following code changes the provided web page according to the button:

How to Connect Arduino to internet with Ethernet Shield

How to Connect Arduino to internet with Ethernet Shield

Upload source Code

Note: if you are a beginner in Arduino then please have a look at my previous article “Beginner guide for Arduino

<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
  • 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;

}

}

}

// give the web browser time to receive the data

delay(1);

// close the connection:

client.stop();

}

}

Code Working

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.

LED CODE

How to Connect Arduino to internet with Ethernet Shield

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

Upload Source

<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:

https://your-IP-address/$1

To turn it off:

https://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 == ‘

Code Working

To work with the code for this example, simply connect a button between the D2 and 5V pins, connect a 10K resistor between D2 and ground, and load the Arduino’s IP address into your web browser.

The page should load a black background. Hold down the button and refresh the browser page. It should now load a white background.