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

I want to control 1 relay or digital pin from wemos

Hello I want to control 1 relay or digital pin from wemos d1 board with 3 virtual buttons blynk ,but I have some problem MI ideea is tu turn on relay when all virtual buttons they are on or high and if one or two virtual button is off I want the relay to keep on and to turn relay off all virtual buttons to switch off

***********************************************************

   Blynk Bridge - Communication between ESP8266
   Sketch code for the receiver module (module which is controlled)
   www.geekstips.com
 **************************************************************/

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth [] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid [] = "";
char pass [] = "";
const int rel = 5;    // pin NODE D0 tenda salotto abbassa



void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  digitalWrite(rel, HIGH);
  pinMode(rel, OUTPUT);    // imposta pin in modalità uscita

}
// This code will update the virtual port 5
BLYNK_WRITE(V53) {
  int pinData = param.asInt();
  if (pinData == HIGH) { //if (String("1000") == param.asStr()){
    digitalWrite(rel, LOW);
  } else {
    digitalWrite(rel, HIGH);



  }
}

BLYNK_WRITE(V52) {
  int pinData = param.asInt();
  if (pinData == HIGH) { //if (String("1000") == param.asStr()){
    digitalWrite(rel, LOW);
  } else {
    digitalWrite(rel, HIGH);



  }
}

BLYNK_WRITE(V51) {
  int pinData = param.asInt();
  if (pinData == HIGH) { //if (String("1000") == param.asStr()){
    digitalWrite(rel, LOW);
  } else {
    digitalWrite(rel, HIGH);



  }
}

void loop() {

  Blynk.run();

First off, you are better to open your own topic and not re-open one that is over 2 years old.

Second, you need to format your code correctly when posting it.

For your question, declare the variables for the virtual buttons as global and just use some If, ELSE IF statements to check conditions and act appropriately when a virtual button is pressed.

@sony8943 I moved this into a new topic and formatted your posted code… this time… please make note of how to do such for future.

Thank you Gunner , I try to open new topic but I cannot because I use smart phon browser and I think I have problem with android browser

I use Chrome…

OK
I have to put some extra in ?code for blynk write 51 52 53

Read what @Toro_Blanco wrote.
You need to use global variables (ones that are declared at the top of your code, not within a function such as BLYNK_WRITE) so that the results can be used throughout your code.

You then need to create a function, which is called by each of the BLYNK_WRITE functions, which uses if/else statements to check if all of your global variables are set to on or off.

Pete.


BLYNK_WRITE(V53) {
  int pinData = param.asInt();
  if (pinData == HIGH) { //if (String("1000") == param.asStr()){
    digitalWrite(rel, LOW);
    Blynk.virtualWrite(V51, LOW);
    Blynk.virtualWrite(V52, LOW);
  } else {
    digitalWrite(rel, HIGH);
    Blynk.virtualWrite(V51, HIGH);
    Blynk.virtualWrite(V52, HIGH);




  }
}

BLYNK_WRITE(V52) {
  int pinData = param.asInt();
  if (pinData == HIGH) { //if (String("1000") == param.asStr()){
    digitalWrite(rel, LOW);
    Blynk.virtualWrite(V51, LOW);
    Blynk.virtualWrite(V53, LOW);
  } else {
    digitalWrite(rel, HIGH);
    Blynk.virtualWrite(V51, HIGH);
    Blynk.virtualWrite(V53, HIGH);



  }
}

BLYNK_WRITE(V51) {
  int pinData = param.asInt();
  if (pinData == HIGH) { //if (String("1000") == param.asStr()){
    digitalWrite(rel, LOW);
    Blynk.virtualWrite(V52, LOW);
    Blynk.virtualWrite(V53, LOW);
  } else {
    digitalWrite(rel, HIGH);
    Blynk.virtualWrite(V52, HIGH);
    Blynk.virtualWrite(V53, HIGH);



  }
}

its not working when i turn off 2 virtual buttons the digital or relay is blinking and i dont understand about global variables (ones that are declared at the top of your code, not within a function such as BLYNK_WRITE) so that the results can be used throughout your code.

You then need to create a function, which is called by each of the BLYNK_WRITE functions, which uses if/else statements to check if all of your global variables are set to on or off.

Well, this forum isn’t the place to learn about global variables, how to write functions, and how to call those functions from within your code.
I’d suggest that you do some research and find a tutorial about C++/Arduino coding in a format that suits your learning style and that is in a language you find easy to follow.

Once you’ve done that, if you have any Blynk specific questions then post them here.

On a side-note, the concept of requiring three widget buttons to all be on before turning on your relay, and needing all three to be off before deactivating your relay, seems a rather strange approach to user interface design.
Whilst learning how to achhieve this is useful coding experience, I think you may then decide that there is a simpler and more useable way to achieve the desired result. It might be worth re-visiting your UI design before getting too bogged-down in the details of how to achieve the result you’re currently working towards.

Pete.

OK thank you but they are is similar topic like mi own ?

Hello again, finally i find and research how to control 1 relay with 4 virtual button, pin etc ;
the code is working fine; please correct me if i have some errors on blynk server.
thank you.


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial
char auth[] = "============";
char ssid[] = "========";
char pass[] = "(=======";
const int rel = 14;
int motorInitiated = 0;
int motorInitiated1 = 0;
int motorInitiated2 = 0;
int motorInitiated3 = 0;
SimpleTimer timer;

BLYNK_WRITE(V10) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated++;// Sets motor Initiated to 1
  } else {
    motorInitiated = 0;// Sets motor Initiated to 1
  }
}



BLYNK_WRITE(V71) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated1++;// Sets motor Initiated to 1
  } else {
    motorInitiated1 = 0;
  }
}

BLYNK_WRITE(V11) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated2++;// Sets motor Initiated to 1
  } else {
    motorInitiated2 = 0;// Sets motor Initiated to 1
  }
}

BLYNK_WRITE(V12) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated3++;// Sets motor Initiated to 1
  } else {
    motorInitiated3 = 0;// Sets motor Initiated to 1
  }
}

void sendSensor() {

  if (motorInitiated == 0 && motorInitiated1 == 0 && motorInitiated2 == 0 && motorInitiated3 == 0)
  {

    digitalWrite(rel, LOW);
  } else {
    digitalWrite(rel, HIGH);
  }




}


void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000, sendSensor);
  digitalWrite(rel, LOW);
  pinMode(rel, OUTPUT);

}

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


1 Like

Looks good.

The only thing I may do different is change the order of some things in setup()

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(rel, OUTPUT);
  digitalWrite(rel, LOW);
  timer.setInterval(1000, sendSensor);
}

I generally declare a pin as an input or output before performing any digitalWrite to it.

1 Like

thankyou