Blynk connection cuts off often

Hey there!

I have a WeMos D1 board with DHT12 and BMP280 sensors attached to it. All the pins are working well, in some cases I do get DHT failed to read.

The problem is everything was working fine and since I connected BMP sensors, the blynk internet communication connection began to cut off at random intervals.
It shows me heartbeat timeout on the serial console and blynk cuts off and reconnects.
I have 5 Virtual pins for reading different sensor data, and all of them are working fine.

What might be the issue?

Thank you in advance.

Please post your code. Formatted.

#include <Adafruit_BMP280.h>

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#define BLYNK_PRINT Serial

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

#define DHTPIN 12     // what digital pin we're connected to

#define BMP_SCK 13
#define BMP_MISO 4
#define BMP_MOSI 14 
#define BMP_CS 5

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

Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

float pressure;       //To store the barometric pressure (Pa)
float temperature;    //To store the temperature (oC)
float SLpressure_mB;


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "the autho key is legit, believe me!";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "The Empire Strikes back";
char pass[] = "xxx9023xxx";

BlynkTimer timer;

void myTimerEvent()
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V3, dht.readTemperature());
  Blynk.virtualWrite(V5, dht.readHumidity());
  Blynk.virtualWrite(V4, SLpressure_mB);
  Blynk.virtualWrite(V2, ELEVATION);
void setup() {
  if (!bme.begin()) {  
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  Serial.println("DHTxx test!");


   Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Setup a function to be called every 30 seconds
  timer.setInterval(30000L, myTimerEvent);


void loop() {
  // Wait a few seconds between measurements.

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t)) {  
    Serial.println("Failed to read from DHT sensor!");

  // Compute heat index in Fahrenheit (the default)
  //float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  //float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.println(" *C ");
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(" *C ");
  Serial.println(" *F");

  pressure = bme.readPressure();
  //temperature = bme.readTemperature();
  SLpressure_mB = (((pressure)/pow((1-((float)(bme.readAltitude(1019)))/44330), 5.255))/100.0);   
  //Print values to serial monitor:
    Serial.print(F("Pressure: "));
    Serial.print(pressure, 2);
    Serial.println(" Pa");
    Serial.print("Equivalent Sea Level Pressure: ");
    Serial.print(SLpressure_mB, 2);
    Serial.println(" mB");
    Serial.print(F("Approx altitude = "));
    Serial.print(bme.readAltitude(1019)); // this should be adjusted to local forcase
    Serial.println(" m");
    ELEVATION = bme.readAltitude(1019);

  //END BMP280 SENSOR READINGS;; // Initiates BlynkTimer

I am not sure if the SPI library, does any interference with your libraries. All my Virtual pins are on PUSH interval settings.

@Vlad_Hristov As requested once already, posted code is to be formatted as per the directions - I fixed your post.

As for your issue, it is due to bad code… you have too much in your void loop() and should be using timers NOT delays!!!

void loop()
:scream: :scream: :scream: :scream:

Delay should not be an issue. Especially when I do the RUN method with timer set for the execution of writing to the virtual pins. I removed the ONLY one delay of 5 seconds and interruptions continue but now they are not as often. Majority of the serial prints are for console viewing purposes, and I am not seeing the code to be that complicated.

If you have it figured out, then is this topic solved? :stuck_out_tongue_winking_eye:

Blynk library has timing requirements as it is constantly communicating back and forth the to server… delays (which STOP all processing) need to be avoided whenever possible… particularly ones used just to slow the void loop() down… since a timed function can do that without blocking.

Thus using timed functions instead of dumping everything in the void loop() is considered better programming practice with Blynk (and with other systems as well… but this is the Blynk forum).

Read through all those links I provided, and all the other in the Help Center, to get a better idea of how to properly setup your sketch.


4 days late and totally out of Blynk (Sorry @Gunner) but… May the 4th be with you …

1 Like