Blynk Keep Disconnecting! Need some HELP!

Hello guys, thanks for spending time here and reading this post.

Lets begin:
I am creating a project something like fire detection system by using:

  • Arduino UNO
  • ESP 8266 (ESP-01)
  • Red LED
  • Buzzer
  • MQ2
  • DHT11
  • IR Flame sensor

Basically I am able to connect with Blynk application if there are only 2 sensors working (etc. mq2&dht11 only). Every hardware is working fine. I am really new in this field and not sure if I had flood the server and causing me keep on disconnecting and not able to let it works. Or maybe other factor that causing it. Please help…

Below is my codes:

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <dht11.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
ESP8266 wifi(&EspSerial);

char auth[] = "";
char ssid[] = "";
char pass[] = "";

BlynkTimer timer;
dht11 DHT11;
#define ESP8266_BAUD 9600
#define DHT11PIN 4
#define buzzer 12//buzzer to pin12
#define redLED 13//redLED to pin13
WidgetLCD lcd (V2);

void setup() {
  Serial.begin(9600);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);

  pinMode(redLED, OUTPUT);//act as an output
  pinMode(buzzer, OUTPUT);

  Blynk.begin(auth, wifi, ssid, pass);
  
  timer.setInterval(5000L, MQ2sensor);//timer will run every 5 sec 
  timer.setInterval(6000L, IRsensor);
  timer.setInterval(7000L, DHT11sensor);
}

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

void MQ2sensor()
{
  int mq2Value = analogRead(A0);
  Blynk.virtualWrite(V1, mq2Value);
  Serial.print("Smoke & Gases: ");
  Serial.println(mq2Value);

}

void IRsensor()
{
  int flame = analogRead(A2);//read flame sensor reading at pin A2
  Serial.print("IR Sensor Value: ");
  Serial.println(flame);
 
  if (flame > 800)
  {
    lcd.clear();
    lcd.print(0,0,"MY Room");
    lcd.print(0,1,"Condition:Normal");
    digitalWrite(buzzer, LOW);
    digitalWrite(redLED, LOW);
  }
  else
  {
    lcd.clear();
    lcd.print(0,0,"ALERT!");
    lcd.print(0,1,"FIRE Detected!");
    Serial.println("Flame detected!");
    digitalWrite(buzzer, HIGH);
    digitalWrite(redLED, HIGH);
    Blynk.notify("Flame detected!Please ACT IMMEDIATELY.");
  }
}

void DHT11sensor()
{
  int chk = DHT11.read(DHT11PIN);
  float t = DHT11.temperature;
  float h = DHT11.humidity; 
  Serial.print("Temperature (C): ");
  Serial.println(t, 1);
  Serial.print("Humidity (%): ");
  Serial.println(h);
  Serial.println("");
  Blynk.virtualWrite(V3,t);
  Blynk.virtualWrite(V4,h);

  if(h < 50 || h > 90 || t < 20 || t > 35)
  {
    digitalWrite(buzzer,HIGH);
    digitalWrite(redLED, HIGH);
    Blynk.notify("Alert!Surrounding reading abnormal."); 
  }
  else
  {
    digitalWrite(buzzer,LOW);
    digitalWrite(redLED, LOW);
  }

}

BLYNK_WRITE(V12)
{
  short alarmstatus = param.asInt();
  if(alarmstatus == HIGH)
  {
  digitalWrite(buzzer,HIGH);
  digitalWrite(redLED,HIGH);
  }
  else
  {
  digitalWrite(buzzer,LOW);
  digitalWrite(redLED,LOW);
  }
}

The nr of notifications per sec is limited to 1 every 5 sec. Try to increase the value of your timers to check if this is the problem.

All of your timers are trying to execute at exactly the same time. You should stagger them.

Pete.

@PeteKnight @pierredebuysscher

I tried to change the value for all the timers differently but that doesn’t really helps. But I does noticed that if the sketches memory > than 70%, sometimes my Blynk will keep on disconnecting. Below is the usage for the most recent code.

Sketch uses 22952 bytes (71%) of program storage space. Maximum is 32256 bytes.
Global variables use 1356 bytes (66%) of dynamic memory, leaving 692 bytes for local variables. Maximum is 2048 bytes.

Is that the issue or just some coincidence? Below is my new codes that able to works properly, but I had removed many commands from the functions…I need those T_T …Add more line will caused Blynk keep on disconnecting and I not sure why…

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <dht11.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
ESP8266 wifi(&EspSerial);

char auth[] = "";
char ssid[] = "";
char pass[] = "";

BlynkTimer timer;
dht11 DHT11;
#define ESP8266_BAUD 115200
#define DHT11PIN 4
#define buzzer 12//buzzer to pin12
#define redLED 13//redLED to pin13
WidgetLCD lcd (V2);

void setup() {
  Serial.begin(115200);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);

  pinMode(redLED, OUTPUT);//act as an output
  pinMode(buzzer, OUTPUT);

  Blynk.begin(auth, wifi, ssid, pass);
  
  timer.setInterval(5000L, MQ2sensor);//timer will run every 5 sec 
  timer.setInterval(6000L, IRsensor);
  timer.setInterval(7000L, DHT11sensor);
}

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

