BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

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

#1

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?

0 Likes

Comando que detecta fuera de linea en el IDE
#2

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.

0 Likes

#3

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?

0 Likes

#4

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

0 Likes

#5

Perfect, thank you very much for your advise!

0 Likes