Getting hands on Arduino Ethernet Shield

-->

Probably everyone knows Arduino and probably using it. Actually this development platform is worth its popularity. Probably the best thing about it is open-source ideology. Indeed it is great development platform that includes software and hardware solutions where even non electronics guru can master great projects very quickly. In a few years Arduino has grown in a great community around the world. And that is great – this means that you have access to endless resources, endless project ideas and lots of members that are willing to help if you are stuck with something. All basic information you can always find in http://www.arduino.cc/.

OK enough of talkies. Lets see what we have here. Thanks to SparkFun electronics, Arduino Duemilanove stands on my table fully assembled and ready to work.

arduino_with_ethernet

I decided to give a try on Arduino Ethernet shield based on Wiznet W5100 chip. It has a library so you don’t need to think of details how Ethernet chip is controlled. Few lines and you have some info in your favorite browser.

Of course if you are using Arduino Pro minimal design, you may also need FTDI Basic Breakout – 5V board.

Now lets grab the newest stable Arduino software release from http://arduino.cc/en/Main/Software. And lets get ready for a simple test. To make things more interesting I decided to make a simple LED control in order to feel client-server and server-client communications. I hooked led to digital pin 4 while other end to GND via current limiting resistor. I have connected Ethernet shield to WRT54G router. As I used standard port 80 there were no need for additional configuration.

While programming you will ideally need one library

#include <Ethernet.h>

Of course you can also use additional like WString.h which I used for fetching data from HTTP request.

First thing is to assign MAC and IP addresses so the board was accepted to local network:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

byte ip[] = { 192, 168, 1, 110 }; // ip in lan

byte gateway[] = { 192, 168, 1, 1 }; // internet access via router

byte subnet[] = { 255, 255, 255, 0 }; //subnet mask

Wiznet type Ethernet shield doesn’t support dynamic address assign (dhcp)– so you have to do this manually. You may want to check out for DHCP library which is early alpha stage.

Printing simple text, drawing tables is really easy task as it ends with multiple lines of print functions like

client.println();

with HTML formatted lines inside. Different task is when you need to send some data to Arduino server itself. One of common methods is reading HTTP request by characters. Simply when sending address with additional parameters like /?L=1 Arduino can capture this line character by character with line

char c = client.read();

And then analyze it. In your selected way. In this case it is very convenient to use string functions like append and contains (refer to TextString )

Arduino_ethernet_LED

Here is a program listing:

//*******************************

#include <WString.h>

#include <Ethernet.h>

/*

Simple Ethernet Test

Arduino server outputs simple text to browser

and controlling LED with simple checkbox

The circuit:

* Arduino Duemilanove

* Arduino Ethernet shield

* Basic FTDI breakout 5V

* LED connected to GND and digital pin 4 via resistor

By Minde

http://www.sciencprog.com/

*/

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

byte ip[] = { 192, 168, 1, 110 }; // ip in lan

byte gateway[] = { 192, 168, 1, 1 }; // internet access via router

byte subnet[] = { 255, 255, 255, 0 }; //subnet mask

Server server(80); //server port

byte sampledata=50; //some sample data – outputs 2 (ascii = 50 DEC)

int ledPin = 4; // LED pin

char link[]=”http://www.scienceprog.com/”; //link data

String readString = String(30); //string for fetching data from address

boolean LEDON = false; //LED status flag

void setup(){

//start Ethernet

Ethernet.begin(mac, ip, gateway, subnet);

//Set pin 4 to output

pinMode(ledPin, OUTPUT);

//enable serial datada print

Serial.begin(9600); }

