Hi all,
I’ve got my project working with Blynk after some amount of effort. However I’m currently having frequent disconnect/reconnect issues which I’m not sure how best to resolve.
I’ve built some test code just with Blynk only which runs without this particular problem which I discussed here. But it seems I’m hitting some transient condition with the rest of my main project code once Blynk is introduced there.
It’s quite long (500+ lines) so I’ll spare you all the gory details but in summary it does the following;
//#define BLYNK_DEBUG // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_HEARTBEAT 30 // Default is 10s
#define BLYNK_TIMEOUT_MS 10000UL // ms unsigned long
#include <ESP8266_Lib.h>
#include <BlynkSimpleStream.h> // Blynk libraries
#include <WiFiEsp.h> // Include support for ESP8266-01 WiFi module
....
const int updateBlynkInterval = (15 * 1000); // x second interval at which to update Blynk
unsigned long lastBlynkConnectionTime = 0; // Variable Setup
....
void setup() {
....
connectWifi(); // Connect to Wifi using WiFiEsp.h
printWifiData(); // Print Wifi IP/MAC info
printCurrentNet(); // Print Wifi Signal details info
delay(1000);
connectBlynk(); // Init Blynk connection
Blynk.begin(client, auth); // Init Blynk
delay(10);
Serial.println(F("Blynk connected!"));
.....
}
void loop() { // Main execution loop
if (millis() - runtime_1 >= 3000UL) { //
getLocalTempHumid(); // Get local sensor data
displayLocalTempHumid(); // Display local data on screen
runtime_1 = millis();
if(test) { Serial.println(runtime_1);} // Scheduler debug
}
if (millis() - runtime_2 >= 5000UL) {
displayRemoteTempHumid(); // Display remote data on screen
runtime_2 = millis();
if(test) { Serial.println(runtime_2);} // Scheduler debug
}
rf_Receive(); // Receive remote station data over RF link
if (millis() - runtime_3 >= 7000UL) {
displayClock(); // Display clock data on screen
runtime_3 = millis();
if(test) { Serial.println(runtime_3);} // Scheduler debug
}
Blynk.run();
updateBlynk(); // Sends data to Blynk cloud every updateBlynkInterval period
updateThingSpeak(); // Upload data to ThingSpeak
if (status != WL_CONNECTED) { // Reconnect WiFi if disconnected
connectWifi();
return;
}
}
..... <lots of code here for functions above>
bool connectBlynk() { // This function tries to connect to the cloud using TCP
if(test) { Serial.println("connectBlynk()");}
client.stop(); // Dont use client.flush() here, causes likely problems
return client.connect(BLYNK_DEFAULT_DOMAIN, BLYNK_DEFAULT_PORT);
}
void updateBlynk() { // Sends data to Blynk cloud every updateBlynkInterval period
if(millis() - lastBlynkConnectionTime > updateBlynkInterval) {
if(!Blynk.connected()) {
connectBlynk();
Blynk.connect();
}
Blynk.virtualWrite(V0, millis() / 1000);
Blynk.virtualWrite(V1, localtemp);
Blynk.virtualWrite(V2, localhumid);
Blynk.virtualWrite(V3, bmeData.temp);
Blynk.virtualWrite(V4, bmeData.hum);
Blynk.virtualWrite(V5, bmeData.pres);
Blynk.virtualWrite(V6, bmeData.volts);
Blynk.virtualWrite(V7, bmeData.pkt_counter);
lastBlynkConnectionTime = millis();
if(test) { Serial.println("Data sent to Blynk");}
}
}
Debug log shows following,
17:30:50.892 -> [WiFiEsp] Connecting to blynk-cloud.com
17:30:51.031 -> [107843]
17:30:51.066 -> ___ __ __
17:30:51.099 -> / _ )/ /_ _____ / /__
17:30:51.099 -> / _ / / // / _ \/ '_/
17:30:51.133 -> /____/_/\_, /_//_/_/\_\
17:30:51.171 -> /___/ v0.6.1 on Arduino
17:30:51.206 ->
17:30:51.206 -> [107934] Connecting...
17:30:51.490 -> [108276] Ready (ping: 38ms).
17:30:51.923 -> Blynk connected!
17:30:53.928 -> Run..
17:31:54.512 -> [171181] Heartbeat timeout
17:31:54.584 -> [171261] Connecting...
17:32:04.939 -> [181620] Login timeout
17:32:05.048 -> [181701] Connecting...
17:32:06.063 -> [WiFiEsp] Disconnecting 3
17:32:06.098 -> [WiFiEsp] Connecting to blynk-cloud.com
17:32:10.486 -> [187162] Connecting...
17:32:10.804 -> [187474] Ready (ping: 38ms).
17:33:11.626 -> [248195] Heartbeat timeout
17:33:11.728 -> [248276] Connecting...
17:33:22.020 -> [258548] Login timeout
17:33:22.093 -> [258630] Connecting...
17:33:22.447 -> [WiFiEsp] Disconnecting 3
17:33:22.485 -> [WiFiEsp] Connecting to blynk-cloud.com
17:33:27.425 -> [263990] Connecting...
17:33:27.746 -> [264303] Ready (ping: 38ms).
17:34:28.415 -> [324873] Heartbeat timeout
I’ve increased the heartbeat and timeout values as you can see from default, but I don’t think this is the right way to go and it will only go so far. I have a very fast 100Mbps fiber link, so I don’t think it’s the link latency and none of the code is blocking to reach the Blynk 10s timeout. As you can see it happens about every minute or so, but the data does get uploaded correctly. I’m also uploading the same data to Thingspeak which works fine.
My understanding is that Blynk.run() in the main loop should manage the connection to blynk-cloud.com once established, but I’m clearly missing something here. What woudl cause the heartbeat timeout to be reached, any thoughts?
Any help much appreciated, thanks.