BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Change button status using virtual pin

hello friends,
First I want to say that I am Brazilian and sorry for any spelling mistakes.
I’m a beginner in this world of programming, and I’m doing a project for my house, it’s simple, just turn on lamps, but I would like a virtual button on the blynk that would turn off all the relays, I already got one, but it doesn’t update the state of the buttons in blynk these buttons are not virtual, they are gpio pins.
can I do this?
I’m using a esp8266 nodemcu
the code I tested I got it right here, but in short, it disarms the relays but in the blynk it remains as if it were on, then I need to press it twice to get back. Sorry if there is already something in this sense, but I don’t know what terms to use to search

You should use virtual pins for all of your widgets rather than mixing virtual and digital pins.

Pete.

hello, thanks, is there any code i can use? I’ve been seeing some virtual pin codes but nothing that fits my

… needs?

And what are those needs? Your title mentions a three way switch, then nothing about it in your post but referencing relays, lamps and wanting feedback from something else… perhaps external control of same relays??

Much more details please.

Sorry, this is my first topic here …
the system will work by cell phone with blynk + 3 way buttons, but this is not the problem
I would like a button on the blynk to turn off all relays and change the status of the buttons on the blynk itself

Not a rehash of what you already said :stuck_out_tongue:

Using code and Virtual Pins? Or simply Widgets directly controlling a GPIO

Three way switch/button?.. as in a Virtual Segmented Switch in Blynk, three separate Virtual (or direct GPIO) Switches/Buttons in Blynk, or as in external house wiring related… three separate switches (in different locations) control one relay/lamp/etc.?

How many relays, hooked up to what GPIO? And what do those relays do exactly?

What needs to feedback to Blynk… the action of an external switch? If so, connected to what and how?

Details please :slight_smile: Assume we don’t know what you are doing, nor how… let alone what you want to do.

I’m using nodemcu esp32 and a board with 4 relays for 4 lamps, and 3-way switches to manually turn off, but that’s not the problem …
what i need is to know how to make a code to use virtual pins to turn these relays on and off and in that code a button also virtual, to turn off all relays, BUT that this virtual button can change the state of the other virtual buttons.
for example: there are 3 buttons on the blynk, then 3 lights on, and when I leave the house, I press this virtual button on the blynk and it turns off the lights on and the other buttons on the blynk of these lights show that they are off.
gpio can be any one, I have a code that starts in low state
I do not need interaction or feedbak of physical buttons …


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xx";
char ssid[] = "xx";
char pass[] = "xx";

void setup()
{
Serial.begin(9600);

pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
// turn off all relays startup
digitalWrite(0, HIGH);
digitalWrite(2, HIGH);

Blynk.begin(auth, ssid, pass);
}
//virtual button turn off relays
BLYNK_WRITE(V0)
{
int i=param.asInt();
if (i==1)
{

delay(200); 
digitalWrite(0, HIGH);
delay(200);
digitalWrite(2, HIGH);
}

}

//virtual button 1
BLYNK_WRITE(V2)
{
int i=param.asInt();
if (i==1)
{
delay(200);
digitalWrite(0, LOW);
}
else
{
delay(200);  
digitalWrite(0, HIGH);
}
}
//virtual button 2
BLYNK_WRITE(V1)
{
int i=param.asInt();
if (i==1)
{
delay(200);
digitalWrite(2, LOW);
}
else
{
delay(200);  
digitalWrite(2, HIGH);
}
}


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

That is what we needed to know… Just use a Blynk.virtualWrite(vPin, value) command to adjust the value/state of another Virtual Pin.

For example this code snippet of two Blynk functions, each linked to a Virtual Button set to switch mode, and each function controls the state of a GPIO (2 & 3 in this example). This will allow only one switch ON at a time, by turning OFF the other switch in case it is already ON.

BLYNK_WRITE(V0) {
  int value = param.asInt(); // Get value as integer
  if (value == 1) {
    Blynk.virtualWrite(V1, 0)  // Turn other switch on V1 OFF
    // Then do something to turn ON something
    digitalWrite(2, 1);  // Enable digital pin 2
  } else {
    // Do something to turn OFF something
    digitalWrite(2, 0); // Disable digital pin 2
  }
}


BLYNK_WRITE(V1) {
  int value = param.asInt(); // Get value as integer
  if (value == 1) {
    Blynk.virtualWrite(V0, 0)  // Turn other switch on V0 OFF
    // Then do something to turn ON something
    digitalWrite(3, 1);  // Enable digital pin 3
  } else {
    // Do something to turn OFF something
    digitalWrite(3, 0); // Disable digital pin 3
  }
}

HOWEVER!! for this to properly work for your three (or four) buttons… each MUST be a Virtual Pin connection, not a direct GPIO link, as all that stuff happens in a coded Blynk function, as shown above.

exactly!!

thank you friend, that was it …
but it came to my mind that I’m going to use 3-way physical buttons, and with that nothing is worth changing the state of the buttons. but it was worth it because i learned to implement virtual led, since i will have to use stylized buttons that do not show their status


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xx";
char ssid[] = "xx";
char pass[] = "xx";

void setup()
{
Serial.begin(9600);

pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
// turn off all relays startup
digitalWrite(0, HIGH);
digitalWrite(2, HIGH);

Blynk.begin(auth, ssid, pass);
}
//virtual button turn off relays
BLYNK_WRITE(V0)
{
int i=param.asInt();
if (i==1)
{

delay(200); 
digitalWrite(0, HIGH);
Blynk.virtualWrite(V1, LOW);// turn state button blynk
Blynk.virtualWrite(V8, 1023); //turn off led blynk
delay(200);
digitalWrite(2, HIGH);
Blynk.virtualWrite(V2, LOW);// turn state button blynk
Blynk.virtualWrite(V9, 1023); //turn off led blynk
}

}

//virtual button 1
BLYNK_WRITE(V2)
{
int i=param.asInt();
if (i==1)
{
delay(200);
digitalWrite(0, LOW);
Blynk.virtualWrite(V9, 1023); 
}
else
{
delay(200);  
digitalWrite(0, HIGH);
Blynk.virtualWrite(V9, 0);
}
}
//virtual button 2
BLYNK_WRITE(V1)
{
int i=param.asInt();
if (i==1)
{
delay(200);
digitalWrite(2, LOW);
Blynk.virtualWrite(V9, 1023); 
}
else
{
delay(200);  
digitalWrite(2, HIGH);
Blynk.virtualWrite(V9, 0);
}
}


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