multiple if commands

Hi!

I am having problem with programming chinese smart plug. I want to make simple toggle switch that turns on relay, if it is off and vice versa. I have succeed making on and off switches, so I think the problem must be with multiple "if" commands.

Here is the problematic part of my code:

if (request.indexOf("/RelayTOGGLE") != -1){  //if command "RelayToggle" is received
    if(value == 0){                                              // value represents state of the relay off=0
    digitalWrite(RelayOFF, HIGH);                //turns on relay
    delay(10);
    digitalWrite(RelayON,LOW );
    value = 1;
    }
    if(value == 1){
    digitalWrite(RelayON, HIGH);        //turns off relay
    delay(10);
    digitalWrite(RelayOFF, LOW);
    value = 0;
    }
    }

With this code RelayToggle only turns off the relay but doesnt turn it on. Any help is appreciated!

Here is the problematic part of my code:

You need to post ALL of your code. We can not see, for instance, what type value is, or where else you may be messing with its value.

value is a lousy name. relayState would be far better.

You appear to be using the value to control the state of two different pins. Why? How does that relate to the name relayToggle?

#include <ESP8266WiFi.h>
 
const char* ssid = "possuwlan";
const char* password = "censored";
 
int RelayON = 12;
int RelayOFF = 5;
WiFiServer server(80);
 
