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

[Tutorial] Blynk Non Blocking Connect Call

The Blynk Library make some blocking network calls, to make sure that it is connected to the blynk server, but when we are making certain projects like this one, we want Blynk to be an helper not the main king, that completely stops all other executuion if it is not able to find its master.

So, in this program I have used a neat trick, I have used an AsyncPing Library that helps to make sure that the device is connected to the internet and not just connected to the WiFi. This is achieved by making the Blynk.connect() call only to be executed when the Ping to the Google Server returns true. This ensures that the sketch completely do not become a blocking sketch, but there are some catch, such as when the blynk server itself is down, than it will stop the sketch, as the device is able to access the internet, but not the blynk server. Some Changes can be made to the sketch, so that if the connection to the blynk server is not achieved, then wait for sometime like 1 hour before reconnecting. So let cut short the crap and let me show you the trick… Use this library AsyncPing (https://github.com/akaJes/AsyncPing)

#include <ESP8266WiFi.h>
#include "AsyncPing.h"
AsyncPing ping;

//Check if internet is up an running.
bool pingCheck = false,
     blynkConnected = false;

//Check if Blynk needs to be connected or not, only if internet is up and running, not only wifi.
void blynkCheckEvent()
{

  if (blynkConnected == false && pingCheck == false)
  {
    ping.begin("8.8.8.8");
  }

  if (blynkConnected == false && pingCheck == true)
  {
    Blynk.connect();
  }
}

void setup()
{
  Blynk.config(auth);
  if ( Blynk.connect())
  {
    blynkConnected = true;
  }
  //The code to make sure that the reconnect code is non blocking, this is achieved by this awesome library that helps us ping in an async manner.
  ping.on(false, [](const AsyncPingResponse & response) {
    if (response.total_recv > 0)
    {
      Serial.println("Ping Sucessfull");
      pingCheck = true;
    }
    else
    {
      pingCheck = false;
      Serial.println("Ping UnSucessfull");
    }

    return true; //doesn't matter
  });
}

void loop()
{
  if (Blynk.connected())
  {
    blynkConnected = true;
    Blynk.run();
    pingCheck = false;
  }
  else
  {
    blynkConnected = false;
  }
}

Do check out my project
ESP8266 and Blynk DashClock
The Git Repository

4 Likes

hi.it is good idea.but where are you using
void blynkCheckEvent()
in this skectsh?

hi, i found a better solution:

Looking for the line that causes the crash, I found it in the BlynkSimpleShieldEsp8266.h library.

Just in line 191 we have this:
while (this-> connect ()! = true) {}

I have simply included the millis () function so that it does not stay at that point for eternity … Such that:
while (this-> connect ()! = true && millis () <1000) {}

Only with this I have managed to make the code work if a Wifi connection is not established.

Greetings.