Web Server Contol Help Needed

I have an Arduino Uno and an Arduino ethernet shield. I am trying to have two inputs and two outputs, displayed on a webpage and send commands back to the arduino to trigger the output pins for 500ms. Here is what I have so far. The inputs(sensors) work well, but will only update on refresh. When I refresh the browser send which ever command was last, trigger gragae one or two. Anyway. What is wrong with this code that triggers the last command on refresh? How can I get this to auto refresh?

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xC2, 0x52 };
byte ip[] = { 
  192,168,1, 30 };

EthernetServer server(81);

int Door1 = 2; //Digital pin 2 for garage door 1
int Door2 = 3; //Digital pin 3 for garage door 2
int sensor1 = A0; //Analog sensor pin for door 1
int sensor2 = A1; //Analog sensor pin for door 2

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
  pinMode(Door1, OUTPUT);
  pinMode(Door2, OUTPUT);
  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
}
#define BUFSIZ 100  //Buffer size for getting data
char clientline[BUFSIZ];  //string that will contain command data
int index = 0;  //clientline index
void loop()
{
  index=0;  //reset the clientline index
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if(index<BUFSIZ)  //Only add data if the buffer isn't full.
        {
          clientline[index]=c;
          index++;
        }
        if (c == '\n' && currentLineIsBlank)
        {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h1><center>Garage Control</h1></center>
<center><form method=get action=/?><input type=radio name=L1>Door 1
<input type=radio name=L2>Door 2
<input type=submit value=submit></form></center>");
          break;
        }
        if(strstr(clientline,"L1")) {  //look for the command to open the door
          digitalWrite(Door1, HIGH);    // opens the 1st garage door
	  delay(50);
	  digitalWrite(Door1, LOW);
        }
        if(strstr(clientline,"L2")) {  //look for the command to open the door
          digitalWrite(Door2, HIGH);    // opens the 2nd garage door
	  delay(50);
	  digitalWrite(Door2, LOW);
        }
      }
    }
