Hi all, this should be a very simple fix. I am trying to have my webserver setup so that when I press the on button the led comes on and then turns off automatically after a delay. The issues is that the button press appends the LED=ON in the url and I can't seem to programatically delete it. The only way to remove it is with another button press. I need it deleted because I wan't the page to auto refresh every 5 seconds. This is because I also need to (eventually) read the status of an input pin and display that status on the webpage.
Ultimately this will become a wifi controlled gate opener. The gate system on my driveway just needs a pulse to open, so I can't have the output on all of the time, and I can't have it going on and off during a page refresh. My gate would just be constantly opening and then trying to close. My wife and kids would think it was demon possessed and then well that,s just not good..... As I mentioned the page refresh is necessary to show when the gate is closed.
My hardware is good and is all working.
Any help is greatly appreciated.
#include <ESP8266WiFi.h>
const char* ssid = "*********";
const char* password = "**********";
int ledPin = 4; // GPIO12 D6
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Set ledPin according to the request
//digitalWrite(ledPin, value);
int value = LOW;
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Refresh: 5");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("
");
client.println("<a href=\"/LED=ON\"><button>ON</button></a>");
client.println("<a href=\"/LED=OFF\"><button>OFF</button></a></p>");
client.println("</html>");
// Match the request
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
delay(1500);
digitalWrite(ledPin, LOW);
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
client.print("Led pin is now: ");
if (value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}