Turn on 4 Relays with One button using Blynk

I have used 8 Relay Module and nodeMCU to make a Home Automation Component but I want to start and stop 4 relays from one virtual button and do the same with Google assistant via IFTTT. Can anyone Help me getting a virtual pin code that could be suitable.

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

char auth[] = "(My token)"; // the auth code that you got on your gmail
char ssid[] = "Sudhir gupta"; // username or ssid of your WI-FI
char pass[] = "s1234567"; // password of your Wi-Fi


void setup()
{
// Debug console
Serial.begin(9600);
pinMode(D1,OUTPUT); //extend these to D8 if you are using a 8 pin relay
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D4,OUTPUT);
pinMode(D5,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);

digitalWrite(D1,HIGH); // Make it low if you want everything to go off
digitalWrite(D2,HIGH); // in case of a power cut
digitalWrite(D3,HIGH);
digitalWrite(D4,HIGH);
digitalWrite(D5,HIGH);
digitalWrite(D6,HIGH);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
Blynk.begin(auth, ssid, pass);

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

First of all, using the pins you’ve chosen may not be the best idea.
If you translate the “D” numbers of the NodeMCU into GPIO numbers the you get this:

D1 = GPIO5
D2 = GPIO4
D3 = GPIO0
D4 = GPIO2
D5 = GPIO14
D6 = GPIO12
D7 = GPIO13
D8 = GPIO15

As you’ll see from this:

using GPIO’s 0, 2 and 15 are potentially problematic, as having something connected to them at power-on could cause the NodeMCU to boot into the wrong mode, preventing the execution of your sketch.
As the NodeMCU doesn’t have enough suitable pins to do what you wnat then you may end0up using multiple devices (and Bridge code) or an ESP32.

To answer your original question, if you had a button widget (in switch mode) connected to pin V1 then the code would look like this:

BLYNK_WRITE(V1) // Triggered automatically when the value of V1 changes
{
 if(param.asInt) // if the value from the widget is 1
 {
    digitalWrite(D1,LOW);
    digitalWrite(D2,LOW);
    digitalWrite(D3,LOW);
    digitalWrite(D4,LOW);
 }
 else // if the value from the widget is 0
 {
    digitalWrite(D1,HIGH);
    digitalWrite(D2,HIGH);
    digitalWrite(D3,HIGH);
    digitalWrite(D4,HIGH);
 }
}

Obviously you’d use your newly selected pin numbers rather than the ones above.

Pete.

1 Like

Thanks a lot. I think the code you have given will work quite well and actually, I know how to manipulate those GPIO concept but thanks for telling. It will definitely help me in some way.

Is the code works perfectly?

This works partial correctly. He missed one bracket due to which I restudied the whole programming.

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

char auth[] = "my auth cude"; // the auth code that you got on your gmail
char ssid[] = "JioFi_100EB49"; // username or ssid of your WI-FI
char pass[] = "2444666668888888"; // password of your Wi-Fi


void setup()
{
// Debug console
Serial.begin(9600);
pinMode(D1,OUTPUT); //extend these to D8 if you are using a 8 pin relay
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D4,OUTPUT);
pinMode(D5,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);

digitalWrite(D1,HIGH); // Make it low if you want everything to go off
digitalWrite(D2,HIGH); // in case of a power cut
digitalWrite(D3,HIGH);
digitalWrite(D4,HIGH);
digitalWrite(D5,HIGH);
digitalWrite(D6,HIGH);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
Blynk.begin(auth, ssid, pass);


}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V1) // Triggered automatically when the value of V1 changes
{
 if (param.asInt()) // if the value from the widget is 1
 {
    digitalWrite(D1,LOW);
    digitalWrite(D2,LOW);
    digitalWrite(D3,LOW);
    digitalWrite(D4,LOW);
 }
 else // if the value from the widget is 0
 {
    digitalWrite(D1,HIGH);
    digitalWrite(D2,HIGH);
    digitalWrite(D3,HIGH);
    digitalWrite(D4,HIGH);
 }
}

This one is working supercorrect. Extend to D8 if you need all to turn on when You press virtual pin V1. If you need any help, message me on anand.ankesh67@gmail.com or Whatsapp me on +91-8209372353 (I am super-active)

Hi @ankeshanand please edit your post and use the correct characters (triple backticks). They look like this:
```

Pete.

Thanks for telling. BTW I also want to know to know about the Timer widget and Blynk Timer. Could you tell me???

Timer widget and BlynkTimer are two totally different things.
Read the documentation and search the forum.

Pete.