I am trying to toggle on/off between two separate HTTP request. One submit button to turn it on, and another separate submit button to turn it off.
I expect the device to launch in the off/low 0V position
digitalWrite(switchPin2, LOW);
The first submit button would turn the device on/HIGH/sw1, the second submit button would turn it off/LOW/sw2.
But what it is doing is at first nothing when the first/sw1 submit button is pressed but when I press the sw2 submit, I get serial output telling me that sw1 was just pressed. Then if I press sw2 again, it will tell me sw2 was just pressed but will not repeat the cycle until i press sw1 again. Then sw2 becomes functional, just performing the wrong function.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
const char* ssid = "ssid";
const char* password = "pass";
int switchPin2 = 4;
//counter variable
int switch2;
String inString;
void webpage() {
server.send(200,
"text/html",
"<html> \
<body> \
<h2>Air conditioner manager</h2> \n\
<p>Turn air conditioner on for "+String(switch2)+" ms</p> \
<form name='frm1 method='post'> \
<input type='text' name='sw1' value=8000> \
<input type='submit' value='Submit'> \
</form> \
<p>Turn air conditioner off for "+String(switch2)+" ms</p> \
<form name='frm2' method='post'> \
<input type='text' name='sw2' value=8000> \
<input type='submit' value='Submit'> \
</form> \
</body> \
</html>");
}
void response() {
//add additional switch/relay here if adding new plug
if (server.hasArg("sw1") && (server.arg("sw1").length() > 0)) {
Serial.print("Switch 1 entered:\t");
Serial.println(server.arg("sw1"));
digitalWrite(switchPin2, HIGH);
Serial.print("\nUnit is on:\n");
int num = server.arg("sw1").toInt();
++switch2;
server.sendHeader("Location", String("/"), true);
server.send (302, "text/plain", "");
} else if (server.hasArg("sw2") && (server.arg("sw2").length() > 0)) {
Serial.print("Switch 2 entered:\t");
Serial.println(server.arg("sw2"));
digitalWrite(switchPin2, LOW);
Serial.print("\nUnit is off:\n");
int num = server.arg("sw2").toInt();
++switch2;
server.sendHeader("Location", String("/"), true);
server.send (302, "text/plain", "");
} else {
server.send(400, "text/html", "<html><body><h1>HTTP Error 400</h1><p>Bad request. Please enter a value.</p></body></html>");
}
}
void setup() {
Serial.begin(115200);
pinMode(switchPin2, OUTPUT);
digitalWrite(switchPin2, LOW);
WiFi.begin(ssid, password); //Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
delay(500);
Serial.println("Waiting to connect...");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // Print the local IP
// server.on("/body", handleBody); // Associate the handler function to the path
server.on("/",HTTP_GET, webpage);
server.on("/",HTTP_POST,response);
server.begin(); // Start the server
Serial.println("Server listening");
}
void loop() {
server.handleClient(); //Handling of incoming requests
}
I get no error, only erratic output.
How do I toggle on/off between 2 submit buttons?
UPDATE: Serial output.
Waiting to connect...
IP address: 192.168.1.9
Server listening
Switch 1 entered: 8000
Unit is on:
Switch 2 entered: 8000
Unit is off:
'
in<form name='frm1 method='post'>
afterfrm1
- is this also true in your original code?