void setup() {
  Serial.begin(9600);
  delay(10);
 
 
  pinMode(RelayON, OUTPUT);
  pinMode(RelayOFF, OUTPUT);
  digitalWrite(RelayON, HIGH);
  digitalWrite(RelayOFF, LOW);
 
  // 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(".");
  }
  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 = 0;
  if (request.indexOf("/RelayON") != -1) {   // this works fine, relay turns on when command RelayON is
    digitalWrite(RelayOFF, HIGH);                    sent.
    delay(10);
    digitalWrite(RelayON, LOW);
    value = 1;
  }
  if (request.indexOf("/RelayOFF") != -1){   //this works fine too, relay turns off when command RealyOF
    digitalWrite(RelayON, HIGH);                    is sent.
    delay(10);
    digitalWrite(RelayOFF, LOW);
    value = 0;
  }
  if (request.indexOf("/RelayTOGGLE") != -1){  //Here I am trying to toggle the relay.
    if(value == 0){
    digitalWrite(RelayOFF, HIGH);
    delay(10);
    digitalWrite(RelayON,LOW );
    value = 1;
    }
    if(value == 1){
    digitalWrite(RelayON, HIGH);
    delay(10);
    digitalWrite(RelayOFF, LOW);
    value = 0;
    }
    }
    
  // 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("Relay is now: ");
 
  if(value == 1 ) {
    client.print("On");  
  } else {
    client.print("Off");
  }
  client.println("

");
  client.println("Click <a href=\"/RelayON\">here</a> Turn relay ON
");
  client.println("Click <a href=\"/RelayOFF\">here</a> Turn relay OFF
");
  client.println("Click <a href=\"/RelayTOGGLE\">here</a> Toggle relay
");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
 
}

I agree value is a bad name for the relaystate, I will change it. I am controlling two different pins, because the smart plug that I am modifying does have RelayON and RelayOFF pins. By experiementing I have succeed to turn on the relay by applying low to RelayON pin and high to RelayOFF pin.

By experiementing I have succeed to turn on the relay by applying low to RelayON pin and high to RelayOFF pin.

What do you have to apply to the two pins to turn the relay off? If you don't know that, you can't blame the problem on the code not working.

  int value = 0;
  if (request.indexOf("/RelayON") != -1) {

Because value is not static, next time loop() is called, it will have a value of 0, so you are NOT using it to remember the state of the relay.

What do you have to apply to the two pins to turn the relay off? If you don't know that, you can't blame the problem on the code not working.

To turn off the relay I have to aplly high to RelayOn pin and low to RelayOff pin.

Because value is not static, next time loop() is called, it will have a value of 0, so you are NOT using it to remember the state of the relay.

Oh that must be the problem. How do I make it static?

How do I make it static?

Rub it with a piece of wool.

Or, just type static in front of the declaration.

:grinning: Just a beginner so didn't really know. Thanks I will try!

Hmmhm, I made the relaystate static and uploaded the code. However the toggle command still doesn't work properly. When the relay is on and toggle command is given relay turns off as it should. But when the relay is off and toggle commad is given, nothing happens :o .

#include <ESP8266WiFi.h>
 
const char* ssid = "possuwlan";
const char* password = "censored";
 
int RelayON = 12;
int RelayOFF = 5;
WiFiServer server(80);
 
void setup() {
  Serial.begin(9600);
  delay(10);
 
 
  pinMode(RelayON, OUTPUT);
  pinMode(RelayOFF, OUTPUT);
  digitalWrite(RelayON, HIGH);
  digitalWrite(RelayOFF, LOW);
 
  // 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(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  digitalWrite(4, HIGH);
 
  // 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
 
  static int relaystate = 0;
  if (request.indexOf("/RelayON") != -1) {
    digitalWrite(RelayOFF, HIGH);
    delay(10);
    digitalWrite(RelayON, LOW);
    relaystate = 1;
  }
  if (request.indexOf("/RelayOFF") != -1){
    digitalWrite(RelayON, HIGH);
    delay(10);
    digitalWrite(RelayOFF, LOW);
    relaystate = 0;
  }
  if (request.indexOf("/RelayTOGGLE") != -1){
    if(relaystate == 0){
    digitalWrite(RelayOFF, HIGH);
    delay(10);
    digitalWrite(RelayON,LOW );
    relaystate = 1;
    }
    if(relaystate == 1){
    digitalWrite(RelayON, HIGH);
    delay(10);
    digitalWrite(RelayOFF, LOW);
    relaystate = 0;
    }
    }
    
  // 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("Relay is now: ");
 
  if(relaystate == 1 ) {
    client.print("On");  
  } else {
    client.print("Off");
  }
  client.println("

");
  client.println("Click <a href=\"/RelayON\">here</a> Turn relay ON
");
  client.println("Click <a href=\"/RelayOFF\">here</a> Turn relay OFF
");
  client.println("Click <a href=\"/RelayTOGGLE\">here</a> Toggle relay
");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
 
}

But when the relay is off and toggle commad is given, nothing happens

At the very least, a Serial.print() should happen. If that doesn't, then the logic is wrong. If it does, then the actions to be performed in the block are wrong. Which problem are we dealing with?

I was a bit inaccurate when I said "nothing happened". Serialprint happens following is printed:"GET /RelayTOGGLE HTTP/1.1
" ,when RelayTOGGLE command is sent. However the relay does't turn on.

However the relay does't turn on.

You have:

  if (request.indexOf("/RelayTOGGLE") != -1){
    if(relaystate == 0){
    digitalWrite(RelayOFF, HIGH);
    delay(10);
    digitalWrite(RelayON,LOW );
    relaystate = 1;
    }
    if(relaystate == 1){
    digitalWrite(RelayON, HIGH);
    delay(10);
    digitalWrite(RelayOFF, LOW);
    relaystate = 0;
    }
    }

When this poorly indented (use Tools + Auto Format to fix that) code is executed, you suspect that the first if statement will evaluate to true. Does it really? Add another Serial.print() statement after the {.

   Serial.println("Kittens are cute");

Then, if you see "Kittens are cute" being printed, you know that the if statement evaluated to true.

What you don't know then is what is in relaystate (really should be relayState). There are a couple of ways to find out. Figure out one of them, and learn whether relaystate contains what you think it does, or not.

The technique is called divide and conquer, and will allow you to isolate a problem (not just this one) very quickly.

if (request.indexOf("/RelayTOGGLE") != -1) {
     Serial.println("Kittens are cute");
    if (relaystate == 0) {
      digitalWrite(RelayOFF, HIGH);
      delay(10);
      digitalWrite(RelayON, LOW );
      relaystate = 1;
    }
    if (relaystate == 1) {
      digitalWrite(RelayON, HIGH);
      delay(10);
      digitalWrite(RelayOFF, LOW);
      relaystate = 0;
    }
  }

I added Serial.println("Kittens are cute");, but "kittens are cute" isn't printed when RelayToggle command is sent, so something wrong with that? Thanks for the tips!

I added Serial.println("Kittens are cute");, but "kittens are cute" isn't printed when RelayToggle command is sent, so something wrong with that?

I can't confirm your issue

When I run your code I see

new client
GET /RelayTOGGLE HTTP/1.1
Kittens are cute
Client disconnected

What does your serial print show for the GET statement given by Serial.println(request) from the three inputs.

To get the relaystate value toggle, you need to fix this section of code by changing an if to an else if.

if (request.indexOf("/RelayTOGGLE") != -1){
    Serial.println("Kittens are cute");
    Serial.println(relaystate);
    if(relaystate == 0){
    digitalWrite(RelayOFF, HIGH);
    delay(10);
    digitalWrite(RelayON,LOW );
    relaystate = 1;
    Serial.println(relaystate);
    }
    else if(relaystate == 1){ //need to change from if to else if
    digitalWrite(RelayON, HIGH);
    delay(10);
    digitalWrite(RelayOFF, LOW);
    relaystate = 0;
    Serial.println(relaystate);
    }
    }

To get the relaystate value toggle, you need to fix this section of code by changing an if to an else if.

Thank you very much!! It finally works! Such a nice community here, again thanks!