Unfortunately, it looks like there really is a bug in the Blynk.connect(timeout ) function.
Moreover, the function does not work only if you specify a timeout, if you do not specify it, then everything is fine!
I timeout Blynk.connect(29) and every few seconds the connection status in the application changes from online to offline, while Blynk.connected() never becomes true.
If you set Blynk.connect() then everything works fine.
Here is my code:
#define BLYNK_TEMPLATE_ID "xxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxx"
#define BLYNK_FIRMWARE_VERSION "0.0.1"
// #define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Update.h>
#include <HTTPClient.h>
BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";
bool StartDelay_148632894 = false;
bool WiFiok = false;
bool BlynkCon = false;
unsigned long ConnectTime_148632894;
unsigned long prevMil;
bool Out_238044322_220347088;
bool Out_128069072_220347088;
bool Out_101447533_220347088;
String overTheAirURL = "";
BLYNK_WRITE(InternalPinOTA)
{
overTheAirURL = param.asString();
HTTPClient http;
Blynk.disconnect();
http.begin(overTheAirURL);
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK)
{
return;
}
int contentLength = http.getSize();
if (contentLength <= 0)
{
return;
}
bool canBegin = Update.begin(contentLength);
if (!canBegin)
{
return;
}
Client& client = http.getStream();
int written = Update.writeStream(client);
if (written != contentLength)
{
return;
}
if (!Update.end())
{
return;
}
if (!Update.isFinished())
{
return;
}
reboot();
}
void setup()
{
WiFi.begin(ssid, pass);
Blynk.config(auth);
timer.setInterval(35 * 1000, BlynkConFunction);
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
WiFiok = true;
Out_101447533_220347088 = true;
}
else
{
WiFiok = false;
Out_101447533_220347088 = false;
}
timer.run();
if (WiFiok && Blynk.connected())
{
Blynk.run();
BlynkCon = true;
Out_238044322_220347088 = true;
StartDelay_148632894 = _isTimer(ConnectTime_148632894, 1000);
Out_128069072_220347088 = StartDelay_148632894;
}
else
{
BlynkCon = false;
Out_238044322_220347088 = false;
StartDelay_148632894 = false;
Out_128069072_220347088 = false;
}
}
bool _isTimer(unsigned long startTime, unsigned long period)
{
unsigned long currentTime;
currentTime = millis();
if (currentTime>= startTime)
{
return (currentTime>=(startTime + period));
}
else
{
return (currentTime >=(4294967295-startTime+period));
}
}
void reboot()
{
#if defined(ARDUINO_ARCH_MEGAAVR)
wdt_enable(WDT_PERIOD_8CLK_gc);
#elif defined(__AVR__)
wdt_enable(WDTO_15MS);
#elif defined(__arm__)
NVIC_SystemReset();
#elif defined(ESP8266) || defined(ESP32)
ESP.restart();
#else
#error "MCU reset procedure not implemented"
#endif
for (; ;)
{
}
}
BLYNK_CONNECTED()
{
ConnectTime_148632894 = millis();
Blynk.syncAll();
}
void BlynkConFunction()
{
if (WiFiok && !BlynkCon)
{
Blynk.connect(29);
}
}