Ethernet Shield / Questions

Hello All,
I am trying to develop a program for test the ethernet shield. I found a program for home automation that I was planning to use. Starting working well until I add another variable called 'prueba' as the last one to turn on/off a led.
When I add this variable in the Setup() , define it as an OUTPUT add the 'if' scenarios and finally add the HTML code for the buttons, the web page did not response.

Please help me to find why this problem occurs.

THANKS!

Here is the code (probably you already see this code before):

#include <String.h>
#include <Ethernet.h>
#include <SPI.h>

/* Simple Ethernet Test
Arduino server output simple text to browser
and controlling LED with simple checkbox */

byte mac[]={ 0x90, 0xA2, 0xDA, 0x05, 0x00, 0x51 };
byte ip[]={192,168,2,10}; // IP del Arduino
byte gateway[]={192,168,2,1}; // Acceso a Internet
byte subnet[]={255,255,255,0}; // subnet mask

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

byte sampledata=3;
// Salidas para encender las luces
int cuartocurvo=2;
int comedor=3;
int sala=4;
int cocina=5;
int lavanderia=6;
int cuartopadres=7;
//int prueba=8; // VARIABLE THAT MAKES HTML NO RESPONSE
//int constante=0;

char link[]="http://www.gloriayjavier.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);
// Config. pines Luces como salidas
pinMode(cuartocurvo,OUTPUT);
pinMode(comedor,OUTPUT);
pinMode(sala,OUTPUT);
pinMode(cocina,OUTPUT);
pinMode(lavanderia,OUTPUT);
pinMode(cuartopadres,OUTPUT);
//pinMode(prueba,OUTPUT); //WHEN I ADD THIS OUTPUT THE HTML WEB DID NOT RESPONSE
//enable serial data print
Serial.begin(9600);
}

void loop(){
// Create a cliend connection
EthernetClient 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 character to string
readString += c;} //replaces readString.append(c);

//Output chart 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
//Checar si el LED debe estar prendido
if(readString.indexOf("L=prende")>0){
//Led has to be turned ON
digitalWrite(cuartocurvo,HIGH); // Enciende el LED
LEDON=true;
}

else if(readString.indexOf("L=apaga")>0){
//Led has to be turned ON
digitalWrite(cuartocurvo,LOW); // Enciende el LED
LEDON=false;
}
if(readString.indexOf("C=prende")>0){
//Led has to be turned ON
digitalWrite(comedor,HIGH); // Enciende el LED
LEDON=true;
}

else if(readString.indexOf("C=apaga")>0){
//Led has to be turned ON
digitalWrite(comedor,LOW); // Enciende el LED
LEDON=false;
}
if(readString.indexOf("S=prende")>0){
//Led has to be turned ON
digitalWrite(sala,HIGH); // Enciende el LED
LEDON=true;
}

else if(readString.indexOf("S=apaga")>0){
//Led has to be turned ON
digitalWrite(sala,LOW); // Enciende el LED
LEDON=false;
}
//COCINA
if(readString.indexOf("Co=prende")>0){
//Led has to be turned ON
digitalWrite(cocina,HIGH); // Enciende el LED
LEDON=true;
}

else if(readString.indexOf("Co=apaga")>0){
//Led has to be turned ON
digitalWrite(cocina,LOW); // Enciende el LED
LEDON=false;
}
//LAVANDERIA
if(readString.indexOf("La=prende")>0){
//Led has to be turned ON
digitalWrite(lavanderia,HIGH); // Enciende el LED
LEDON=true;
}

else if(readString.indexOf("La=apaga")>0){
//Led has to be turned ON
digitalWrite(lavanderia,LOW); // Enciende el LED
LEDON=false;
}

// CUARTO PADRES
if(readString.indexOf("Pa=prende")>0){
//Led has to be turned ON
digitalWrite(cuartopadres,HIGH); // Enciende el LED
LEDON=true;
}
else if(readString.indexOf("Pa=apaga")>0){
digitalWrite(cuartopadres,LOW);
LEDON=false;
}
// WHEN I ADD THIS PART HTML WEB PAGE DID NOT WORK "****
/if(readString.indexOf("Pu=prende")>0){
//Led has to be turned ON
//constante=readString.indexOf("Pu=prende");
//Serial.println("indice PU prende:"+ constante);
digitalWrite(prueba,HIGH); // Enciende el LED
LEDON=true;
}
else if(readString.indexOf("Pu=apaga")>0){
//Led has to be turned ON
digitalWrite(prueba,LOW); // Enciende el LED
LEDON=false;
} /
//
***************************************************

//****** CREANDO LA PAG. WEB********

//Now Output HTML data staring with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//set background to white
client.print("");
//send first heading
client.println("

Gloria's House!

");
client.println("
");
client.println("
");
//output some sample data to browser
client.println("Luces a Controlar: ");
client.print(sampledata);//lets output some data
client.println("
");//some space between lines
client.println("
");
//printing gloria& javier link
client.println("Link: ");
client.print("Gloria y Javier");
client.println("
");
client.println("
");
//controlling led via checkbox
client.println("

CONTROL DE LUCES

");
client.println("R&D Room
");
client.println("Comedor
");
client.println("Sala
");

client.println("Cocina
");
client.println("Lavanderia
");
client.println("Cuarto Padres
");
//client.println("Prueba
"); //THIS LAST BUTTON MAKES WEB PAGE TO STOP WORKING WELL.
client.println("
");
//printing LED status
client.print("LED status: ");

if (LEDON)
client.println("ON");
else
client.println("OFF");
client.println("


");
client.println("
");
client.println("");
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}
//*******************************

lucescasa.ino (7.57 KB)

Code Tags

and finally add the HTML code for the buttons, the web page did not response.

All the static html my be running you out of memory. Try using the F() macro like below for the html to see if that will fix the issue.

//zoomkat 4-05-12
//web LED code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ') 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//turns pin 5 on/off

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  192, 168, 1, 102 }; // arduino server ip in lan
byte gateway[] = { 
  192, 168, 1, 1 }; // internet access via router gateway
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //arduino server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient 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; 
          //Serial.print(c); //print what server receives to serial monitor
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString);

          //now output HTML data header

          client.println(F("HTTP/1.1 200 OK"));
          client.println(F("Content-Type: text/html"));
          client.println();

          client.println(F("<HTML><HEAD><TITLE>Arduino GET test page</TITLE></HEAD><BODY><H1>Zoomkat's simple Arduino button</H1><a href='/?on''>ON</a>&nbsp;<a href='/?off''>OFF</a></BODY></HTML>"));

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Hello zoomkat,

Thank you very much for your help. I add that feature and change the condition to read the strings.

if (c== '\n'){
//dirty skip of "GET /favicon.ico HTTP/1.1"
String control = readString.substring(6,13); //This will read the part of the String that contains the command (e.g. L1=1)
Serial.println(control); //testing purposes
//Cuarto de Renata
if (control == "R=prend"){
digitalWrite(cuartocurvo,HIGH); // Enciende el LED
LEDON=true;
}

if(control == "R=apaga"){
//Led has to be turned OFF
digitalWrite(cuartocurvo,LOW); // Enciende el LED
LEDON=false;
}

....................
Also as you mention me, I use the F() macro.
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println();
//set background to white
client.print(F(""));................................

It works now. But I am looking for information about this F()macro because I do not understand his function. Please if you have a link or tutorial let me know.