I'm playing with modifying the basic web server example to control some relays. The problem I'm running into seems to be a browser cache issue. I've found multiple solutions, but none of them seem to function for me. When a form button is clicked, it writes to this window.location. The arduino takes action based on that value. Unfortunately, the refresh of the web page results in reloading that cached value and it performs the operation again. If I close the browser window and reopen it, the window.location value goes away, but once I click a button again the value persists with each refresh. Does someone have a solution for this problem? Basically I need a way to clear the window.location value or variable when the page refreshes.
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 10, 45, 4);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int buttonpin = 9;
String readString; // We will be using strings to keep track of things.
String emptyString = " ";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(buttonpin, OUTPUT);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (readString.length() < 120) {
readString += c;
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<hmtl>");
client.println("window.location.href = window.location.href.replace('');");
client.println("</head>");
client.println("<title>");
client.println("ARDUINO + ETHERNET Page");
client.println("</title>");
client.println("<body bgcolor=black>");
client.println("<font color=white>");
client.println("<center>");
client.println("<p>");
client.println("<table border=0 width=200>");
client.println("<tr>");
client.println("<td align=center>");
client.println("<font color = turquoise size=10>");
client.println("00 <font color = turquoise size=6>deg F");
client.println("</td>");
client.println("</tr>");
client.println("</table>");
client.println("<p>");
client.println("<FORM>");
client.println("<INPUT type=button value=CYCLE SWITCH style=font-size:30px onClick=window.location='/?onecycle\'><p>");
client.println("<INPUT type=button value=OPEN DOOR style=font-size:30px onClick=window.location='/?open\'><p>");
client.println("<INPUT type=button value=CLOSE DOOR style=font-size:30px onClick=window.location='/?close\'><p>");
client.println("<INPUT type=button value=OPEN FOR CAT style=font-size:30px onClick=window.location='/?cat\'>");
client.println("</FORM>");
client.println("<table border=1 width=200>");
client.println("<tr>");
client.println("<td align=center>");
client.println("<font color=white size=3>");
client.println("The Door Is");
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td align=center>");
client.println("<font color=white size=7>");
client.println("OPEN");
client.println("</td>");
client.println("</tr>");
client.println("</table>");
client.println("</center>");
client.println("</font>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
Serial.println(readString);
if(readString.indexOf("?onecycle") >0) // these are the snippets the Arduino is watching for.
{
PushTheButton();
}
if(readString.indexOf("?open") >0) // these are the snippets the Arduino is watching for.
{
PushTheButton();
delay(200);
PushTheButton();
}
if(readString.indexOf("?close") >0) // these are the snippets the Arduino is watching for.
{
PushTheButton();
delay(2000);
PushTheButton();
}
readString = emptyString;
}
}
void PushTheButton()
{
digitalWrite(buttonpin, HIGH);
delay(200);
digitalWrite(buttonpin, LOW);
}