Blynk Disconnecting When App is Open

Hi,
I have a simple DHT11 based temperature and it transmits data and show up in the app, but when the app is connected, it goes through a constant connection/disconnection. See the stream below:

Humidity: 47.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.88 *C 78.59 *F
Humidity: 47.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.88 *C 78.59 *F
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
Humidity: 47.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.88 *C 78.59 *F
Humidity: 47.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.88 *C 78.59 *F
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
[285812] Connecting to blynk-cloud.com:8442
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
[288249] Ready (ping: 2275ms).
[293251] Login timeout
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
[295526] Connecting to blynk-cloud.com:8442
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
[298020] Ready (ping: 2275ms).
[303022] Login timeout
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
[305297] Connecting to blynk-cloud.com:8442
Humidity: 47.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.88 *C 78.59 *F
[307693] Ready (ping: 2276ms).
[312695] Login timeout
Humidity: 46.00 %	Temperature: 25.00 *C 77.00 *F	Heat index: 24.76 *C 76.56 *F
[314970] Connecting to blynk-cloud.com:8442
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
[317329] Ready (ping: 2275ms).
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F
Humidity: 48.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.91 *C 78.64 *F
Humidity: 46.00 %	Temperature: 26.00 *C 78.80 *F	Heat index: 25.86 *C 78.54 *F

You can see precisely where the app opens (and disconnection begins) and where the app is closed.
The code is here:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

#define DHTTYPE DHT11   // DHT 11
#define DHTPIN D4     // what pin we're connected to

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MyAUTH";
char ssid[] = "MySSID";
char pass[] = "MyPass";

float f = 0;
float h = 0;
float t = 0;

// contains initialization  
void setup()
{
  Serial.setTimeout(1000);
  Serial.begin(9600);

  dht.begin();
  
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();

  // Wait a few seconds between measurements.
  delay(2000);

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

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // 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(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");

  Blynk.virtualWrite(V2,f);
  Blynk.virtualWrite(V3,h);
}

Any ideas about why this is happening?
Thank you,
Tom

Move everything out of the loop, except Blynk.run(), and put it into a function. Call the function at 2 second intervals with SimpleTimer. You need to do this so Blynk.run() isn’t stopped from running by the massive stall of delay(2000).

1 Like

Solved, thank you. What delay is acceptable?
Regards,
Tom

As you know, Delay function pauses the program… Try to avoid all delays. Blynk hates them…

1 Like

@tommyvdp as @psoro has pointed out you should try to avoid all delays. There are actually two issues:

  1. Currently Blynk have a 10s heartbeat timeout, it was previously 5s, and if you miss a heartbeat you WILL be disconnected from the Cloud server. If you run a local server you can change the heartbeat timeout to quite a high figure but that normally means you have written some pretty poor code. Some poor coders might have a 2s delay which goes into a loop and misses the heartbeat.

  2. ESP’s hate delays more than Blynk does as it needs to be doing “WiFi stuff” on a continuous basis.

Personally I try to keep any delays below 200 milliseconds, anything above this is normally called with SimpleTimer.

2 Likes

Not only blynk :smiley: In general such delays must be avoided in a embedded world.
You can setup a counter in you main loop and delay for 1ms, after 2000 count, read the temperature and reset the counter something like

void loop()
{
static uint16_t myCounter;
Blynk.run();

DTHRead();

myCounter++;
delay(1)

if (myCounter > 2000) {
myCounter = 0;
DTHRead();

}

}