int val1 = analogRead(sensor1);
int val2 = analogRead(sensor2);
     
        if (val1 < 100)
{
  client.println("<h1><center>Garage door 1 is closed</h1></center>
");
  }
else  if (val1 >= 100)
{
  client.println("<h1><center>Garage door 1 is open</h1></center>
");// action B
}

if (val2 < 100)
{
  client.println("<h1><center>Garage door 2 is closed</h1></center>
");
  }
else if (val2 >= 100)
{
  client.println("<h1><center>Garage door 2 is open</h1></center>
");// action B
}
    delay(50);
    client.stop();
  }
}

How can I get this to auto refresh?

Use the appropriate META tags.

        if(index<BUFSIZ)  //Only add data if the buffer isn't full.
        {
          clientline[index]=c;
          index++;
        }
        if (c == '\n' && currentLineIsBlank)

Consistentuseofwhitespaceisgood.

          client.println("<h1><center>Garage Control</h1></center>
<center><form method=get action=/?><input type=radio name=L1>Door 1
<input type=radio name=L2>Door 2
<input type=submit value=submit></form></center>");

You ARE allowed to send this using more than one command, you know…

int val1 = analogRead(sensor1);
int val2 = analogRead(sensor2);
     
        if (val1 < 100)
{
  client.println("<h1><center>Garage door 1 is closed</h1></center>
");
  }
else  if (val1 >= 100)
{
  client.println("<h1><center>Garage door 1 is open</h1></center>
");// action B
}

if (val2 < 100)
{
  client.println("<h1><center>Garage door 2 is closed</h1></center>
");
  }
else if (val2 >= 100)
{
  client.println("<h1><center>Garage door 2 is open</h1></center>
");// action B
}

Two action B’s?

What kind of analog sensors are you using to detect open/closed? My garage door sensors are digital. It’s either open or closed.

Thanks for the direction. I ended up scraping that code and modifying another one. This one works much better. There are no sensors yet, but I imagined using some type of magnetic switch or micro-switch, which would be digital! So you are right. I can just change the pins for that.

Here is the final web server code which will run on the arduino with an ethernet shield. Has two sensor inputs A0 and A1 and two outputs, pins 2 and 3. Web buttons control the digital pins. I got it scaled correct for a android and iphone. No webserver needed, just the arduino and router. Has no auto refresh, but I think I may add it and ditch the refresh button.

Anyway, posted for someone else to use as a base for some home automation.

#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
  byte ip[] = { 192, 168, 1, 30 };   //Manual setup only
  byte gateway[] = { 192, 168, 1, 1 }; //Manual setup only
  byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only

  // if need to change the MAC address (Very Rare)
  byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xC2, 0x52 };

  EthernetServer server = EthernetServer(80); //port 80
////////////////////////////////////////////////////////////////////////

void setup(){
  Serial.begin(9600);

  //Pins 10,11,12 & 13 are used by the ethernet shield

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

  Ethernet.begin(mac, ip, gateway, subnet); //for manual setup

  server.begin();
  Serial.println(Ethernet.localIP());

}

void loop(){

  // listen for incoming clients, and process qequest.
  checkForClient();

}

void checkForClient(){

  EthernetClient client = server.available();

  if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {

        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<meta name='viewport' content='width = 320' />");
          client.println("<meta name='viewport' content='initial-scale=1, user-scalable=no' />");
          client.println("<h2><center>Garage Door Control</h2></center>");
          client.println("<center><input type=button onClick=location.href='http://192.168.1.30' value='Update Status'></center>");
          client.print("
");
int door1status = analogRead(A0);
int door2status = analogRead(A1);
          if (door1status < 10){client.println("<center>Kristen's door is closed</center>");}
          if (door1status >= 10){client.println("<center>Kristen's door is <b>OPEN!</b></center>");}
          client.println("<center><input type=button onClick=location.href='http://192.168.1.30/?2' value='Activate Kristen&#39;s Door'></center>
");
          if (door2status < 10){client.println("<center>Mike's door is closed</center>");}
          if (door2status >= 10){client.println("<center>Mike's door is <b>OPEN!</b></center>");}
          client.println("<center><input type=button onClick=location.href='http://192.168.1.30/?3' value='Activate Mike&#39;s Door'></center>
");
          client.println("<center><input type=button onClick=location.href='http://192.168.1.30/?23' value='Activate Both Doors'></center>
");
          sentHeader = true;
        }

        char c = client.read();

        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info

        if(reading){
          Serial.print(c);

           switch (c) {
            case '2':
              //add code here to trigger on 2
              triggerPin(2, client);
              break;
            case '3':
            //add code here to trigger on 3
              triggerPin(3, client);
              break;
          }

        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
          break;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

  } 

}

void triggerPin(int pin, EthernetClient client){
//blink a pin - Client needed just for HTML output purposes.  
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
}
void triggerPin(int pin, EthernetClient client

Why pass the client reference to this function?
You don’t use it.

if (door1status < 10){client.println("<center>Kristen's door is closed</center>");}
          if (door1status >= 10)

If something isn’t less than ten, it must be greater than or equal to 10; a simple else would suffice here.

If you want your web page to auto refresh, just put it into your html code:

<head><meta http-equiv=\"refresh\" content=\"5\"></head> like PaulS said before

This will make your web page auto refresh every 5 secs

AWOL:

void triggerPin(int pin, EthernetClient client

Why pass the client reference to this function?
You don’t use it.

I don’t know, I copied the code from a sample online and really have no clue what I am doing! :slight_smile: This is the first time I am coding ANYTHING! I don’t even know what that does so I left it. LOL!

AWOL:

if (door1status < 10){client.println("<center>Kristen's door is closed</center>");}

if (door1status >= 10)



If something isn't less than ten, it must be greater than or equal to 10; a simple else would suffice here.

Good point!

Thanks!