Problems getting high/low to function correction

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?

Are you intending that buttons toggle, that is alternate presses switch on and off ?

If not, I suppose I would try to have only one form but with 4 buttons, ON1, OFF1, ON2, OFF2 and something in java script which fills a hidden field with something to identify which button was pressed, then submits the form.

In your code to parse the response, you'd simply use digitalWrite() to switch the appropriate relay on or off, depending on which button was pressed

You're incorporating an extra on and off.

One submit button is for turning on, the other submit button is for turning off.

...later, I will be turning the AC on and off from another computer via post request. One request for on, another to turn it off.

6v6gt:
Are you intending that buttons toggle, that is alternate presses switch on and off ?

If not, I suppose I would try to have only one form but with 4 buttons, ON1, OFF1, ON2, OFF2 and something in java script which fills a hidden field with something to identify which button was pressed, then submits the form.

In your code to parse the response, you'd simply use digitalWrite() to switch the appropriate relay on or off, depending on which button was pressed

digitalWrite(switchPin2, HIGH);

doesn't give me a response.

I get no response from

Serial.print("Switch 2 entered:\t");

in my serial output

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.