Hi,
I’m running Blynk on an ESP32 and works fine as long there’s internet. When the power goes off at the router, the Blynk won’t recover however, the ESP still runs fine with the rest of its code. I tried several Blynk functions but none of them seem to help in my case.
The latest code I used in the loop is this
if (Blynk.connected())
{
Blynk.run();
}
else {
if (DEBUG) Serial.println("Connecting to Blynk again.");
Blynk.disconnect();
Blynk.connect();
}
@Emilio glad it helped but when I said recent I didn’t mean 12 months old.
The default forum search is relevance which makes sense but most recent is equally important on a fast moving site about IOT i.e. Blynk’s system constantly changes.
Pretty sure I posted better code in the last 6 weeks but I’ll leave you to iron out the bugs in my original code
@Costas Thanks for getting back to me. There’re many notes on this issue here and to find a safe and sound code is a challenge. As I said, the code did help me in recover the lost wifi connection however, didn’t like much the 11 second timer. Will keep searching as per your suggestion.
The code is easily modified to change the number of iterations and the duration of the timeout etc.
I certainly posted cleaner code recently to do the same thing for Ethernet and can probably be found with the term NoBlynkBlock.
I have never really been convinced that Blynk timeouts work and I have looked at the code. So in recent versions I just use Blynk.connect() and not Blynk.connect(x) where x was supposed to be so many ms and () is the system default time out. All the break is doing is letting you define your own timeout to bypass the beginners use of the Blynk.begin() blocking routine.
@Costas Understood.
However, the code below didn’t work in my case. It ran ok at startup then, when I turned the wifi off-on, it did not recover.
Why the V11?
It doesn’t do anything, it was just to display something in the app and V11 was spare in my project when I was testing the code.
Pretty sure I tested all eventualities like router off to start and router going off after connecting to the server etc. Did you test the example exactly as it’s written?
@Costas Yes, I used the same test code. The only change I made was to match it to my ESP32 board. Will look into the ESP32 WiFi.h to see if it’s the same as for the ESP8266.
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "PHONE_HOME";
char pass[] = "01234";
// Blynk
char auth[] = "8d7102";
char server[] = "blynk-cloud.com";
BlynkTimer timer;
unsigned int myServerTimeout = 3500; // 3.5s server connection timeout (SCT)
unsigned int myWiFiTimeout = 3200; // 3.2s WiFi connection timeout (WCT)
unsigned int functionInterval = 7500; // 7.5s function call frequency (FCF)
unsigned int blynkInterval = 25000; // 25.0s check server frequency (CSF)
void setup()
{
Serial.begin(115200);
Serial.println();
if (WiFi.status() == 6) {
Serial.println("\tWiFi not connected yet.");
}
timer.setInterval(functionInterval, myfunction);// run some function at intervals per functionInterval
timer.setInterval(blynkInterval, checkBlynk); // check connection to server per blynkInterval
unsigned long startWiFi = millis();
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (millis() > startWiFi + myWiFiTimeout) {
Serial.println("\tCheck the WiFi router. ");
break;
}
}
Blynk.config(auth, server);
checkBlynk();
}
void myfunction() {
Serial.println("\tLook, no Blynk block.");
if (WiFi.status() == 3) {
Serial.println("\tWiFi still connected.");
}
if (Blynk.connected()) {
//Blynk.virtualWrite(V11, millis() / 1000);
}
}
void checkBlynk() {
if (WiFi.status() == WL_CONNECTED)
{
unsigned long startConnecting = millis();
while (!Blynk.connected()) {
Blynk.connect();
if (millis() > startConnecting + myServerTimeout) {
Serial.print("Unable to connect to server. ");
break;
}
}
}
if (WiFi.status() != 3) {
Serial.print("\tNo WiFi. ");
}
Serial.printf("\tChecking again in %is.\n", blynkInterval / 1000);
Serial.println();
}
void loop()
{
if (Blynk.connected()) {
Blynk.run();
}
timer.run();
}
You still have Blynk.begin() which is causing the entries above. Many projects need to keep processing when the internet is down and that can only be done with Blynk.config() and Blynk.connect().
My project is still running locally. I’ve it running a clock and monitoring several signals with no immediate need for internet. I thought “[…] Connecting to blynk-cloud.com:8442” meant that the code is trying to connect to Blynk’s server.
Yes every 5s and I would like it to look like this:
[92205] Connecting to blynk-cloud.com:8442
Sketch still running ......
[97206] Connecting to blynk-cloud.com:8442
Sketch still running ......
[102207] Connecting to blynk-cloud.com:8442
Sketch still running ......
[107208] Connecting to blynk-cloud.com:8442
Sketch still running ......
[112209] Connecting to blynk-cloud.com:8442
Sketch still running ......
[117210] Connecting to blynk-cloud.com:8442
Sketch still running ......
[122211] Connecting to blynk-cloud.com:8442
but I’m sure it will not do this with Blynk.begin(). You have cover when the router is struck by lightning, cat chews the cable and the kids unplug to plug in their iPhone