Active Relay causes Blynk to disconnect

I have an app where I use a DHT11 sensor to activate a relay connected to a fan. When the desired temperature is reached, the relay is activated and the fan runs as expected. However, once the relay is active and the fan is running, my Blynk app disconnects.

I am currently using the USB to connect to Blynk.

Any thoughts?

Thank you.

A fewā€¦ Any details, code, descriptions of how you power the relay?

I had something similar with my central heating project. My tentative conclusion was that the esp8266 that I was using for Wi-Fi connection is very sensitive to interference especially on its power supply.

I solved it by changing to the esp32 which seems more interference immune, as well as much faster.

what psu are you using? did you try with a different psu?

The vast majority of Blynkers (including myself) use ESP8266s and relays on a daily basis and donā€™t experience any problems with disconnection when the relay is activated.

Itā€™s important to use decent onto-isolated relays that are powered by a good quality PSU thatā€™s wired correctly (not taking its power via the ESP.

In my experience, most Blynk disconnection issue are the result of bad coding, including failure to use timers when interrogating temperature and humidity sensors.
If you have a piece of code thatā€™s being executed in every void loop cycle, which checks the temperature/humidity readings - possibly using a small delay to allow the sensor time to recover between readings - then you might get get away with this without experiencing Blynk disconnections. However, as soon as the criteria are met that requires the relay to be activated, the code will have other instructions to process, including activating the relay and probably changing the state of some variables. In this scenario, the Blynk.run process is already being ā€˜starvedā€™ of processor time by the badly written void loop and the additional bit of processing needed to run the relay activation routine will be the ā€˜straw that breaks the camels backā€™, causing a disconnection.

Switching to a faster processor (from Arduino to ESP8266 or up again to ESP32) or simply compiling the code to use a faster processor clock speed, may alleviate the symptoms in the short term. This is because youā€™re allowing more processing to be done between Blynk heartbeats, but itā€™s not a long-term solution to the problem.

If your code is well-written and youā€™re using good quality relays and PSUs then it would be worth monitoring Wi-Fi signal strength and looking at whether the thing thatā€™s being switched by the relay is causing interference which is causing the Wi-Fi signal to drop-out (an easy way to test is to remove the load from the relay and see if the problem persists). Things like mai s operated central heating pumps and motorised valves may need a suitable capacitors across their Line and Neutral terminals to reduce EMF interference.

Pete.

1 Like

i had to use pairs of MOVā€™s across the relay contacts to solve this issue with my 24VAC damper motorsā€¦

Has the relay been optically isolated? If not, that could be the issue. You can buy relay boards with Optcouplers on them, Iā€™d advise those any case, just to be on the safe side. You are switching mains supply with those tiny things, so a bit of isolation is actually mandatory :slight_smile:

Use a transistor 2222 tun conect realy

hello,
I think itā€™s probably an EMC problem when the relay is switched on: is there a diode on the relays solenoid to reduce the overvoltage on power supply or itā€™s genarated by the switched load (FAN) ??

THIS is the problem, Iā€™m almost 100% sure of that.

So many assumed solutions to a ā€œproblemā€ that hasnā€™t even been clarified, by a user that hasnā€™t returned to the forum since the initial post :stuck_out_tongue: Too Funny :rofl:

1 Like

Well, probably he found the problem or he forget the project, but brown out problems are coming normally from a defective PSU or solenoids without diodeā€¦ this is a must known in electronics.

1 Like

The OP never mentioned brown outā€¦ rather that fan and relay works, but Blynk disconnectsā€¦ and also connecting using USB indicates an Arduino and USB link that is possibly failing not necessarily powerā€¦ but again, without clarification we could all just as easily assume a dozen other issues.

Ok, Iā€™m back from a long week in the great outdoors!! Thanks for all of the comments, I am pouring through them now. If I had to guess, even if the issue isnā€™t just bad code, I will guess my potentially bad code, (posted below), isnā€™t helping matters. It is basically a bunch of sample code that I have slapped together to start learning, so constructive criticism is more than welcome.

Thank you.

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT SwSerial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
int valvePIN = 9;
int fanPIN = 8;    
#include <BlynkSimpleStream.h>
#include <DHT.h>

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

#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{  
  const int MOISTURE_LEVEL =500;
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit
  int m = analogRead(A0);
  if (isnan(h) || isnan(t)){
    SwSerial.println("Failed to read from DHT sensor!");
    return;
  }
  if (isnan(m)){
    SwSerial.println("Failed to read from Moisture Sensor!");
    return;
  }  
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V7, m);
  //Blynk.virtualWrite(V8, valvePIN);
  Blynk.virtualWrite(V9, fanPIN);

    if(m > MOISTURE_LEVEL)
  {
    Blynk.setProperty(V7, "color", "#D3435C");
    digitalWrite(fanPIN, HIGH);
  }
  else 
  {
    Blynk.setProperty(V7, "color", "#23C48E");
    digitalWrite(fanPIN, LOW);
  }

     if(t > 69)
  {
    //Blynk.setProperty(V8, "color", "#23C48E");
    digitalWrite(fanPIN, LOW);
    delay(30000);
  }
  else 
  {
    //Blynk.setProperty(V8, "color", "#D3435C");
    digitalWrite(fanPIN, HIGH);
  } 

}

void setup()
{
  // Debug console
  SwSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  pinMode(valvePIN, OUTPUT);
  digitalWrite(valvePIN, LOW);
  pinMode(fanPIN, OUTPUT);
  digitalWrite(fanPIN, HIGH);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

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

Not sure if this is turning ON or OFF the fan, but a 30 second delay either way will most definitely trigger Blynkā€™s 10 second heartbeat timeout :stuck_out_tongue: And also blocks all other code from running.

1 Like

yup, itā€™s not a brown out :rofl:

I had problem like that. I use relay board with 8 relays. Power comes from different power supply to relays. also i added diodes and transistors to activate relays.

If i run single relay, no problem. When i run 8 relays at the same time, i lost connection. I figured it out, that was a electromagnetic field problem. I isolated my relays with alluminium and grounded that.

Also changing high power cables with coaxial type may be helpful.

@yilmazyurdakul to keep this topic from further red herringsā€¦ this OP issue is NOT power or interference related. just bad code. :stuck_out_tongue:

1 Like

Ha ha, I will concede to the ā€œbad codeā€ diagnosisā€¦ On that note, how do I go about telling the the relay to go active for a set amount of time without interrupting the blynk communications?

Thanks

Timersā€¦ particularly a timeout timer should work

http://playground.arduino.cc/Code/SimpleTimer#F_setTimeout