I am facing issue of Heartbeat timeout. I have reconnection code which is running via SimpleTimer but it never connects back. Really not sure what is going wrong. Sometimes, it works by entirely restarting ESP8266.
Logs says like this
[1198811] Heartbeat timeout: 1198811, 1179810, 1198169
[1203170] Connecting to blynk-cloud.com:80
Reconnection code:
void setupTimers() {
simpleTimer.setInterval(60000L, checkBlynkConnection);
}
void blynkConnect() {
for (int x = 0; x < 10; x++) {
bool result = Blynk.connect();
if (result) {
break;
} else {
if (x == 9) {
Serial.println("Blynk not connected - resetting");
restartESP();
delay(500);
}
}
delay(3000);
}
}
void checkBlynkConnection() {
for (int x = 0; x < 10; x++) {
bool result = Blynk.connected();
if (result) {
break;
} else {
if (x ==9) {
blynkConnect();
delay(500);
}
}
delay(3000);
}
}
Agree on what Pete says. The only way to get this stable is either fix that or get an ESP32 which features two cores that you can use. One for Blynk and one for your other stuff.
But maybe there is another question which needs answering first, why would you want to handle connection management yourself? Why not have Blynk fix that for you? Is there any particular reason you want to reset after ten tries? Just being curious a bit too
To answer your question - if the device goes offline(meaning connection lost from Blynk or sloppy internet), it never connects back. Hence, It tries to connect back to blynk and tries for 10 times. How can connection be restored once lost without writing additional timer code? Just curious to know.
Thanks for all the help as always! Documentation was very helpful.
One query - does blynk automatically connect back if the connection is lost for some reason - may be lost internet, blynk restart etc.?
Secondly, I will minimize the calls in the loop and test further.
Blynk will handle it and with a clean loop() it will be much more stable. Reading the serial port can already wreak havoc on things, let alone if you insert conditional statements
@vishalavalani, you mentioned in another topic that you’re thinking of using Node-Red.
If go down that route then you don’t need to run any Blynk code on your devices at all. My standard setup is to run Wi-Fi and MQTT code on my devices. The devices talk to my Node-Red server using MQTT and Node-Red talks to Blynk.
One advantage of this is that MQTT connection to Node-Red is much more accommodating in terms of cluttered void loops. Obviously you shouldn’t use this as a way of getting around bad programming, but there are some things, like waiting for data over a serial interface that are very difficult to do in the Blynk environment (unless you use @Lichtsignaal’s suggestion of utilising multiple cores within the ESP32.
If you’ve not already come across it, then you might find this topic useful…
@PeteKnight thanks for detailed information. it is very useful. I have also purchased another router and testing on it. So far things are better and may be router was the culprit which is very unfortunate.
Wifi can be a pain in the butt and there are so many factors which will contribute to bad signal (I know, I manage 1600+ accesspoints on a daily basis …). The router could be an issue, but it’s usually a series of things combined which will turn things over for the worst or better.
I still would advice to refactor the loop() as to reach the highest possible degree of robustness.