void MQ2sensor()
{
  int mq2Value = analogRead(A0);
  Blynk.virtualWrite(V1, mq2Value);

  Serial.print("Smoke & Gases: ");
  Serial.println(mq2Value);
}

void IRsensor()
{
  int flame = analogRead(A2);//read flame sensor reading at pin A2
  Serial.print("IR Sensor Value: ");
  Serial.println(flame);
 
  if (flame > 800)
  {
    lcd.clear();
    lcd.print(0,0,"My Room");
    lcd.print(0,1,"Condition:Normal");
  }
  else
  {
    lcd.clear();
    lcd.print(0,0,"ALERT!");
    lcd.print(0,1,"FIRE Detected!");
    Serial.println("Flame detected!");
    digitalWrite(buzzer, HIGH);
    digitalWrite(redLED, HIGH);
    Blynk.notify("Flame detected!Please ACT IMMEDIATELY.");
  }
}

void DHT11sensor()
{
  int chk = DHT11.read(DHT11PIN);
  float t = DHT11.temperature;
  float h = DHT11.humidity; 
  Serial.print("Temperature (C): ");
  Serial.println(t, 1);
  Serial.print("Humidity (%): ");
  Serial.println(h);

  Blynk.virtualWrite(V3,t);
  Blynk.virtualWrite(V4,h);

  if(h < 50 || h > 90 || t < 20 || t > 35)
  {
    digitalWrite(buzzer,HIGH);
    digitalWrite(redLED, HIGH);
    Blynk.notify("Alert!Room reading abnormal."); 
  }

}

BLYNK_WRITE(V12)
{
  short alarmstatus = param.asInt();
  if(alarmstatus == HIGH)
  {
  digitalWrite(buzzer,HIGH);
  digitalWrite(redLED,HIGH);
  }
  else
  {
  digitalWrite(buzzer,LOW);
  digitalWrite(redLED,LOW);
  }
}

SoftwareSerial doesn’t work well at speeds higher then 9600, especially on a device like an Uno.
However, you can’t just change this value in your sketch. You have to re-configure the ESP-01 to work at the lower baud rate, using AT commands.

TBH, you’d be better-off throwing the UNO/ESP-01 in the bin and using a NodeMCU or similar, as this would also fix your memory issues…

Pete.

Hi Pete,

sorry for not updating for a week, I had updated the latest code above. I had changed the baudrate to 9600, it does works smoother than before. But I still cannot add this code in to the MQ2sensor section:

if(mq2Value > 250)
  {
    Serial.print("Smoke detected!");
    digitalWrite(buzzer, HIGH);
    digitalWrite(redLED, HIGH);
    Blynk.notify("Alert!Smoke Detected!");//blynk push notification
  }
  else
  {
    digitalWrite(redLED, LOW);
    digitalWrite(buzzer, LOW);
  }

After I add these line of commands, I get the following result in the serial monitor:

    11:43:49.853 ->    / _ )/ /_ _____  / /__
    11:43:49.899 ->   / _  / / // / _ \/  '_/
    11:43:49.899 ->  /____/_/\_, /_//_/_/\_\
    11:43:49.945 ->         /___/ v0.6.1 on Arduino Uno
    11:43:49.992 -> 
    11:43:50.411 -> [587] Connecting to Home
    11:43:53.580 -> [3772] AT version:1.1.0.0(May 11 2016 18:09:56)
    11:43:53.626 -> SDK version:1.5.4(baaeaebb)
    11:43:53.674 -> compile time:May 20 2016 15:08:19
    11:43:53.720 -> OK
    11:43:54.654 -> [4850] Failed to enable MUX
    11:43:57.865 -> [8048] +CIFSR:STAIP,"192.168.0.101"
    11:43:57.911 -> +CIFSR:STAMAC,"8c:ce:4e:c7:45:64"
    11:43:57.959 -> [8056] Connected to WiFi
    11:44:08.264 -> [18443] Ready (ping: 25ms).

My Blynk Application still cannot connect and start working properly.

Your serial output looks normal (use triple backticks when posting this, not blockquotes) and it says that your device has connected to Blynk with a 25ms ping time.

The “Connected to WiFi” message seems strange though, is this something you’ve added to your sketch but not included in the code you posted?

This section of code is badly written, as it will try to send a Blynk notification every 5 seconds while the mq2Value is greater than 250. This is is being called every 5 seconds, which is the absolute minimum time between notifications that Blynk will allow.
You’d be better sending one message, and setting a flag so you know not to send any more. Then re-setting that flag when the mq2Value falls below 250.

Pete.

Hi Pete,

The flag that you stated is something like the below?

  int flag = 0;
    if(mq2Value > 250)
      {
        Serial.print("Smoke detected!");
        digitalWrite(buzzer, HIGH);
        digitalWrite(redLED, HIGH);
        Blynk.notify("Alert!Smoke Detected!");//blynk push notification
    flag = 1;
      }
    else 
    {
    flag = 0;
    }

Something like that, although it depends where you intend to place this declaration:

Also, don’t use the variable name of flag, use something more descriptive like blynk_notification_sent so that you’ll understand what it does when you debug it later.
It’s also good practice to add some commentary to the code when you declare the variables…

int blynk_notification_sent = 0; // flag to show when a notification has been sent, used to prevent multiple notifications from being sent for a single smoke detection event.

Pete.