Home Automation Coding Issues - HTML "Submit" Preventing Webserver Initiation

So I am working on implementing this home automation sketch, and for some reason when i add the 2 commented client.println lines the arduino will not initialize the webserver. it compiles right but will not work.

(i know i dont have any actual IR codes or anything implemented but i just want to get the webpage to serve up right.

looking for any advice

thanks!

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>


byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //I left the MAC address and IP address blank.
byte ip[] = {192,168,1,177 };                     // You will want to fill these in with your MAC and IP address.

EthernetServer server(80); // Assigning the port forwarded number. It's almost always 80.

String readString; // We will be using strings to keep track of things.

int LivingRoomLight = 9;

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

#define W5200_CS  10
#define SDCARD_CS 4

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
  // start the Ethernet connection and the server:
  pinMode(LivingRoomLight, OUTPUT);
  Ethernet.begin(mac, ip);
  Serial.println("Ethernet Begun");  
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {                     // This is all where we start up the server and strings.
        char c = client.read();                     
        if (readString.length() < 100) {
          readString += c;
        } 
        if (c == '\n') {
          Serial.println(readString);                      // And here we begin including the HTML
          client.println("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>");
          client.println("<html>");
          client.println("<head>");
          client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
          client.println("<title>");
          client.println("Blajda Home Automation");
          client.println("</title>");
          client.println("<link rel='stylesheet' type='text/css' href='http://www.ryanblajda.com/wp-content/uploads/2014/09/webserver.css'/>");
          client.println("</head>");
          client.println("<body>");   
          client.println("<center>"); 
          client.println("<h2 class='section'><a href='http://192.168.1.177'>Home</a></h2>");
          client.println("<h2 class='section'><a href='#lighting'>Lighting</a></h2>");
          client.println("<h2 class='section'><a href='#ir'>IR Control</a></h2>");
          client.println("<div id='content'>");
          client.println("<h2><a id='lighting'>Lighting</a></h2>");
          client.println("<h3>Living Room</h3>");
          client.println("Light 1 <input class='left' type='submit' value='Off' onClick=window.location='/?livinglightoff1\'><input class='right' type='submit' value='On' onClick=window.location='/?livinglighton1\'>");
          client.println("<h3>Bedroom</h3>");
          client.println("Light 1 <input class='left' type='submit' value='Off' onClick=window.location='/?bedlightoff1\'><input class='right' type='submit' value='On' onClick=window.location='/?bedlighton1\'>");
          client.println("<h2><a id='ir'>IR Control</a></h2>");
          client.println("<h3>Projector</h3>");
          client.println("Power <input class='left' type='submit' value='Off' onClick=window.location='/?projectoroff\'><input class='right' type='submit' value='On' onClick=window.location='/?projectoron\'>");
          client.println("
");
          client.println("<h3>Apple TV</h3>");
          //client.println("<input class='left' type='submit' value='Up' onClick=window.location='/?appletvup\'><input class='right' type='submit' value='Down' onClick=window.location='/?appletvdown\'>");
          //client.println("<input class='left' type='submit' value='Left' onClick=window.location='/?projectorleft\'><input class='right' type='submit' value='Right' onClick=window.location='/?projectorright\'>");
          client.println("</div>");
          client.println("<center>");
          client.println("</body>");
          client.println("</html>");
          
          delay(1);

          if (readString.indexOf("?livinglighton1") > 0) {
            digitalWrite(9, HIGH);
             Serial.println("On");
          }
          
          else {
            
            if (readString.indexOf("?livinglightoff1") > 0) {
              digitalWrite(9, LOW);
              Serial.println("Off");
            }
          }
          
          readString="";
          client.stop();            // End of session.

        }
      }
    }
  }

}

looking for any advice

OK. I advise you to quit running out of SRAM.

None of those string literals need to be copied from FLASH to SRAM, but they all are. Stop that:
Serial.println(F("Ethernet Begun"));

(Applies to all the client.print() lines, too.)

i tried adding that, and it didnt work, i only saw text it wouldnt render as actual HTML

An example of a lot of static html bundled inside the F() macro to save memory.

//double quote " in html replaced with a single quote' to prevent compile error
//html lines consolidated and put inside F() macro to save memory space

