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() {
          "<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> \

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");
    digitalWrite(switchPin2, HIGH);
    Serial.print("\nUnit is on:\n");
    int num = server.arg("sw1").toInt();
    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");
    digitalWrite(switchPin2, LOW);
    Serial.print("\nUnit is off:\n");
    int num = server.arg("sw2").toInt();
    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() {
  pinMode(switchPin2, OUTPUT);
  digitalWrite(switchPin2, LOW);
  WiFi.begin(ssid, password);  //Connect to the WiFi network
  while (WiFi.status() != WL_CONNECTED) { // Wait for connection
    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.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:
Server listening
Switch 1 entered:   8000

Unit is on:
Switch 2 entered:   8000

Unit is off:
  • are you able to toggle the relay with simple code?
    – jsotola
    Jan 18, 2021 at 19:52
  • Could you add the Serial output to your question? I do not really unerstand what you are doing (why "POST" request?), so maybe there is something wrong with he communication.
    – ridgy
    Jan 18, 2021 at 20:57
  • @jstola do you mean like in the sample code where it turns on for a second and then turns off? Yes. That's the code I started with. I'm just splitting that function in half.
    – brad
    Jan 18, 2021 at 21:33
  • 1
    Seems like you pressed three times Switch 1 and then once Switch 2... Btw., there is a typo in your HTTML; missing ' in <form name='frm1 method='post'> after frm1 - is this also true in your original code?
    – ridgy
    Jan 19, 2021 at 9:42
  • 1
    @ridgy That was it! Now it works. A missing quote. Yeesh
    – brad
    Jan 20, 2021 at 1:38


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.