Thanks a bunch for the tips. I have already searched this a while, and I struggled with finding a solid and updated way of doing this. Some of the threads I found had tips on how to handle reconnection, only to state later on that changes in either Blynk or ESP8266 code caused it to not work anymore.
So I ended with writing my own connection handler. I’ve tested it for a while now, and it seems to handle both wifi and Blynk server crashes well. I haven’t had any “stuck” situations so far, at least. If you see any issues with this, please let me know.
#define BLYNK_PRINT Serial // Defines the object that is used for printing
//#define BLYNK_DEBUG // Optional, this enables more detailed prints
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer connectionHandlerTimer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "pass";
typedef enum {
CONNECT_TO_WIFI,
AWAIT_WIFI_CONNECTION,
CONNECT_TO_BLYNK,
AWAIT_BLYNK_CONNECTION,
MAINTAIN_CONNECTIONS,
AWAIT_DISCONNECT
} CONNECTION_STATE;
CONNECTION_STATE connectionState;
uint8_t connectionCounter;
void ConnectionHandler(void) {
switch (connectionState) {
case CONNECT_TO_WIFI:
BLYNK_LOG("Connecting to %s.", ssid);
WiFi.begin(ssid, pass);
connectionState = AWAIT_WIFI_CONNECTION;
connectionCounter = 0;
break;
case AWAIT_WIFI_CONNECTION:
if (WiFi.status() == WL_CONNECTED) {
BLYNK_LOG("Connected to %s", ssid);
connectionState = CONNECT_TO_BLYNK;
}
else if (++connectionCounter == 50) {
BLYNK_LOG("Unable to connect to %s. Retry connection.", ssid);
WiFi.disconnect();
connectionState = AWAIT_DISCONNECT;
connectionCounter = 0;
}
break;
case CONNECT_TO_BLYNK:
BLYNK_LOG("Attempt to connect to Blynk server.");
Blynk.config(auth, IPAddress(192, 168, 2, 30), 8080);
Blynk.connect();
connectionState = AWAIT_BLYNK_CONNECTION;
connectionCounter = 0;
break;
case AWAIT_BLYNK_CONNECTION:
if (Blynk.connected()) {
BLYNK_LOG("Connected to Blynk server.");
connectionState = MAINTAIN_CONNECTIONS;
}
else if (++connectionCounter == 50) {
BLYNK_LOG("Unable to connect to Blynk server. Retry connection.");
Blynk.disconnect();
WiFi.disconnect();
connectionState = AWAIT_DISCONNECT;
connectionCounter = 0;
}
break;
case MAINTAIN_CONNECTIONS:
if (WiFi.status() != WL_CONNECTED) {
BLYNK_LOG("Wifi connection lost. Reconnect.");
Blynk.disconnect();
WiFi.disconnect();
connectionState = AWAIT_DISCONNECT;
connectionCounter = 0;
}
else if (!Blynk.connected()) {
BLYNK_LOG("Blynk server connection lost. Reconnect.");
Blynk.disconnect();
connectionState = CONNECT_TO_BLYNK;
}
else {
Blynk.run();
}
break;
case AWAIT_DISCONNECT:
if (++connectionCounter == 10) {
connectionState = CONNECT_TO_WIFI;
}
break;
}
}
void setup()
{
Serial.begin(9600);
connectionHandlerTimer.setInterval(100, ConnectionHandler);
connectionState = CONNECT_TO_WIFI;
}
void loop()
{
connectionHandlerTimer.run();
}