client.print(F("<html><head><meta http-equiv='Content-Language' content='pt-br'>"
"<meta http-equiv='Content-Type' content='text/html; charset=windows-1252'>"
"<title>Unirp - Centro Universitário de Rio Preto</title></head><body>"
"<div align='center'><h1>Centro Universitario de Rio Preto</h1>"
"<h2>Gerenciamento de Volume e Temperatura da Banheira</h2></div>"
"<div align='center'><h4> Pressione o Botão para encher na temperatura desejada</h4>"
"<table border='0' width='50%' cellpadding='2'><tr></tr><tr><td>"
"<!--Botão Temperatura Fria--><form method='POST' action='arduino.php'>"
"<p align='center'><input type='hidden' value='fria' name='tempD' >"
"<input type='submit' value='Água Fria' name='fria'></p></form>"
"<!--Botão Temperatura Morna--><form method='POST' action='arduino.php'>"
"<p align='center'><input type='hidden' value='morna' name='tempD' >"
"<input type='submit' value='Água Morna' name='morna'></p></form>"
"<!--Botão Temperatura Quente--><form method='POST' action='arduino.php'>"
"<p align='center'><input type='hidden' value='quente' name='tempD' >"
"<input type='submit' value='Água Quente' name='quente'></p></form></form>"
"<div align='center'></div></tr></table></div></body></html>"));

oooooh okay. i did that per line, not everything in one client.println();

i did that per line

Which was perfectly reasonable.

i tried adding that, and it didnt work, i only saw text it wouldnt render as actual HTML

So, you changed your code, and didn't show it to us. You ran the code, and saw some results, which you didn't share. And, yet, you still expect us to help you fix your code. Give that some thought, and I'm willing to bet that the next step occurs to you, sooner or later.

no need to get snippy.

Im going to try doing what zoomkat said and post my code and results, oh almighty PaulS. lol

well that did not work either. :~

heres my code.

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>


byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //I left the MAC address and IP address blank.
byte ip[] = {192,168,1,177 };                     // You will want to fill these in with your MAC and IP address.

EthernetServer server(80); // Assigning the port forwarded number. It's almost always 80.

String readString; // We will be using strings to keep track of things.

int LivingRoomLight = 9;

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

#define W5200_CS  10
#define SDCARD_CS 4

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
  // start the Ethernet connection and the server:
  pinMode(LivingRoomLight, OUTPUT);
  Ethernet.begin(mac, ip);
  Serial.println("Ethernet Begun");  
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {                     // This is all where we start up the server and strings.
        char c = client.read();                     
        if (readString.length() < 100) {
          readString += c;
        } 
        if (c == '\n') {
          Serial.println(readString);                      // And here we begin including the HTML
          client.println(F("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>"
          "<html>"
          "<head>"
          "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
          "<title>"
          "Blajda Home Automation"
          "</title>"
          "<link rel='stylesheet' type='text/css' href='http://www.ryanblajda.com/wp-content/uploads/2014/09/webserver.css'/>"
          "</head>"
          "<body>"
          "<center>"
          "<div class='accordion'>"
          "<div id='tab-0'>"
          "<a href='http://192.168.1.177' class='tab'>Home</a>"
          "</div><!--tab0-->"
          "<div id='tab-1'>"
          "<a href='#tab-1' class='tab'>Lighting</a>"
          "<div class='content'>"
          "Living Room 1 <input class='left' type='submit' value='Off' onClick=window.location='/?livinglightoff1#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?livinglighton1#tab-1\'>"
          "</div><!--content-->"
          "</div><!--tab1-->"
          "<div id='tab-2'>"
          "<a href='#tab-2' class='tab'>IR Control</a>"
          "<div class='content'>"
          "Projector"
          "

"
          "<input class='left' type='submit' value='Off' onClick=window.location='/?projectoroff#tab-2\'><input class='right' type='submit' value='On' onClick=window.location='/?projectoron#tab-2\'>"
          "

"
          //client.println("<input class='center' type='submit' value='Up' onClick=window.location='/?projectorup#tab-2\'>");
          //client.println("

");
          //client.println("<input class='left' type='submit' value='Left' onClick=window.location='/?projectorleft#tab-2\'><input class='right' type='submit' value='Right' onClick=window.location='/?projectorright#tab-2\'>");
          //client.println("

");
          //client.println("<input class='left' type='submit' value='Down' onClick=window.location='/?projectordown#tab-2\'>");
          "</div><!--content-->"
          "</div><!--tab2-->"
          "</div>"
          "</center>"
          "</body>"
          "</html>"));
          
          delay(1);

          if (readString.indexOf("?livinglighton1") > 0) {
            digitalWrite(9, HIGH);
             Serial.println("On");
          }
          
          else {
            
            if (readString.indexOf("?livinglightoff1") > 0) {
              digitalWrite(9, LOW);
              Serial.println("Off");
            }
          }
          
          readString="";
          client.stop();            // End of session.

        }
      }
    }
  }

}

Your code includes libraries and such that i don't see a need for. Below is some server test code that works on my setup. You can try it and see if it works in your setup. The below code doesn't include disabling an SD card if inserted, so remove the SD card if inserted. You can open the serial monitor and see the commands received by the arduino.

