I am trying to write code that will capture data via http post and flip a relay switch. It's for an air conditioner. Just a simple on and off button from 2 separate buttons.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
const char* ssid = "getAjobFool";
const char* password = "password";
//switchpin assignement variables.
//add additional switch/relay here if adding new plug
int switchPin2 = 4;
//counter variable
//add additional switch/relay here if adding new plug
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);
int num = server.arg("sw1").toInt();
delay(num);
//digitalWrite(switchPin2, HIGH);
++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);
int num = server.arg("sw2").toInt();
delay(num);
//digitalWrite(switchPin2, HIGH);
++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);
//add additional switch/relay here if adding new plug
pinMode(switchPin2, OUTPUT);
//add additional switch/relay here if adding new plug
digitalWrite(switchPin2, HIGH);
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 can get a response out of my
name='sw2'
from
if(server.hasArg("sw1") && (server.arg("sw1").length()>0)){
Serial.print("Switch 1 entered:\t");
Serial.println(server.arg("sw1"));
digitalWrite(switchPin2, HIGH);
int num = server.arg("sw1").toInt();
delay(num);
//digitalWrite(switchPin2, HIGH);
++switch2;
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
(second) submission to switch on, but I cannot get it's inverse function to work on
name='sw1'
How do I get my relay switch off?