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, :)
1 Person Made This Project!
- jbike made it!
32 Discussions
4 years ago
please reply me as soon as possible .shashibhushan3535@gmail.com
4 years ago
sir i have to do project on "e-notice board (led matrix 8by 40) using arduino ethernet
sheild board" .can you provide me code for this .Here i have to scroll the text also.
6 years ago on Introduction
Hi If I need to see weather the LED is ON or OFF then what changes is to be made in programming to see the status of LED on web page
6 years ago on Step 5
hello thanx for this example....
im a begininger... i want some help for Coding Ethernet shield n also arduino....
i want to do a simple project to control Relays from webpage..
i hav trouble in writing the sketch....
can u plez send me the sketch... il send u all the details.. can u give me ur email... or mail me on pramitsawant11@gmail.com
7 years ago on Introduction
Thanks for sharing the project with us! Any idea why when I send the link, the IP address and the PORT, It does not open with them ?!
Reply 7 years ago on Introduction
What is your gateway IP from your router?
7 years ago on Introduction
good day?? i would like to ask why am i still getting this error?
'LED' does not name a type
thankyou!!
Reply 7 years ago on Introduction
Hey,
I removed this library because it was obsolete
Try the new code that I upload.
Cya
8 years ago on Introduction
CAN I HAVE THE SCHEMATIC DIAGRAM? THANKYOU
BTW, WHAT IF INSTEAD OF LED, I WILL USE ETHERNET TO CONTROL OUTLETS, IS THERE ANY CHANGES IN THE CODE?
THANKYOU!!
Reply 8 years ago on Introduction
Here is a link to a schematic that you can adapt for your needs. http://www.arduinors.net/blog/wp-content/uploads/2008/11/npn_driver.gif
8 years ago on Step 4
Hey, you will gonna need this library,
Here is the link, www.arduino.cc/playground/Code/LED
cya
8 years ago on Step 4
hello, first thank you for the great presentation, it is really clear and lean!
however i got some errors on my program, can you point me how to fix it?
sketch_jan09a:46: error: 'LED' does not name a type
sketch_jan09a:47: error: 'LED' does not name a type
sketch_jan09a:48: error: 'LED' does not name a type
sketch_jan09a:49: error: 'LED' does not name a type
sketch_jan09a.cpp: In function 'void loop()':
sketch_jan09a:74: error: 'class Server' has no member named 'available'
sketch_jan09a:74: error: cannot declare variable 'client' to be of abstract type 'Client'
sketch_jan09a:97: error: 'led1' was not declared in this scope
sketch_jan09a:105: error: 'led2' was not declared in this scope
sketch_jan09a:113: error: 'led3' was not declared in this scope
sketch_jan09a:121: error: 'led4' was not declared in this scope
i know that my LED library is missing, also the avr/pgmspace library, where can i get thouse?
thanks
8 years ago on Step 2
What if you don't have access to a router? (e.g apartment building, hotel rooms...) Can I use the ethernet port on my win7 computer?
Reply 8 years ago on Step 2
I think you can, Never try this way, I think you have to set the sub mask and the gateway manually on your computer.
8 years ago on Introduction
Should I choose a random MAC address? Or is there some way to find out what to use?
9 years ago on Introduction
Olá, eu consegui carregar o código com sucesso, mas não consigo acessar a página. ao digitar o ip eu só obtenho página não encontrada. eu testei pingar o ip e ele responde.
ficquei com dúvida também sobre o mac adress, eu posso adotar o que está no seu código?
Reply 9 years ago on Introduction
Voce alterou o IP do programa para o ip que voce usa na sua Rede?
Por exemplo, tem gente que usa 192.168.0.xxx outros usam 192.168.1.xxx ou ate mesmo 10.1.1.xxx.
Verifica se o IP que ta no programa do arduino nao ta conflitando com outra maquina na rede.
E por fim, o MAC voce pode alterar ou deixar o mesmo. Como esta dando problema, mudo o endereco tbm.
Se nao der certo de novo. Me avise.
9 years ago on Step 4
it keeps giving me this error
sketch_may29a.cpp: In function 'void loop()':
sketch_may29a:84: error: 'class String' has no member named 'append'
sketch_may29a:84: error: expected `;' before '{' token
sketch_may29a:89: error: 'class String' has no member named 'contains'
sketch_may29a:97: error: 'class String' has no member named 'contains'
sketch_may29a:105: error: 'class String' has no member named 'contains'
sketch_may29a:113: error: 'class String' has no member named 'contains'
sketch_may29a:122: error: 'class String' has no member named 'contains'
sketch_may29a:124: error: cannot convert 'String' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)'
sketch_may29a:152: error: expected `}' at end of input
sketch_may29a:152: error: expected `}' at end of input
sketch_may29a:152: error: expected `}' at end of input
and i have WStrings and led libraries installed
11 years ago on Introduction
Hello Otaviousp, Thank you for this tutorial, I am currently rewriting it without the twittercode and with loads of comments so me (and others looking at it) will understand more what happens in the code :) I wonder though, you have two switch commands doing seemingly the same thing, switch(i) (with four cases) and then below switch(led) with four cases. What does the second switch do? Is it just for writing to Twitter? And thanks again for the code! :)
Reply 9 years ago on Introduction
Hello Sourcery,
could you post me the sketch without the twittercode please? Or where could I find it?
Thanks a lot. :)