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 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.
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.
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
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) ??
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 Too Funny
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.
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 And also blocks all other code from running.
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.
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?