Proper connection management to avoid having unconnected Blynk hold up a script

Hey all, so am working on a Blynk script to run on the Sonoff Home Automation Switches from ITead and had a couple of questions.

Am I right in saying that if my wireless drops out for some reason (eg, Wireless AP is reset) then

Blynk.run();

Will reconnect to the wireless for me? And that also handles the initial connection?

Does it “block” the rest of the sketch waiting for the wireless to connect?

The device should also be checking a physical switch state which should operate whether the wireless is connected or not. (Don’t want to not be able to turn my lights on/off if my wifi goes down for some reason).

If so, would something like this work in the Loop of my sketch

bool isconnected = Blynk.connected();

if (isconnected) {
  Blynk.run();
  } else {
  Blynk.connect(5);
}

I’m running a local blynk server, so i’m hoping that 5 seconds should be enough. I don’t want to delay my sketch any further than that.

Or is there a better way to do what I want to do?

It is a combination of things…

First when using ESP as a standalone device, you should use Blynk.config() instead of Blynk.begin() to avoid it blocking without connection at boot.

http://docs.blynk.cc/#blynk-firmware-configuration

Then yes, as you have surmised, you want to use other connection management commands…

void loop()
{
  if (Blynk.connected()) { // checks to see if connected to server
    Blynk.run(); // runs only if connected to server
  }
  timer.run(); // runs all the time
}

Then add in a timer every few minutes to reconnect, if necessary, using Blynk.connect()… meanwhile the rest of your script keeps running.

Yes, I am using Blynk.config

So, the timer won’t block the rest of the sketch when running?

Even if the Blynk.connect() is sitting waiting for a connection?

That is one of the purposes of the timer… Non-blocking. Also runs independent of a server connection.

Perfect, thank you very much for your advise!