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.

Jack

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...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Serial.println("Blynk Server Connected... Syncing from Blynk server.");
  Blynk.syncAll();

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

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  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(WiFi.localIP());
      Serial.println("Trying to connect to the Blynk server...");
      
      Blynk.connect(1000);
    }
  }
  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
  Serial.begin(9600);

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

  WiFi.begin(ssid,pass);
  Blynk.config(auth);
  
  
  btnChkTimer.setInterval(100L, checkPhysicalButton);
  blynkChkTimer.setInterval(checkInterval, blynkCheck);

}

void loop()
{
  btnChkTimer.run();
  blynkChkTimer.run();

   if(Blynk.connected()){
      Blynk.run();
   }
}

BLYNK_READ(V1){
  Blynk.virtualWrite(V1,WiFi.RSSI());
}

@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"));
        break;     
     }
    }

in loop():

  if(Blynk.connected()){
    Blynk.run();                     // only do Blynk routines if connected to server
  }
2 Likes

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.

Jack

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