//zoomkat 8-04-12
//simple button GET server code to control servo and arduino pins 5, 6, and 7
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use ' instead of " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
///note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

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

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

String readString; 

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

void setup(){

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

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server multi pin button test 1.0"); // 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);
        } 

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

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

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

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          // For simple testing, pin 5, 6, 7, and 8 are used in buttons
          // DIY buttons
          client.println("<a href=/?on2 >ON</a>"); 
          client.println("<a href=/?off3 >OFF</a>"); 
          client.println("&nbsp;<a href=/?off3579 >ALL OFF</a>

"); 

          // mousedown buttons
          client.println("<input type=button value=ON onmousedown=location.href='/?on4'>"); 
          client.println("<input type=button value=OFF onmousedown=location.href='/?off5'>");        
          client.println("&nbsp;<input type=button value='ALL OFF' onmousedown=location.href='/?off3579'>

");        
                   
          // mousedown radio buttons
          client.println("<input type=radio onmousedown=location.href='/?on6'>ON</>"); 
          client.println("<input type=radio onmousedown=location.href='/?off7'>OFF</>"); 
          client.println("&nbsp;<input type=radio onmousedown=location.href='/?off3579'>ALL OFF</>

");    
   
          
          // custom buttons
          client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on8'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off9'>");
          client.print("&nbsp;<input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/?off3579'>");

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          }
          if(readString.indexOf('3') >0)//checks for 3
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          }
          
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
          
           if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println("Led 7 On");
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println("Led 7 Off");
          }     
          
            if(readString.indexOf('8') >0)//checks for 8
          {
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println("Led 8 On");
          }
          if(readString.indexOf('9') >0)//checks for 9
          {
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println("Led 8 Off");
          }         
             
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

since i have a shield with the w5200 wiznet chip i have to disable the SD card if its not in use or the ethernet will not enable at all.

also if you dont mind lending any other advice, i was trying to modify my code so that it would print the On button when the light is off, and the Off Button when the light is on, but it seems i cannot get it to work...

this is the code i tried to implement.

if (analogRead(LivingRoomLight) == HIGH) {
            client.println("Living Room 1 <input class='left' type='submit' value='Off' onClick=window.location='/?livinglightoff1#tab-1\'>");
          }
          
          else if (analogRead(LivingRoomLight) != HIGH) {
              client.println("Living Room 1 <input class='right' type='submit' value='On' onClick=window.location='/?livinglighton1#tab-1\'>");
          }
          
          else {
            
          }
          
          if (readString.indexOf("?livinglighton1") > 0) {
            digitalWrite(9, HIGH);
             Serial.println("On");
          }
          
          else {
            
            if (readString.indexOf("?livinglightoff1") > 0) {
              digitalWrite(9, LOW);
              Serial.println("Off");
            }
          }
