Code continue while "connecting" to blynk server or unavailable. With WiFi connected

My code will delay for while when my my NodemMCU tries to connect to blynk server.

I have solved wifi connection issue using timer example in previous post.

but when blynk.connect() runs when wifi is connected WHEN internet is NOT connected(hence cant connect to blynk server). Code will freeze until connection timeout.

Is there a better way to let code proceed even when blynk server is connecting?

i can see in the library that blynk.connect() function calls run(); would that have caused it? since technically we do not want to run without being connected.


Post your code here, otherwise we can’t help you…

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx";

const int checkInterval = 10000; //WiFi/Blynk connection check 10s
const int btnPin = 14; //D3
const int ledPin = 2;  //D4
const int relay1 = 16; //D0

BlynkTimer btnChkTimer;
BlynkTimer blynkChkTimer;

int ledState = LOW;
int btnState = HIGH;

// Every time we connect to the cloud...
  // Request the latest state from the server
  Serial.println("Blynk Server Connected... Syncing from Blynk server.");

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);

// When App button is pushed - switch the state
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
  digitalWrite(relay1, ledState);

void blynkCheck() {
  if (WiFi.status() == 3) {
    if (!Blynk.connected()) {
      Serial.println("WiFi OK...");
      Serial.print("Local IP: ");
      Serial.println("Trying to connect to the Blynk server...");
  if (WiFi.status() == 1) {
    Serial.println("No WiFi connection, offline mode.");

void checkPhysicalButton(){
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
      digitalWrite(relay1, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState);
    btnState = LOW;
  } else {
    btnState = HIGH;

void setup()
  // Debug console

  pinMode(btnPin,INPUT_PULLUP); //set mode pin D3
  pinMode(ledPin,OUTPUT); //set mode pin D4
  pinMode(relay1,OUTPUT); //set mode pin D0

  btnChkTimer.setInterval(100L, checkPhysicalButton);
  blynkChkTimer.setInterval(checkInterval, blynkCheck);


void loop()



@fooshx in setup() something like:

    unsigned long blynktimeout = 4000;    // set to required milliseconds
    unsigned long blynkstart = millis();
    while (Blynk.connect() == false) {
     if (millis() > blynkstart + blynktimeout){
        Serial.println(F("Server not available"));

in loop():

  if(Blynk.connected()){;                     // only do Blynk routines if connected to server

Hi Did your suugestion

The hang is still there whenever it tries to connect to the blynk server code seem to freeze until connection times out

Any ideas? or should I use another timer to cater for the blynk.connect();? but seem like the problem is when its connecting it will wait.


Yes Blynk.begin() is a blocking routine, use Blynk.config() alongside the routine I provided.

but I’m not using blynk.begin(); I’m already using blynk.config(auth); then blynk.connect();
just that when blynk.connect() is called this will freeze for a while when ‘connecting’

It should “freeze” for the 4s per my code extract. Is this too long?

You also need to combine it with a timer that checks if you are connected to the server at intervals.

1 Like