How to avoid code lock if Blynk server is not responding ( or internet is down )

I’m working with ESP8266/32 and I am stuck with something,
I build myself a “smartconnect” función that attempts to connect to wifi credentials savef on the spiff
If it fails, it host a website to enter ssid,pass, auth token, and after saving the credentials the esp restarts.
It works just fine, also if the ESP is connected to they wifi network, It host anothe website to change some parameters…
but this is the issue, if I enter a wrong token, the esp connects to the wifi network, and get stucks forever at “invalid token”… pseudo code as example…

The same problems hapens if by any reasone internet goes down and the esp dot get the heartbeat from blynk server.

the idea of the app is to be able to acces the website hosted on the ESP even if the token is invalid, or if the internet goes down.
I try to raise a flag as “invalid token” fllag, to try Blyk.disconnect() but, the issue is with the Blink.Beguin, it get stuck forever.

Is there any workarround? this is a pseudo code

void setup(){
Serial.begin(115200);
SmartWifiConnect();
Blynk.begin(auth, ssid, pass);
Serial.println(“this will never be printed if internet is down or if token is not valid”);
validTokenFlag = Blynk.isTokenInvalid();
}

void loop(){
if(validTokenFlag){
Blynk.run();
}
if(firstboot){
Blynk.syncAll();
firstboot = false;
}

MyWebServerHandler();
}

You should be able to set a timeout constant: BLYNK_TIMEOUT_MS.

More useful constants can be found here:

1 Like

@Andres_Alvarez
Take a look at @Gunner example, this is the best way to do, we all use his code.

1 Like

@Andres_Alvarez, the problem you have is that you’re using Blynk.begin which is a blocking command. If it can’t establish a connection to the Blynk server, either because it doesn’t get past the process of connecting to WiFi, or because you’ve entered an incorrect Auth code, code execution will stop at this point.

You need to shift to using Blynk.config and Blynk.connect, and you also need to manage your own WiFi connection process.

Pete.

1 Like

thanks Peter
I followed your idea, and now using
Blynk.config(auth, “blynk-cloud.com”, 8442);
Blynk.connect();

works just fine, and it also reconnects by itself

1 Like