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

awesome i will test out the header and see if that fixes it. also here is the code in question regarding the if then statement.

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");
           }
         }

Well? What is the question about that code?

ah. yes. that would be helpful.

the code is supposed to print the first line if the light is off, and the second when it is on, but it doesnt seem to be working.

Once again I must ask you to be more specific. "Not working" could mean anything. Does the Arduino burst into flame?

Does it print the first line, the second line, both lines or no lines?

no matter the state of the LED it only prints the if statement.

i just adjusted the code to what is below, and the code will change if i click the button to turn the LED on, then refresh the page the button will change to the OFF mode, but not immediately after clicking the button. im not sure why.

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 {
            if (digitalRead(LivingRoomLight) == HIGH) {
              client.println("Living Room 1 <input class='right' type='submit' value='Off' onClick=window.location='/?livinglightoff1#tab-1\'>");
              Serial.println(digitalRead(LivingRoomLight));
            }
          }

Can you please try to be clearer.

it only prints the if statement

What do you mean by that?

and the code will change if i click the button

What code?

I suspect you are not understanding the client/server nature of HTTP communications. The page won't refresh on your web browser just because something happens on the Arduino. You need to get a new page ("refresh the page") for the new status to appear.

Can you post all your code please, and try to describe what is happening, exactly.

The state of the switch has NOTHING to do with the state of the LED after the snippet you keep posting is done. Why do you expect them to have something to do with each other? Why would the web browser use care about the state of the switch?

here is the full code.

so what is supposed to happen, is the arduino prints a line which includes a HTML button based on the state of boolean variable, since i seem to have more success with that. when i click the button i believe the page refreshes, so the line printed should change. it does not so i assume you are right and the page is not actually refreshing.

#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;

boolean lighton = false;

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

#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'>");

/// THIS PART RIGHT HERE SHOULD CHANGE BASED ON THIS VARIABLE (WHICH IS CHANGED WHEN THE BUTTON IS CLICKED AND THE LED PIN BECOMES HIGH

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

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

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

          else {

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

          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><!--accordion-->");
          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.

        }
      }
    }
  }

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

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

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

          else {

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

Putting aside other possible problems, you are testing lighton variable first, and then later on, setting the variable. How does that work?

Why will you not show us the serial output that you get? Why will you not describe what happens when you click the icons on the browser?

         if (lighton == false) {
  ...
          }
          else {
            if (lighton == true) {
  ...
            }
          }

Why test for lighton being false and true? Is there a third possibility?

Why not:

         if (lighton == false) 
          {
  ...
          }
          else 
          {
  ...
          }

And, what PaulS said.

well when the program void setup is run the variable is set to false, and the LED written LOW. so the if else statement should be able to read the state of the LED and variable since it has been set to low....

the output i see from the serial monitor is as follows.

there is no third possibility but i used the similar if else scheme as the if else that writes the LED HIGH and LOW since it worked.