Note: I am using local Blynk server 0.25.4
I have found a potential bug in Blynk v0.4.8 using the below simple sketch:
Scenario: when using Blynk v0.4.8 it will successfully connect to Blynk server at setup() but then I turn off my local Blynk server to simulate the sketch loosing connectivity to Blynk and as per design the reconnectBlynk() function executes in my script - however it will crash and reset my Wemos D1 Mini; the crash seems to occur at Blynk.connect(4333); statement in reconectBlynk() function. I disable the wdt before I execute this function but still a crash occurs.
When I roll-back to Blynk v0.4.7 and perform the same test reconnectBlynk() executes fine and no hardware resets occur.
Any idea what may be happening within Blynk.connect(4333) function in reconnectBlynk() that could be causing this ?
Test code using below:
#define USE_ESP8266 // compile for esp8266 board
#define BLYNK_PRINT Serial // Defines the object that is used for printing
// #define BLYNK_DEBUG // Optional, this enablecs more detailed prints
#define USE_ESP8266_WIFI_MANAGER
#define DISPLAY_DEBUG_BLYNK_STATUS
#define wifimanager_timeout_setup 120 // 30 seconds timeout on setup only
#define wifimanager_timeout_reconnect 60 // 30 seconds timeout on reconnect attempts
//========== END SWITCHES ======================
#ifdef USE_ESP8266
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// #include <BlynkSimpleEsp8266_SSL.h>
#include <ArduinoOTA.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#endif
// #include <SimpleTimer.h> // already part of BlynkTimer
#include <TimeLib.h>
#include <WidgetRTC.h>
int timer_ids[5];
int counter = 0;
#define BaudRate 115200
SimpleTimer timer;
char auth_code[] = "yourcode";
char blynk_server[] = "server";
void setup()
{
// put your setup code here, to run once:
Serial.begin(BaudRate);
#ifdef USE_ESP8266_WIFI_MANAGER
WiFiManager wifiManager;
wifiManager.setTimeout(wifimanager_timeout_setup);
if (!wifiManager.autoConnect("WiFi"))
{ // might not need if statement
Serial.println("failed to connect, we should reset as see if it connects");
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("Failed to connect to internet");
}
delay(3000);
}
#endif
Blynk.config(auth_code, blynk_server);
if (Blynk.connect())
{
Serial.println("BLYNK is Connected!");
// rtc.begin();
}
else
{
Serial.println("BLYNK is Not Connected!");
}
#ifdef USE_ESP8266
ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("reconnect test");
// No authentication by default
ArduinoOTA.setPassword((const char *)"");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
});
ArduinoOTA.begin();
#endif
timer_ids[0] = timer.setInterval(25000L, reconnectBlynk); // check every 10s time if still connected to server
timer_ids[1] = timer.setInterval(1000L, im_connected);
}
void im_connected()
{
Serial.print(counter++);
if (Blynk.connected())
{
Serial.println(" : All good and connected to Blynk!");
}
else
{
Serial.println(" : Seemed to have lost connected to Blynk :-(");
}
}
void loop()
{
// put your main code here, to run repeatedly:
if (Blynk.connected() == true)
{
Blynk.run();
}
timer.run();
#ifdef USE_ESP8266
ArduinoOTA.handle();
#endif
#ifdef USE_ESP8266
ESP.wdtFeed(); // feed watchdog
#endif
}
//===== RE CONNECT IF BLYNK NOT CONNECTED =====
void reconnectBlynk()
{
#ifdef DEBUG_TIMER
Serial.println("timer_ids[x] = reconnectBlynk()");
#endif
bool internet_connected = true;
if (Blynk.connected() == false)
{
Serial.println("******************************* In reconnect");
#ifdef USE_ESP8266
ESP.wdtFeed();
#endif
#ifdef USE_ESP8266_FIXED_WIFI
if (WiFi.status() != WL_CONNECTED)
{
#ifdef USE_ESP8266
ESP.wdtDisable();
#endif
WiFi.begin(ssid, pass); // Connect to WiFi network
Serial.println("Waiting to connect to Wifi:");
long waittime = millis() + 7000; // 7 seconds wait time
while (WiFi.status() != WL_CONNECTED)
{ // Wait for board to connect to WiFi network
delay(500);
Serial.print(".");
if (millis() > waittime)
{
break;
internet_connected = false;
}
}
Serial.println(" ");
#ifdef USE_ESP8266
ESP.wdtEnable(WDTO_8S); // enable Watch Dog Time WDT for 8s
#endif
}
#endif // use_esp8266_fixed_wifi
#ifdef USE_ESP8266_WIFI_MANAGER
if (WiFi.status() != WL_CONNECTED)
{
#ifdef USE_ESP8266
ESP.wdtDisable();
#endif
WiFiManager wifiManager;
wifiManager.setTimeout(wifimanager_timeout_reconnect); // wait for 90 seconds until timeout
if (!wifiManager.autoConnect("esp8266 WiFi"))
{ // might not need if statement
Serial.println("failed to connect, we should reset as see if it connects");
internet_connected = false;
delay(3000);
}
#ifdef USE_ESP8266
ESP.wdtEnable(WDTO_8S); // enable Watch Dog Time WDT for 8s
#endif
}
#endif // use_esp8266_wifi_manager
#ifdef USE_ESP8266
ESP.wdtDisable();
Serial.println("reconnectBlynk(): WDT disabled");
#endif
if (internet_connected)
{
Serial.println("reconnectBlynk(): Before Blynk.connect");
if (Blynk.connect(4333))
{
Serial.println("reconnectBlynk(): After Blynk.connect successfull");
#ifdef USE_ESP8266
ESP.wdtEnable(WDTO_8S); // enable Watch Dog Time WDT for 8s
Serial.println("reconnectBlynk(): WDT enabled");
#endif
}
else
{
Serial.println("reconnectBlynk(): After Blynk.connect UN-successfull");
#ifdef USE_ESP8266
ESP.wdtEnable(WDTO_8S); // enable Watch Dog Time WDT for 8s //
Serial.println("reconnectBlynk(): WDT enabled");
#endif
}
} // end internet connectection
else
{
Serial.println("We don't have internet connection !");
#ifdef USE_ESP8266
ESP.wdtEnable(WDTO_8S); // enable Watch Dog Time WDT for 8s //
Serial.println("reconnectBlynk(): WDT enabled");
#endif
}
Serial.println("reconnectBlynk(): restarting timer and exiting function");
timer.restartTimer(timer_ids[0]); // restart "reconnect" timer
} // end if !blynk_connected
}