3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Ethernet Shield LED SERVER

Step 4The 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 to .pde

Here is the code:


#include <LED.h>
#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);

Server server(8246);

LED led1 = LED(7);
LED led2 = LED(6);
LED led3 = LED(5);
LED led4 = LED(4);

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()
{

Client 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){

led1.toggle();
if(led1.getState()) st1=8;
else st1=9;
led=1;

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

led2.toggle();
if(led2.getState()) st2=8;
else st2=9;
led=2;

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

led3.toggle();
if(led3.getState()) st3=8;
else st3=9;
led=3;

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

led4.toggle();
if(led4.getState()) st4=8;
else st4=9;
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();
}
}

« Previous StepDownload PDFView All StepsNext Step »
1 comment
May 28, 2011. 11:33 PMsapc says:
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

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
31
Followers
5
Author:otaviousp