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

Control relay all on ,all off

esp8266

#1
 //6relay  on off all on all off
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SPI.h>
/********************************************/
char auth[] = "xxxxxxxxxxxxxxxxx";//token
char ssid[]   = "xxxxxxx";
char pass[]   = "xxxxxxxxxx";
/**************RELAY********************************/
const int relay1 = 15;//D8-GPIO15  relay1 
const int relay2 = 13;//D7-GPIO13  relay2 
const int relay3 = 12;//D6-GPIO12   relay3 
const int relay4 = 14;//D5-GPIO14   relay4 
const int relay5 = 4;//D2-GPIO4   relay5 
const int relay6 = 5;//D1-GPIO5   relay6 
/***************************************/
SimpleTimer timer;
/***************************************/
WidgetLED led7(V13); //online
//buttonV1,V2, V3,V4,V5,V6 bistabil switch button android
//button V20,V21 monostabil pusch button android  ON/OFF ALL
/***************************************/
BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);
  Blynk.syncVirtual(V20);
  Blynk.syncVirtual(V21);
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V1)  // virtual V1 BISTABIL relay1
{
  if (param.asInt()) {
    digitalWrite(relay1, HIGH);
  
  } else {
    digitalWrite(relay1, LOW);
  }
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V2)  // virtual V2 BISTABIL relay2
{
  if (param.asInt()) {
    digitalWrite(relay2, HIGH);
  
  } else {
    digitalWrite(relay2, LOW);
  }
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V3)  // virtual V3 BISTABIL relay3
{
  if (param.asInt()) {
    digitalWrite(relay3, HIGH);
  
  } else {
    digitalWrite(relay3, LOW);
  }
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V4)  // virtual V4 BISTABIL relay4
{
  if (param.asInt()) {
    digitalWrite(relay4, HIGH);
  
  } else {
    digitalWrite(relay4, LOW);
  }
}
/**********BISTABIL switch button android*******************/
BLYNK_WRITE(V5)  // virtual V5 BISTABIL relay5
{
  if (param.asInt()) {
    digitalWrite(relay5, HIGH);   
  } else {
    digitalWrite(relay5, LOW);
  }
}
/*****************BISTABIL switch button android***************/
BLYNK_WRITE(V6)  // virtual V6 BISTABIL relay6
{
  if (param.asInt()) {
    digitalWrite(relay6, HIGH);   
  } else {
    digitalWrite(relay6, LOW);
  }
}

/*****************MONOSTABIL push button android***************/
BLYNK_WRITE(V20)//all ON  relay1,2,3,4,5,6
{
  if (param.asInt()) {
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V1, HIGH);
    digitalWrite(relay2, HIGH);
    Blynk.virtualWrite(V2, HIGH);
    digitalWrite(relay3, HIGH);
    Blynk.virtualWrite(V3, HIGH);
    digitalWrite(relay4, HIGH);
    Blynk.virtualWrite(V4, HIGH);
    digitalWrite(relay5, HIGH);
    Blynk.virtualWrite(V5, HIGH);
    digitalWrite(relay6, HIGH);
    Blynk.virtualWrite(V6, HIGH);
      }
}
/****************MONOSTABIL push button android****************/
  BLYNK_WRITE(V21)//all OFF   relay1,2,3,4,5,6
  {
  if (param.asInt()) {
    digitalWrite(relay1, LOW);
    Blynk.virtualWrite(V1, LOW);
    digitalWrite(relay2, LOW);
    Blynk.virtualWrite(V2, LOW);
    digitalWrite(relay3, LOW);
    Blynk.virtualWrite(V3, LOW);
    digitalWrite(relay4, LOW);
    Blynk.virtualWrite(V4, LOW);
    digitalWrite(relay4, LOW);
    Blynk.virtualWrite(V4, LOW);
    digitalWrite(relay5, LOW);
    Blynk.virtualWrite(V5, LOW);
    digitalWrite(relay6, LOW);
    Blynk.virtualWrite(V6, LOW);
  }
}
/***ONLINE*************/
void ledonline()
{
  if (led7.getValue()) {
    led7.off();
  } else {
    led7.on();
  }
}
/*****************************************/
void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  /****************************************/
  pinMode(relay1, OUTPUT);//relay1
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  /**********************************/
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);

  /*************************************************/  
  timer.setInterval(1000L, ledonline);
 }
/********************************************/
void loop()
{
  Blynk.run();
  timer.run();
}


#2

A nice and simple project, you done relay good :stuck_out_tongue_winking_eye:

If you are interested, here is some coding shortcuts that should work…

From this…

to this…

/************BISTABIL switch button android*************/ 
BLYNK_WRITE(V1) // virtual V1 BISTABIL relay1
{ 
    digitalWrite(relay1, param.asInt());  // use value provided by widget
}

And this…

To this

BLYNK_WRITE(V20)  // all ON relay 1,2,3,4,5,6 
{
  if (param.asInt()) { 
    for (int i=1; i <= 6; i++) {  // Loop from 1 to 6
    digitalWrite(i, HIGH); 
    Blynk.virtualWrite(i, HIGH);
    }
  }
}

#3
```
BLYNK_WRITE(V20)  // all ON relay 1,2,3,4,5,6 
```

{
if (param.asInt()) {
for (int i=1; i &lt;= 6; i++) { // Loop from 1 to 6
digitalWrite(i, HIGH);
Blynk.virtualWrite(i, HIGH);
}
}
}
bad
BLYNK_WRITE(V20) // all ON relay 1,2,3,4,5,6
{
if (param.asInt()) {

```
for (int i = 1; i <= 16; i+=1) {  // Loop from 1 to 6
digitalWrite(i, HIGH);  
Blynk.virtualWrite(i, HIGH);
}
```

}
}

#4
//6relay  on off all on all off
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SPI.h>
/********************************************/
char auth[] = "xxxxxxxxxxxxxxx";//token
char ssid[]   = "xxxxx";
char pass[]   = "xxxxx";
/**************RELAY********************************/
const int relay1 = 15;//D8-GPIO15  relay1
const int relay2 = 13;//D7-GPIO13  relay2
const int relay3 = 12;//D6-GPIO12   relay3
const int relay4 = 14;//D5-GPIO14   relay4
const int relay5 = 4;//D2-GPIO4   relay5
const int relay6 = 5;//D1-GPIO5   relay6
/***************************************/
SimpleTimer timer;
/***************************************/
WidgetLED led7(V13); //online
//buttonV1,V2, V3,V4,V5,V6 bistabil switch button android
//button V20,V21 monostabil pusch button android  ON/OFF ALL
/***************************************/
BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);
  Blynk.syncVirtual(V20);
  Blynk.syncVirtual(V21);
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V1) // virtual V1 BISTABIL relay1
{
  digitalWrite(relay1, param.asInt());  // use value provided by widget
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V2) // virtual V2 BISTABIL relay2
{
  digitalWrite(relay2, param.asInt());  // use value provided by widget
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V3) // virtual V3 BISTABIL relay3
{
  digitalWrite(relay3, param.asInt());  // use value provided by widget
}
/************BISTABIL switch button android*************/
BLYNK_WRITE(V4) // virtual V4 BISTABIL relay4
{
  digitalWrite(relay4, param.asInt());  // use value provided by widget
}
/**********BISTABIL switch button android*******************/
BLYNK_WRITE(V5) // virtual V5 BISTABIL relay5
{
  digitalWrite(relay5, param.asInt());  // use value provided by widget
}
/*****************BISTABIL switch button android***************/
BLYNK_WRITE(V6) // virtual V6 BISTABIL relay6
{
  digitalWrite(relay6, param.asInt());  // use value provided by widget
}
/*****************MONOSTABIL push button android***************/
BLYNK_WRITE(V20)  // all ON relay 1,2,3,4,5,6 
{
  if (param.asInt()) {
    
    for (int i = 1; i <= 16; i+=1) {  // Loop from 1 to 6
    digitalWrite(i, HIGH);  
    Blynk.virtualWrite(i, HIGH);
    }
  }
}
/****************MONOSTABIL push button android****************/
BLYNK_WRITE(V21)  // all ON relay 1,2,3,4,5,6 
{
  if (param.asInt()) { 
    
    for (int i = 1; i <= 16; i+=1) {  // Loop from 1 to 6
    digitalWrite(i, LOW); 
    Blynk.virtualWrite(i, LOW);
    }
  }
}
/***ONLINE*************/
void ledonline()
{
  if (led7.getValue()) {
    led7.off();
  } else {
    led7.on();
  }
}
/*****************************************/
void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  /****************************************/
  pinMode(relay1, OUTPUT);//relay1
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  /**********************************/
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);
  /*************************************************/
  timer.setInterval(1000L, ledonline);
}
/********************************************/
void loop()
{
  Blynk.run();
  timer.run();
}