Thursday, November 26, 2015

LED Web Server with use of Ethernet

Hello every one, In this post I’ll show how simple is to control things over the Internet using a few things like an 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.
 Connections

The LED library was removed for more compatibility.

What we goanna need?


How can perform the Connections?

Arduino board Ethernet Shield plug (look amazing how they fit). Arduino LED connects to pin. I pin, the use of 7, 6, 5 and 4. You can add 2 buttons on pins 8 and 9. RJ45 cable from your router to connect to the Ethernet Shield.

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. Take attention please avoid use the port 80 or 8080, sometimes these ports are blocked.

Programming…?

With some modifications based on the Arduino sketch webserver.pde example. Easy to download a copy of my sketch is posted above.
I have a web page with more information to the load is to use a few tricks. HTML code for other things we have enough RAM, the program is stored in memory, so. Just ask any questions about the code.

Here's 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();
  }
}

Results:

We get everything so right that we start playing with our small web server can lead. 
Just type in your browser Arduino IP and port and will start to load the Web page. Load your sketch to your Arduino and do not forget to turn it on.
As you can see, with this method it is possible to control anything. With some modification on the program to realize the world is possible.
Any comments, bugs or suggestions, feel free to post here.

2 comments:

  1. Thank you for your sharing! If you want, Could I share this project with other makers over wiznetmuseum.com ??
    WIZnet designed W5100 chip on the ethernet shield and I work for it. I am looking for the projects with Ethernet shield.
    Thank you again, I learn a lot !

    ReplyDelete
  2. PCB Drilling Machine - Printed Circuit Board Drilling Machine Latest Price, Manufacturers & Suppliers
    PCB Drilling | PCB Drilling Machine

    ReplyDelete