my code is for a watering system and executes its primary function just fine. i can surf to the URL, click on a relay and select it to add water.
my only issue is accounting for user error. if user does an accidental refresh, a second unwanted watering event could occur, which is bad.
The issue is that following selection, the user’s URL get’s updated to the relay call
http://192.168.1.12/SWITCH=2
a simple redirect following the call would fix the issue, landing it on
http://192.168.1.12
instead. then an accidental refresh would do no harm.
I tried
client.println("Location: /");
in many places in my code but no redirect ever happened. I could have been doing it wrong.
example code:
if (request.indexOf("/SWITCH=0") != -1){
digitalWrite(switchPin0, LOW);
digitalWrite(statusLed, LOW); // blink led
delay(1);
digitalWrite(statusLed, HIGH);
//set water pump time here
delay(8000);
digitalWrite(switchPin0, HIGH);
value = HIGH;
++switch0;
client.println("Location: /"); //where i think it should go, but fails
} else if (request.indexOf("/SWITCH=1") != -1){
digitalWrite(switchPin1, LOW);
digitalWrite(statusLed, LOW); // blink led
delay(1);
digitalWrite(statusLed, HIGH);
//set water pump time here
delay(8000);
digitalWrite(switchPin1, HIGH);
value = HIGH;
++switch1;
client.println("Location: /");
}
actual code:
//note, when you write to low, you turn the switchport on. It's backwards for some reason
#include <ESP8266WiFi.h>
#define statusLed 2 // built-in led for the ESP8266-12
const char* ssid = "myssid";
const char* password = "mypass";
int switchPin0 = 16;
int switchPin1 = 5;
int switch0;
int switch1;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(statusLed, OUTPUT);
pinMode(switchPin0, OUTPUT);
pinMode(switchPin1, OUTPUT);
digitalWrite(switchPin0, HIGH);
digitalWrite(switchPin1, HIGH);
// 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(".");
}
digitalWrite(statusLed, HIGH);
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
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();
// Match the request
int value = HIGH;
if (request.indexOf("/SWITCH=0") != -1){
digitalWrite(switchPin0, LOW);
digitalWrite(statusLed, LOW); // blink led
delay(1);
digitalWrite(statusLed, HIGH);
//set water pump time here
delay(8000);
digitalWrite(switchPin0, HIGH);
value = HIGH;
++switch0;
} else if (request.indexOf("/SWITCH=1") != -1){
digitalWrite(switchPin1, LOW);
digitalWrite(statusLed, LOW); // blink led
delay(1);
digitalWrite(statusLed, HIGH);
//set water pump time here
delay(8000);
digitalWrite(switchPin1, HIGH);
value = HIGH;
++switch1;
} else {
//reserved
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Switch pin is now: ");
client.println("
");
client.println("Switch 0");
client.println("Click <a href=\"/SWITCH=0\">here</a> to send a measured amount
");
client.println("Number of times plant 0 has been fed is ");
client.println(switch0);
client.println("
");
client.println("Switch 1");
client.println("Click <a href=\"/SWITCH=1\">here</a> to send a measured amount
");
client.println("Number of times plant 1 has been fed is ");
client.println(switch1);
client.println("
");
client.println("
");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
digitalWrite(statusLed, LOW); // blink led
delay(1);
digitalWrite(statusLed, HIGH);
}
How do i redirect my landing page post selection? or a better solution?
List of things that do not work:
writing location after the content does not work
client.println("Content-Type: text/html");
client.println("Location: /");
Throwing the RFC at me, does not work.