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

BLYNK.connect() timeout event syntax

I want to add an event if a connection timeout occurs and was wondering if I can get a comment on what I have found (and it compiles).

Reading the doc’s I see that “bool result = Blynk.connect();” will give a false if the default 30 second timeout occurs. So I have said…

Blynk.begin(auth, ssid, pass);
 
 while (Blynk.connect() == false) {
     bool result = Blynk.connect();      
       if(!result){
           goToSleep();
       }
 }

Thanx

Please edit your post and add triple backticks ( ``` ) before and after your sketch.

I had it inside “code” mark down but the quotes seem to maintain format so thanx for that

Blynk.begin is a blocking command. Code execution will not progress on to the while loop until a connection to WiFi and the Blynk server has successfully been established.
This makes all of your code within the while loop redundant.

If you want a non-blocking connection routine then you should start by manually creating the WiFi connection, then if this is successful use Blynk.config() and Blynk.connect().
Using a while loop is not appropriate in that situation.

Pete.

1 Like

@PeteKnight Thank you for your comment and feedback, the fountain of knowledge that you offer this forum is second to none and greatly appreciated.

You’re right, this is about the WiFi connection more than the Blynk server connection. If the Wifi connects I don’t seem to have failures or timeouts with the Blynk server connection but being a rural location and battery project there can be times when there is no WiFi due to a blackout and just going back to sleep till the next cycle seems the logical way to go.

Information or documentation just seems to be very thin on this subject?

You might want to have a look at this project…

Note that you’ll need to use blynk.cloud rather than blynk-cloud.com in the Blynk.config() for the new version of Blynk, and add your template ID at the very top of the sketch.

It’s worth reading through the whole of the topic, as the project changes significantly over time.

Pete.

1 Like

Thank you yet again @PeteKnight, Mr Google didn’t find this so the reference referral is much appreciated.

FYI, before your reply came through I had a crack at my own 2-step, 2 x timeout config. It compiles fine and I just need to test it to confirm. It’s close to the same as your beehive project except I also added the Blynk connect timeout to cover all bases. For archive completeness this is what I’ll try (and refine as needed). Once tested and confirmed I’ll report back for the record.

Once again, thank you!
Cheers
Ralph

// Constants
#include "credentials.h"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266_SSL.h>

// WiFi Callback
bool connectToWiFi(void){   
    WiFi.begin(ssid, pass);
    int i = 0;
    while(WiFi.status() != WL_CONNECTED){
        i++;
        delay(500);
        if(i >= 60){
            return false;
        }
    }
    WiFi.mode(WIFI_STA);
    return true;
}

// Setup Calls
  Serial.println("ESP Awake Now............");

  bool wifiConnect = connectToWiFi();
  if(wifiConnect == false){
      goToSleep();
  }
  Blynk.config(auth);   
  while (Blynk.connect() == false) {
      bool result = Blynk.connect();    
        if(result == false){
           goToSleep(); 
        }
  }
  
  Serial.println(""); Serial.println("Blynk Connected............\n");

I still think the while loop is the wrong approach.

When you call Blynk.connect() it will attempt to connect until the timeout expires. The default timeout is 30 seconds.

If it fails then Blynk.connected() (note the difference!) will return false.

Whenever the code encounters a Blynk.run() command, if Blynk.connected() is false then it will attempt to re-connect again.
For this reason, Blynk.connect() could be regarded as being redundant, but I prefer to use it anyway.

More (Legacy) info here…

Pete.

1 Like

I’ve been trying to get this bit of the project sorted theoretically before doing a test but I think I need to try testing the theory now before I waste any more of your time.

I now realize that I don’t need to quit on failure as it would put abnormalities in my daily averaging results. Also in run mode I’ll never hit the main loop so “Blynk.run()” never happens. There will be a “PGM” mode to be always on for updates but that’s separate, already tested and works fine.

Things I’ve been doing have come from my understanding, what already works and the V2 doc’s

If I understand what you are saying when “Blynk.config & Blink.connect” are called setup doesn’t continue till either a connection or timeout? If that’s the case I will try this then:-

 Serial.println("ESP Awake Now............");
  bool wifiConnect = connectToWiFi();
    if(wifiConnect == true){
      Blynk.config(auth);
        bool result = Blynk.connect();
          if(result == true){
            Serial.println("Blynk Connected............");
          }else{
            Serial.println("Blynk Failed to connect but we will Update the data anyway\n");
          }
    }else{
      Serial.println("WiFi Connection Failure......We will Update the data anyway\n");
  }  

Thanks again for all your time and help @PeteKnight
Cheers
Ralph

You are correct. The “while” statement is redundant andI have now tested my latest version as per my last reply and it seems to work as you’d expect. Here’s the serial monitor proof

Thanks for your patience and help on this
Cheers
Ralph

1 Like