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