Change state of graphical buttons on bynk by another button command

I am using blynk server with nodemcu. This project is about to control 4 loads from blynk app. I have used 4 switch buttons for 4 loads to turn on and off. Also I am using push button switch for making all loads off at a time. I want make other switch button states to off when I press all loads off push button. How to achieve it ?

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "xxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxx";
char pass[] = "xxx";

void setup()
{
  pinMode(D0,OUTPUT);pinMode(D1,OUTPUT);pinMode(D2,OUTPUT);pinMode(D3,OUTPUT);
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}

BLYNK_WRITE(V0) // switch which makes all loads off by single push button
{
    if (param.asInt()) 
    {      
        digitalWrite(D0, LOW);
        digitalWrite(D1, LOW);
        digitalWrite(D2, LOW);
        digitalWrite(D3, LOW);
    }
        
}
BLYNK_WRITE(V1)// switch control for load 1
{
    if (param.asInt())       
        digitalWrite(D0, HIGH);
    else 
        digitalWrite(D0, LOW);
    
}

BLYNK_WRITE(V2)// switch control for load 2
{
    if (param.asInt())       
        digitalWrite(D1, HIGH);
    else 
        digitalWrite(D1, LOW);
    
}

BLYNK_WRITE(V3)// switch control for load 3
{
    if (param.asInt())       
        digitalWrite(D2, HIGH);
    else 
        digitalWrite(D2, LOW);
    
}

BLYNK_WRITE(V4)// switch control for load 4
{
    if (param.asInt())       
        digitalWrite(D3, HIGH);
    else 
        digitalWrite(D3, LOW);
    
}

void loop()
{
  Blynk.run();
}

Try changing your BLYNK_WRITE(V0) code sothat you’re doing Blynk.virtualWrites to V1,V2,V3 and V4 instead of the existing DigitalWrites.

Pete.

Thanks for the response. I am new to this field. Could you please elaborate your response. I couldn’t get it… @PeteKnight

What you’re doing here is setting the digital pins on the NodeMCU LOW by writing to them directly using a digitalWrite command:

BLYNK_WRITE(V0) // switch which makes all loads off by single push button
{
    if (param.asInt()) 
    {      
        digitalWrite(D0, LOW);
        digitalWrite(D1, LOW);
        digitalWrite(D2, LOW);
        digitalWrite(D3, LOW);
    }    
}

Instead, you should be setting the switch widgets in the app to Off, using the Blynk.virtualWrite command, like this:

{      
    Blynk.virtualWrite(V1, 0);
    Blynk.virtualWrite(V2, 0);
    Blynk.virtualWrite(V3, 0);
    Blynk.virtualWrite(V4, 0);
} 

However, your initial code has some problems…
The switch widget attached to V0 will (by default) send a “0” when it’s turned off, and a “1” when it’s turned on.

The code:

    if (param.asInt()) 

is effectively saying “if the value that comes back from the switch widget on V0 is true, then do this…”
In C++ terms, true equals 1 and false equals zero, so your code would perform these digital writes (or virtual writes if you changed the code) when the switch on V0 is turned On.

Another way of writing this line of code is:

    if (param.asInt()==1) 

I suspect also that your relay is probably an active LOW device, in which case setting the pin to LOW will actuate the relay rather than de-actuate it.

The final issue is the pins that you’ve chosen to use, and how you’re referring to them in your code. You’re using the “D” numbers that are screen-printed on to the NodeMCU, rather than referencing the GPIO numbers of the pins. This help with the wiring, but makes it more difficult to use the same code on other devices, and also makes it more difficult o work-out which pins you shouldn’t be using.
If you look at this diagram…

you’ll see that you’ve chosen to use GPIO16. 5, 4 and 0.

If you look at this table…

you’ll see that GPIO0 needs to be HIGH at boot. connecting a relay to it could prevent the NodeMCU from booting, so you should avoid that pin.

Pete.

You need to create a void sync with blynk.syncvirtual (V1,V2, Vx)

Thanks for the response. I appreciate it. I am getting error “exit status 1
‘blynk’ was not declared in this scope” when I use “Blynk.virtualWrite(V1, 0);”.

Thanks for the response. I am new to this field. I request you to please modify in my code if u can. It would help me a lot.

You need to post your latest code (correctly formatted) and full details of which part of your code is highlighted when you get the compilation error.

Pete.

(unformatted code removed)

The B in Blynk is supposed to be uppercase :wink:

And you didn’t properly format your code as required in the Welcome Topic and requested again by @PeteKnight …

(Yes, I tire of repeating this :stuck_out_tongue_winking_eye: Why people no read? :thinking: )

1 Like

thanks for the response, I have made it finally.