if (analogRead(LivingRoomLight) == HIGH) {

It is unlikely that analogRead(), which reads an analog pin, is going to return 1. If, indeed, a digital device is attached to it, the reading is more likely to be 0 or 1022+.

Earlier, it was suggested that you might be running out of memory, and yet here are MORE string literals in SRAM. Have you measured how much free memory you have?

dammit i cant believe i used analogRead. what an idiot.

the only reason i asked the if then question was because I could not get the client.println(F(""); to work and wanted to work on something else.

since i have a shield with the w5200 wiznet chip i have to disable the SD card if its not in use or the ethernet will not enable at all.

I don't have an ethernet shield with the w5200 chip, but with the w5100 chip the SD card disable is only required if an SD card is actually inserted in the SD card slot. If you have an SD card in the SD slot, remove it for the code I posted. Your code may have other issues.

the only way i got the shield to work was with a modified version of the Ethernet library and disabling the SD Card when its not in use. It should not be impeding anything else.


unrelated to the SD card and SRAM issues

even with digitalRead it wont print the else statement.... GAAAHH

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

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //I left the MAC address and IP address blank.
byte ip[] = {
  192,168,1,177 };                     // You will want to fill these in with your MAC and IP address.

EthernetServer server(80); // Assigning the port forwarded number. It's almost always 80.

String readString; // We will be using strings to keep track of things.

int LivingRoomLight = 9;

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

#define W5200_CS  10
#define SDCARD_CS 4

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
  // start the Ethernet connection and the server:
  pinMode(LivingRoomLight, OUTPUT);
  Ethernet.begin(mac, ip);
  Serial.println("Ethernet Begun");
  digitalWrite(LivingRoomLight, LOW); 
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {                     // This is all where we start up the server and strings.
        char c = client.read();                     
        if (readString.length() < 100) {
          readString += c;
        } 
        if (c == '\n') {
          Serial.println(readString);          // And here we begin including the HTML
          client.println("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>");
          client.println("<html>");
          client.println("<head>");
          client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
          client.println("<title>");
          client.println("Blajda Home Automation");
          client.println("</title>");
          client.println("<link rel='stylesheet' type='text/css' href='http://www.ryanblajda.com/wp-content/uploads/2014/09/webserver.css'/>");
          client.println("</head>");
          client.println("<body>");
          client.println("<center>"); 
          client.println("<div class='accordion'>");
          client.println("<div id='tab-0'>");
          client.println("<a href='http://192.168.1.177' class='tab'>Home</a>");
          client.println("</div><!--tab0-->");
          client.println("<div id='tab-1'>");
          client.println("<a href='#tab-1' class='tab'>Lighting</a>");
          client.println("<div class='content'>");

          if (digitalRead(LivingRoomLight) == LOW) {
            client.println("Living Room 1 <input class='left' type='submit' value='On' onClick=window.location='/?livinglighton1#tab-1\'>");
            //Serial.println(digitalRead(LivingRoomLight));
          }

          else {
              client.println("Living Room 1 <input class='right' type='submit' value='On' onClick=window.location='/?livinglightoff1#tab-1\'>");
              //Serial.println(digitalRead(LivingRoomLight));
            }

          if (readString.indexOf("?livinglighton1") > 0) {
            digitalWrite(9, HIGH);
            Serial.println("On");
          }

          else {

            if (readString.indexOf("?livinglightoff1") > 0) {
              digitalWrite(9, LOW);
              Serial.println("Off");
            }
          }

          client.println("</div><!--content-->");
          client.println("</div><!--tab1-->");
          client.println("<div id='tab-2'>");
          client.println("<a href='#tab-2' class='tab'>IR Control</a>");
          client.println("<div class='content'>");
          client.println("Projector");
          client.println("

");
          client.println("<input class='left' type='submit' value='Off' onClick=window.location='/?projectoroff#tab-2\'><input class='right' type='submit' value='On' onClick=window.location='/?projectoron#tab-2\'>");
          client.println("

");
          //client.println("<input class='center' type='submit' value='Up' onClick=window.location='/?projectorup#tab-2\'>");
          //client.println("

");
          //client.println("<input class='left' type='submit' value='Left' onClick=window.location='/?projectorleft#tab-2\'><input class='right' type='submit' value='Right' onClick=window.location='/?projectorright#tab-2\'>");
          //client.println("

");
          //client.println("<input class='left' type='submit' value='Down' onClick=window.location='/?projectordown#tab-2\'>");
          client.println("</div><!--content-->");
          client.println("</div><!--tab2-->");
          client.println("</div>");
          client.println("</center>");
          client.println("</body>");
          client.println("</html>");

          delay(1);

          /*if (readString.indexOf("?livinglighton1") > 0) {
           digitalWrite(9, HIGH);
           Serial.println("On");
           }
           
           else {
           
           if (readString.indexOf("?livinglightoff1") > 0) {
           digitalWrite(9, LOW);
           Serial.println("Off");
           }
           }*/

          readString="";
          client.stop();            // End of session.

        }
      }
    }
  }

}
          "<html>"
          "<head>"
          "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
          "<title>"
          "Blajda Home Automation"
          "</title>"
          "<link rel='stylesheet' type='text/css' href='http://www.ryanblajda.com/wp-content/uploads/2014/09/webserver.css'/>"
          "</head>"
          "<body>"

I just want to point out that this text has no newlines in it. However I tested that as a web page and it seemed to work without them.

the only reason i asked the if then question was because I could not get the client.println(F(""); to work and wanted to work on something else.

What do you mean by that? The code you posted compiled OK for me.

and it didnt work

You need to be a lot more specific than say things "don't work" or not. Don't compile? Return unexpected results? If so, what results? Copy and paste or screenshot.

Ok so when i upload this code below to the arduino and try to view the webpage i get.....the image attached.

I printed your text to the serial monitor, saved to a disk file (test.html) and it opened up showing your buttons. So the problem is not with the HTML.

You might need a HTTP header sent first, or it will interpret the rest as pure text.

i seeee. that makes sense. i will try that. any ideas on the if then issue?

Perhaps if you explain this "if then" issue in more detail ...

What I mean by the HTTP header is, before you send the HTML (the DOCTYPE onwards) send this:

      client.println (F("HTTP/1.1 200 OK"));
      client.println (F("Server: My Arduino Server"));
      client.println (F("Connection: close"));
      client.println (F("Content-Type: text/html; charset=iso-8859-1"));
      client.println ();

The blank line is important.

That tells the client that the content type is HTML.