Introduction: Ethernet Shield LED SERVER

Hey guys, in this instructable I will show how simple is to control things over the Internet using a few things like a Arduino Board, an Ethernet Shield and some LEDs to show the results. The arduino will emulate a Web Server and after receives some command will turn on or off the LED.

UPDATE 27/01/13
Works on arduino IDE 1.0.1
The LED library was removed for more compatibility

Step 1: What We Gonna Need?

- Arduino Board
- Ethernet Shield ( with wiznet chip version)
- Led Board (use an old led board from a POV circuit)
- Wires

Step 2: Plugging All Together

Plug the Ethernet shield at the arduino board (look how amazing they fit).

Connect the LED to the arduino PIN. I use the pins, 7,6,5 and 4 for this.

You can add 2 button on pin 8 and 9.

Connect the RJ45 cable from the Ethernet shield to your router.

Next Step, going online!

Step 3: How to Configure Your Router

My router is a WRT54G from linksys, a wireless router with 4 LAN ports. The only thing you have to do to gain access through internet is Port forward the port you use on your server. In my case I use the port 8246 and a free local IP. TIP: avoid use the port 80 or 8080, sometimes this ports are blocked.

This site can help you to "Port forward" base on your router/modem. http://portforward.com/english/routers/port_forwarding/Linksys/WRT54G/default.htm

Step 4: The Program

The arduino sketch is based on the webserver.pde example with some modifications. A copy of my sketch is post above for easy download.
I have to use some tricks to load a web page with more information. The HTML code is stored at the program memory, so, we have enough RAM for the other things. Any questions about the code just ask.

TIP: after download the file, rename it from .tmp to .ino if necessary

Here is the code:


#include <Ethernet.h>
#include <SPI.h>
#include <avr/pgmspace.h>

prog_char string_0[] PROGMEM = "<html><body><h2>Controle de LED pela Internet</h2><font size= 4><form method=GET>";
prog_char string_1[] PROGMEM = "<br><input type=submit name=b1 value=Led1>";
prog_char string_2[] PROGMEM = "<br><input type=submit name=b2 value=Led2>";
prog_char string_3[] PROGMEM = "<br><input type=submit name=b3 value=Led3>";
prog_char string_4[] PROGMEM = "<br><input type=submit name=b4 value=Led4>";
prog_char string_5[] PROGMEM = "";  //"<br>Insert your name here:";
prog_char string_6[] PROGMEM = "";  //"<input name=msg value=no_name MAXLENGTH=20>";
prog_char string_7[] PROGMEM = "</form></body></html>";
prog_char string_8[] PROGMEM = "Ligada (ON)";
prog_char string_9[] PROGMEM = "Desligada (OFF)";

prog_char string_10[] PROGMEM = "<meta http-equiv=refresh content=30 > "; //for auto refresh

PROGMEM const char *string_table[] =     // change "string_table" name to suit
{  
  string_0,
  string_1,
  string_2,
  string_3,
  string_4,
  string_5,
  string_6,
  string_7,
  string_8,
  string_9,
  string_10
};

char buffer[85];    // make sure this is large enough for the largest string it must hold


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 134 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };

String inString = String(35);

EthernetServer server(8246);


boolean led1 = false;
boolean led2 = false;
boolean led3 = false;
boolean led4 = false;

String msg="";
int tam=0;
int st1=9,st2=9,st3=9,st4=9;

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip,gateway,subnet);
  server.begin();
  Serial.println("Serial READY");
  Serial.println("Ethernet READY");
  Serial.println("Server READY");
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
}

void loop()
{

  EthernetClient client = server.available();

  int led=0;
  if (client) {

    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {

      if (client.available()) {

        char c = client.read();
        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (inString.length() < 35) {
            inString.concat(c);

         }
        if (c == '\n' && current_line_is_blank) {

          if(inString.indexOf("b1")>0){

             if(led1==false){
               st1=8;
               led1=true;
               digitalWrite(4,HIGH);
             }
             else{
               st1=9;
               led1=false;
               digitalWrite(4,LOW);
             }
             led=1;

           }
           if(inString.indexOf("b2")>0){

             if(led2==false){
               st2=8;
               led2=true;
               digitalWrite(5,HIGH);
             }
             else{
               st2=9;
               led2=false;
               digitalWrite(5,LOW);
             }
             led=2;

           }
           if(inString.indexOf("b3")>0){

             if(led3==false){
               st3=8;
               led3=true;
               digitalWrite(6,HIGH);
             }
             else{
               st3=9;
               led3=false;
               digitalWrite(6,LOW);
             }
             led=3;

           }
           if(inString.indexOf("b4")>0){

             if(led4==false){
               st4=8;
               led4=true;
               digitalWrite(7,HIGH);
             }
             else{
               st4=9;
               led4=false;
               digitalWrite(7,LOW);
             }
             led=4;

           }
           /*
           if(inString.indexOf("msg")>0){
              char charBuf1[50];
              char charBuf2[50];
              strcpy(msg,(char*)inString.substring(inString.indexOf("g")+2,inString.indexOf(" H")));                        
              //Serial.print("msg: ");
              Serial.println(msg);
           }
         */
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          strcpy_P(buffer, (char*)pgm_read_word(&(string_table[0]))); // Necessary casts and dereferencing, just copy.
          client.println( buffer );
          for (int i = 1; i < 8; i++)
          {
            strcpy_P(buffer, (char*)pgm_read_word(&(string_table[i]))); // Necessary casts and dereferencing, just copy.
            client.println( buffer );
            switch(i){

              case 1: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st1]))); client.println( buffer ); break;
              case 2: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st2]))); client.println( buffer ); break;
              case 3: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st3]))); client.println( buffer ); break;
              case 4: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st4]))); client.println( buffer ); break;

            }
            delay(30);
          }
          if(digitalRead(8)==HIGH){
            client.println("<br>Botao 1, ON");
          }else{
            client.println("<br>Botao 1, OFF");
          }
          if(digitalRead(9)==HIGH){
            client.println("<br>Botao 2, ON");
          }else{
            client.println("<br>Botao 2, OFF");
          }

         //strcpy_P(buffer, (char*)pgm_read_word(&(string_table[10]))); client.println( buffer );


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

Step 5: Results

If we did everything right we can start playing with our little led web server.

Just type on your browser the arduino IP and PORT and the web page will start to loading. Don't forget to load your sketch at your arduino and turn it on.

As you can see, it is possible to control anything with this method. And is possible to sense the world with a few modification on the program.

Any comments ,bugs or suggestion please let me know.

If you like it, please rate it, :)