Position: Index > Unclassified >

Getting hands on Arduino Ethernet Shield

2017-11-27 11:14  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

 

Updated! Arduino Ethernet code

Since last Arduino Ethernet code there we several Arduino IDE releases with changes that affected the code listed in this post. Due high interest, we updated it with minor changes that makes it work as expected. The biggest occurred because Wstring.h library isn’t no longer in use, because String.h library is included in core that brings some difference in several functions used in code.

In code we need to write

readString = c;instead readString.append(c);

if(readString.indexOf(“L=1″) >0)instead if(readString.contains(“L=1″))

Also we need to re-import Ethernet.h library in order to bring along all necessary libraries like Client.h, Server.h, SPI.h, Udp.h.

The other problem occurred when program run was that LED actually never lights up when checkbox is selected. I usedSerial.print(c);to track down the problem. And it seems that method GET sends two strings:

Our code was catching and analyzing both strings. We only need to take string where parameter is sent: “GET /?L=1 HTTP/1.1” and skip “GET /favicon.ico HTTP/1.1”. As our example is very simple we can see ,that second string lacks “?” symbol. So we check the string if there is a ‘?’ symbol, of not, skip whole analyze.

And last fix is applied to LED checkbox. Now it stays checked if LED is ON. Hope you find these changes useful.

Here is a code listing:

#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 110 };			// ip in lan
byte gateway[] = { 192, 168, 0, 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() < 100)
      {
        //store characters to string
        readString  = c; //replaces readString.append(c);
      }
        //output chars to serial port
        Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
          //dirty skip of "GET /favicon.ico HTTP/1.1"
          if (readString.indexOf("?") <0)
          {
            //skip everything
          }
          else
          //lets check if LED should be lighted
        if(readString.indexOf("L=1") >0)//replaces 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
          if (LEDON)
          client.println("<form method=get name=LED><input type=checkbox name=L value=1 CHECKED>LED<br><input type=submit value=submit></form>");
          else
          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();
            }
          }
        }
      }
 }

And you can download Arduino sketch ethtest.

****

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 likeWString.hwhich 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=1Arduino 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 likeappendandcontains(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.

You can also purchase cheaper Arduino UNO or Arduino Mega from dealextreme.

Download project file here: ethtest


Reprinted Url Of This Article:
http://www.scienceprog.com/getting-hands-on-arduino-ethernet-shield/