Pages: [1]   Go Down
Author Topic: Ethernet Shield / Questions  (Read 1230 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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("<body style=background-color:white>");
          //send first heading
          client.println("<font color='red'><h1>Gloria's House!</font></h1>");
          client.println("<hr />");
          client.println("<hr />");
          //output some sample data to browser
          client.println("<font color='blue' size='5'>Luces a Controlar: ");
          client.print(sampledata);//lets output some data
          client.println("<br />");//some space between lines
          client.println("<hr />");
          //printing gloria& javier link
          client.println("<font color='blue' size='5'>Link: ");
          client.print("<a href=");
          client.print(link);
          client.println(">Gloria y Javier</a>");
          client.println("<br />");
          client.println("<hr />");
          //controlling led via checkbox
          client.println("<h1>CONTROL DE LUCES</h1>");
          client.println("<form method=get name=LED>R&D Room<br><input type=submit name=L value=prende><input type=submit name=L value=apaga></form>");
          client.println("<form method=get name=LED>Comedor<br><input type=submit name=C value=prende><input type=submit name=C value=apaga></form>");
          client.println("<form method=get name=LED>Sala<br><input type=submit name=S value=prende><input type=submit name=S value=apaga></form>");
         
          client.println("<form method=get name=LED>Cocina<br><input type=submit name=Co value=prende><input type=submit name=Co value=apaga></form>");
          client.println("<form method=get name=LED>Lavanderia<br><input type=submit name=La value=prende><input type=submit name=La value=apaga></form>");
          client.println("<form method=get name=LED>Cuarto Padres<br><input type=submit name=Pa value=prende><input type=submit name=Pa value=apaga></form>");
          //client.println("<form method=get name=LED>Prueba<br><input type=submit name=Pu value=prende><input type=submit name=Pu value=apaga></form>"); //THIS LAST BUTTON MAKES WEB PAGE TO STOP WORKING WELL.
          client.println("<br />");
          //printing LED status
          client.print("<font size='5'>LED status: ");
         
          if (LEDON)
            client.println("<font color='green' size='5'>ON");
          else
            client.println("<font color='grey' size='5'>OFF");
            client.println("<hr />");
            client.println("<hr />");
            client.println("</body></html>");
            //clearing string for next read
            readString="";
            //stopping client
          client.stop();
        }
      }
    }
  }
}
//*******************************


* lucescasa.ino (7.57 KB - downloaded 5 times.)
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code Tags
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.


Code:
//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="";

        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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("<body style=background-color:white>"));................................

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.

Logged

Pages: [1]   Go Up
Jump to: