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

Relays Going Off

Hello guys, how are you all doing? I will try to explain this in the best possible way. If you don’t get anything, just ask me. So let’s go.

I’m working on a simple home automation project for my house using ESP32 with WiFi connection and Blynk Server, not local server. So far the logic is very simple. The ideia so far is to use relays to control lights. For now, nothing more. The lights will be controlled by the Blynk App, Wall Switches and Google Assistant. I will be controlling just 8 lights. Over time I will add more functions to the project, but I’m trying to keep it simple as I’m developing it, so it will not be a mess so soon.

I’m using a 16 Relay Module (Low Level Trigger), remember that I’m using just 8 relays out of those 16. I will be using 8 AC Voltage Sensors to know when the lights are turned on or off using wall switches so I can have button feedback within the Blynk App. I’m still coding and implementing stuff. For example the AC Voltage sensors are NOT done yet, but the issue I’m having has nothing to do with it. This circuit is powered by a 5V 1A power supply.

I’ve tested the button feedback using Google Assistant and IFTTT and I know that the button feedback is working fine. If I turn on or off any relay using Google Assistant and IFTTT I have the response within Blynk App. This part of the code is fine.

So I started the real world tests. I turned on all the 8 relays and I left it on to know if they wouldn’t turn off for some reason. And they did. After about 30 minutes all the relays turned off, with no feedback within the app. So I tried again. This time all the relays were off within 5 minutes. One more time and all the relays were off immediately after I turned them on. Finally the app was saying “project is offline” and stayed like this until I reboot the ESP. It doesn’t matter if I use the button ALL ON or if I turn them on one by one or even using Google Home and IFTTT. After some time they all always turn off and I have no idea why.

On my app (iOS version) I’m using 10 buttons. All off them are virtual. V1 to V8 are buttons the control one relay each. V9 Button will turn all the relays on and V10 button will turn all the relays off. V1 to V8 are switch button. V9 and V10 are push buttons.


#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
//#include <TimeLib.h> //not using it yet

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // auth token

char ssid[] = "xxxxxxxxxxxx";    //Wifi name
char pass[] = "xxxxxxxxxxx";    //Wifi password


// relays pins 

int relay1 = 2; 
int relay2 = 4;
int relay3 = 5;
int relay4 = 18;
int relay5 = 19;
int relay6 = 21;
int relay7 = 22;
int relay8 = 23;

// using Boolean variables to declare the button state

boolean switch1State;
boolean switch2State;
boolean switch3State;
boolean switch4State;
boolean switch5State;
boolean switch6State;
boolean switch7State;
boolean switch8State;

// 
BLYNK_WRITE(V1)  // every time the button is pressed, this will run
{
  switch1State = param.asInt();  // get the switch value
  if (switch1State == HIGH) {
    digitalWrite(relay1, HIGH);  // set the pin to HIGH
  } else {
    digitalWrite(relay1, LOW);  // set the pin to LOW
  }
}

BLYNK_WRITE(V2) 
{
  switch2State = param.asInt();  
  if (switch2State == HIGH) {
    digitalWrite(relay2, HIGH);  
  } else {
    digitalWrite(relay2, LOW); 
  }
}

BLYNK_WRITE(V3)  
{
  switch3State = param.asInt();  
  if (switch3State == HIGH) {
    digitalWrite(relay3, HIGH);  
  } else {
    digitalWrite(relay3, LOW);  
  }
}

BLYNK_WRITE(V4)  // 
{
  switch4State = param.asInt();  
  if (switch4State == HIGH) {
    digitalWrite(relay4, HIGH);  
  } else {
    digitalWrite(relay4, LOW);  
  }
}

BLYNK_WRITE(V5)  
{
  switch5State = param.asInt(); 
  if (switch5State == HIGH) {
    digitalWrite(relay5, HIGH);  
  } else {
    digitalWrite(relay5, LOW);
  }
}

BLYNK_WRITE(V6)  
{
  switch6State = param.asInt();  
  if (switch6State == HIGH) {
    digitalWrite(relay6, HIGH);
  } else {
    digitalWrite(relay6, LOW); 
  }
}

BLYNK_WRITE(V7)  
{
  switch7State = param.asInt(); 
  if (switch7State == HIGH) {
    digitalWrite(relay7, HIGH); 
  } else {
    digitalWrite(relay7, LOW); 
  }
}

BLYNK_WRITE(V8)  
{
  switch8State = param.asInt();  
  if (switch8State == HIGH) {
    digitalWrite(relay8, HIGH);  
  } else {
    digitalWrite(relay8, LOW);
  }
}

BLYNK_WRITE(V9){  // all on function 
    
    
      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(relay5, LOW);
      Blynk.virtualWrite(V5, LOW);

      digitalWrite(relay6, LOW);
      Blynk.virtualWrite(V6, LOW);

      digitalWrite(relay7, LOW);
      Blynk.virtualWrite(V7, LOW);

      digitalWrite(relay8, LOW);
      Blynk.virtualWrite(V8, LOW);
    
 }

 BLYNK_WRITE(V10){  // all off function
 
      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);

      digitalWrite(relay7, HIGH);
      Blynk.virtualWrite(V7, HIGH);

      digitalWrite(relay8, HIGH);
      Blynk.virtualWrite(V8, HIGH);
    
 }

 void setup()
{

  pinMode(relay1, OUTPUT); 
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);

  
  digitalWrite(relay1, HIGH); // Set HIGH (off) when BOOT
  digitalWrite(relay2, HIGH); 
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
  digitalWrite(relay5, HIGH);
  digitalWrite(relay6, HIGH);
  digitalWrite(relay7, HIGH);
  digitalWrite(relay8, HIGH);
  

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

}

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


I will upload a picture of the system in the comments. Please, any advice on how to make this code better will be appreciated. Thanks guys.

As you can see, the AC Voltage sensors are not connected yet because I’m not using them for now. This will be the next thing to do, I’m firstly trying to figure out why the relays are turning off.

Did you check your power supply temperature after problem? May be voltage/amp problem. If power is ok, check internet connection. You must syncronise your virtual pin states after reboot.

I am using 8 relay in similar system for weeks without problem. I just added heatsink on regulator.

Same problem on 1 relay on?

1 Like

So, I did check the temperature of the power supply and It was ok. I think the internet is not the problem here. Anyways, what do you mean by “you must syncronise your virtual pin states after reboot.” ?

Same problem on 1 relay on? I did try only one relay, but I tried 4 and the I still have the problem.

@elsonmpf, I am using 4 relays and 433 Mhz transmiter with similar code without issue.
did you monitoring the serial port ?
are you powering the relay module with 5V ?

BTW, nice video. Witch hardware are you using to dimmer the lights ?

Yes Alexis. The relays are being powered with 5V as well as the ESP32. I’m not monitoring the serial port. Should I do it?

You need to sync your data after reconnection. If you dont have any problem on your power supply, you need to add this code into your sketch. After closing relay, you need to wait for reconnection. If your power supply is working without problem, relays will come back after connection.

Dimmer is working on NPN transistor with PWM signal. Its 12 volts. Serial monitor is really good idea :smiley:

bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

less than 7$

14-020283

yes you should !

ohh nice, I will upload the code and run some tests.

Awesome Alexis. I will take a look at it. As I said I will be improving the system with time.

I will take a look on serial monitor.

you will see if blynk disconnected or something else

Nice, I’m try to implement the code right now.

Ok guys, I just updated the code with:

bool isFirstConnect = true;
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
}
}

I’m timing for how long the system will be on.

Alexis, after this I will implement the serial monitor.

you have just to active serial monitor in your IDE interface top right corner

14-020284

No no, I will use the serial monitor on the app. It doesn’t work if I connect the USB to the ESP32 and the Computer with the relays connected. The USB port always stop working.

Ok guys. So apparently the relays are ok now. I left it on for 12+ hours and they did not turn off.

Did you find the problem? Internet or power problem?