void loop(){

// Create a client connection

Client client = server.available();

if (client) {

while (client.connected()) {

if (client.available()) {

char c = client.read();

//read char by char HTTP request

if (readString.length() < 30) {

//store characters to string

readString.append(c); }

//output chars to serial port

Serial.print(c);

//if HTTP request has ended

if (c == ‘\n’) {

//lets check if LED should be lighted

if(readString.contains(“L=1″)) {

//led has to be turned ON

digitalWrite(ledPin, HIGH); // set the LED on

LEDON = true;

}else{

//led has to be turned OFF

digitalWrite(ledPin, LOW); // set the LED OFF

LEDON = false; }

// now output HTML data starting with standart header

client.println(“HTTP/1.1 200 OK”);

client.println(“Content-Type: text/html”);

client.println();

//set background to yellow

client.print(“<body style=background-color:yellow>”);

//send first heading

client.println(“<font color=’red’><h1>HTTP test routines</font></h1>”);

client.println(“<hr />”);

client.println(“<hr />”);

//output some sample data to browser

client.println(“<font color=’blue’ size=’5′>Sample data: “);

client.print(sampledata);//lets output some data

client.println(“<br />”);//some space between lines

client.println(“<hr />”);

//drawing simple table

client.println(“<font color=’green’>Simple table: “);

client.println(“<br />”);

client.println(“<table border=1><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>”);

client.println(“<tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>”);

client.println(“<br />”);

client.println(“<hr />”);

//printing some link

client.println(“<font color=’blue’ size=’5′>Link: “);

client.print(“<a href=”);

client.print(link);

client.println(“>Visit Scienceprog!</a>”);

client.println(“<br />”);

client.println(“<hr />”);

//controlling led via checkbox

client.println(“<h1>LED control</h1>”);

//address will look like http://192.168.1.110/?L=1 when submited

client.println(“<form method=get name=LED><input type=checkbox name=L value=1>LED<br><input type=submit value=submit></form>”);

client.println(“<br />”);

//printing LED status

client.print(“<font size=’5′>LED status: “);

if (LEDON)

client.println(“<font color=’green’ size=’5′>ON”);

else

client.println(“<font color=’grey’ size=’5′>OFF”);

client.println(“<hr />”);

client.println(“<hr />”);

client.println(“</body></html>”);

//clearing string for next read

readString=”";

//stopping client

client.stop();

}}}}}

//*******************************

Test result on Firefox browser:

ethernet_shield_tests

If any questions or comments regarding this – don’t hesitate to drop a comment.

Download project file here: ethtest

Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post
 |  TrackBack URI for this post

New on WinAVR Tutorial
New on WinARM Tutorial

11 Responses to “Getting hands on Arduino Ethernet Shield”

  1. Claijon Says:

    Where can I get WString.h?
    Nice post!

  2. scienceprog Says:

    Its here named string.zip
    http://arduino.cc/en/Tutorial/TextString

  3. Rogerlette Says:

    I,

    Thanks for this great tutorial but I dont succed to make it work…

    - I download all the necessary library
    - I modify the ip and gateway according to my network
    - I put one led on the arduino pin 4 as you describe
    - I upload ethtest.pde in the arduino

    And finaly I put the IP in a mozilla browser but it reply page not found…

    Thank you by advance four your help,

    PS : Sorry for my Frenchy English…

  4. scienceprog Says:

    Are you using any routers?
    Look again at:
    byte ip[] = { 192, 168, 1, 110 }; // ip in lan
    byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
    byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
    Server server(80); //server port
    could be that your router is blocking port 80, so you should use one that isn’t blocked. And of course double check ip and gateway.

  5. Lars Says:

    Doesn’t work.

    when you compile, it says:
    error: stray ‘\’ in program In function ‘void loop()’:

    And it keeps on saying that.

    Looks like a cool code, doe..

  6. Rogerlette Says:

    Thank’s you Scienceprog,

    I dont really understand why but my rooter was blocking port 80, I try another and It’s now working well…

  7. Rogerlette Says:

    Another question…

    I would like to modify the program in order to accept only connections from a list of clients, do you have any idea to make that kind of filter?

    Thank’s by advance for your help.

  8. Dan Says:

    @Lars:
    You will get these errors if you copy and paste from this page directly to Arduino window. The single and double quotes are not rendered properly. Edit the file in gedit. Use the replace function to change the “open” and “close quotes” to just plain quotes. Same thing with the apostrophes.

    Dan

  9. Yimmy Says:

    Is there a way to easily parse the values from the LED status? for example, if you had an RGB led and you could set the color by a drop down box? 1=red, 2=green, 3=blue. Once it’s in a variable it’s easy but It’s not clear on how to parse those values into a variable.

  10. scienceprog Says:

    All same – just use dropdown form instead of checkbox. Like:

    and then same parsing method with readString.contains(), where you find value like saab.

  11. Fatih Says:

    Hallo

    How can I come to that control page.
    I can’t open that test page.

    thanks

Leave a Reply