BLYNK.connect() timeout event syntax

No, it just adds a degree of complexity to a process that already has quite a few factors to understand when testing. I’d prefer to keep the code structured into chunks that have a clearly defined functions and not have to track the status of a number of flag variables that can easily be eliminated.

Pete.

Here is the result of the test, your code is completely used, I only changed the library from 8266 to 32:

11:44:40.860 ->     ___  __          __
11:44:40.860 ->    / _ )/ /_ _____  / /__
11:44:40.860 ->   / _  / / // / _ \/  '_/
11:44:40.860 ->  /____/_/\_, /_//_/_/\_\
11:44:40.860 ->         /___/ v1.0.1 on ESP32
11:44:40.860 -> 
11:44:40.860 -> Connecting to Wi-Fi...
11:44:40.956 -> Wi-Fi connection - attempt # 1 of 15
11:44:41.481 -> Wi-Fi connection - attempt # 2 of 15
11:44:41.958 -> Wi-Fi connection - attempt # 3 of 15
11:44:42.483 -> Wi-Fi connection - attempt # 4 of 15
11:44:42.960 -> Wi-Fi connection - attempt # 5 of 15
11:44:43.488 -> Successfully connected to Wi-Fi
11:44:43.488 -> Attempting to connect to Blynk...
11:44:43.488 -> [2645] Connecting to blynk.cloud:80
11:44:43.677 -> [2868] Ready (ping: 101ms).
11:44:43.773 -> Blynk.connect() attempt ended
11:44:43.773 -> Blynk connection attempt succeeded - connected
11:45:13.488 -> Checking connections.. WiFi Okay,  Blynk Okay
11:45:43.470 -> Checking connections.. WiFi Okay,  Blynk Okay
11:46:13.458 -> Checking connections.. WiFi Okay,  Blynk Okay
11:46:43.455 -> Checking connections.. WiFi Okay,  Blynk Okay
11:47:13.455 -> Checking connections.. WiFi Okay,  Blynk Okay
11:47:16.808 -> [155997] Heartbeat timeout
11:47:43.470 -> Checking connections.. WiFi Okay,  Blynk not connected
11:47:43.470 -> Attempting to connect to Blynk...
11:47:43.470 -> [182645] Connecting to blynk.cloud:80
11:48:01.985 -> Blynk.connect() attempt ended
11:48:01.985 -> Blynk connection attempt failed - not connected
11:48:13.465 -> Checking connections.. WiFi Okay,  Blynk not connected
11:48:13.465 -> Attempting to connect to Blynk...
11:48:13.465 -> [212645] Connecting to blynk.cloud:80
11:48:31.967 -> Blynk.connect() attempt ended
11:48:31.967 -> Blynk connection attempt failed - not connected
11:48:43.451 -> Checking connections.. WiFi Okay,  Blynk not connected
11:48:43.499 -> Attempting to connect to Blynk...
11:48:43.499 -> [242645] Connecting to blynk.cloud:80
11:49:01.976 -> Blynk.connect() attempt ended
11:49:01.976 -> Blynk connection attempt failed - not connected

Those 18 seconds…
Here is the code itself:

#define BLYNK_TEMPLATE_ID "---"
#define BLYNK_DEVICE_NAME "---"
#define BLYNK_AUTH_TOKEN "---"

#define BLYNK_PRINT Serial

#include <WiFi.h> 
#include <WiFiClient.h> 
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
char ssid[] = "---";
char pass[] = "---";

int blynk_timeout_seconds = 5;                // Timeout value for the Blynk.connect(timeout) command. The library default is 18 seconds
long check_connection_seconds = 30;           // Check the connection every 30 seconds
int wait_between_wifi_attempts_millis = 500;  // How long do we wait (in milliseconds) between each WiFi connection attempt
int max_wifi_connect_attempts = 15;           // How may times we try to connect to WiFi before giving-up 

BlynkTimer timer; 


void setup()
{
  Serial.begin(74880);
  Blynk.config(BLYNK_AUTH_TOKEN); // When we try to connect to Blynk, these are the credentials we'll use
  Connect_to_WiFi();              // Call the function that attempts to connect to WiFi

  timer.setInterval(check_connection_seconds * 1000L, Check_Connections);

  Connect_To_Blynk();             // Call the function that attempts to connect to Blynk
}

void loop()
{
//  if (WiFi.status() == WL_CONNECTED) // Commented-out for testing
  {
    if(Blynk.connected())
    {
      // Only execute Blynk.run if we are connected to Blynk
      Blynk.run();
    }
  }   
  timer.run();  // feed the timer process
}


 void Connect_to_WiFi()
{
  Serial.println(F("Connecting to Wi-Fi..."));
  
  int wifi_attempt_count = 1; // used to track how many times we've tried to connect to WiFi

  if (WiFi.status() != WL_CONNECTED)
  {
      WiFi.begin(ssid, pass); // connect to WiFi
  }

  while (WiFi.status() != WL_CONNECTED  && wifi_attempt_count < max_wifi_connect_attempts) // Loop until we've connected, or reached the maximum number of attemps allowed
  {

    Serial.print(F("Wi-Fi connection - attempt # "));
    Serial.print(wifi_attempt_count);
    Serial.print(F(" of "));
    Serial.println(max_wifi_connect_attempts); 
    timer.run();
    delay(wait_between_wifi_attempts_millis);
    timer.run();
    wifi_attempt_count++;       
  }

  // We reach this point when either we're connected to Wi-Fi, or we've reached the maximum number of attempts.
  // We need to do differnet things, depending which it is...
  if (WiFi.status() == WL_CONNECTED)
  {
    // We get here if we're connected to WiFi
    WiFi.mode(WIFI_STA); // Put the NodeMCU in Station Mode, so it doesn't broadcast its SSID
    Serial.println(F("Successfully connected to Wi-Fi"));
  }
  else
  {  
    // we get here if we tried multiple times, but can't connect to Wi-Fi. We need to go into standalone mode...
    Serial.println(F("Failed to connect to Wi-Fi"));    
  }
}


void Connect_To_Blynk()
{
//  if (WiFi.status() == WL_CONNECTED)// Commented-out for testing
  {
    // if we're connected to WiFi then try to connect to Blynk...
    Serial.println(F("Attempting to connect to Blynk..."));      
    Blynk.connect(blynk_timeout_seconds*1000);

    Serial.println(F("Blynk.connect() attempt ended"));                         // For testing 
    if(Blynk.connected())
    {
      Serial.println(F("Blynk connection attempt succeeded - connected"));       // For testing         
    }
    else
    {
      Serial.println(F("Blynk connection attempt failed - not connected"));     // For testing   
    }
  }
}


void Check_Connections() // Called with a timer 
{
  Serial.print("Checking connections.. ");
  
  if (WiFi.status() == WL_CONNECTED)
  {
    // We get here if we are connected to WiFi...
    Serial.print("WiFi Okay,  ");

    // now check if we're connected to Blynk...
    if(Blynk.connected())
    {
      Serial.println("Blynk Okay");      
    }  
    else
    {
      // We get here if we are connected to WiFi, but not to Blynk, so try to re-connect...
      Serial.println("Blynk not connected");        
      
      Connect_To_Blynk();        
    }
  }
  else
  {
    // We get here if we aren't connected to WiFi & Blynk, so try to re-connect...
    Serial.println("WiFi not connected");   
//    Connect_to_WiFi(); // Commented-out for testing
    Connect_To_Blynk();   
  }
}

As you can see, the reason is not “wrong code” but something else…

What version of the ESP32 core are you using?

Do you have an ESP8266 you can test it with?

Pete.

Using ESP object:
v3.3.5-1-g85c43024c

If I find 8266, I’ll check.

Test result, your code and esp8266.
Now I got the same 6 seconds as in your tests.

14:25:47.010 ->     ___  __          __
14:25:47.010 ->    / _ )/ /_ _____  / /__
14:25:47.010 ->   / _  / / // / _ \/  '_/
14:25:47.010 ->  /____/_/\_, /_//_/_/\_\
14:25:47.010 ->         /___/ v1.0.1 on ESP8266
14:25:47.010 -> 
14:25:47.010 -> Connecting to Wi-Fi...
14:25:47.010 -> Wi-Fi connection - attempt # 1 of 15
14:25:47.485 -> Wi-Fi connection - attempt # 2 of 15
14:25:48.000 -> Wi-Fi connection - attempt # 3 of 15
14:25:48.519 -> Wi-Fi connection - attempt # 4 of 15
14:25:48.984 -> Wi-Fi connection - attempt # 5 of 15
14:25:49.496 -> Wi-Fi connection - attempt # 6 of 15
14:25:50.719 -> Wi-Fi connection - attempt # 7 of 15
14:25:51.234 -> Wi-Fi connection - attempt # 8 of 15
14:25:51.702 -> Successfully connected to Wi-Fi
14:25:51.750 -> Attempting to connect to Blynk...
14:25:51.750 -> [4794] Connecting to blynk.cloud:80
14:25:51.842 -> [4936] Ready (ping: 65ms).
14:25:51.936 -> Blynk.connect() attempt ended
14:25:51.936 -> Blynk connection attempt succeeded - connected
14:26:21.720 -> Checking connections.. WiFi Okay,  Blynk Okay
14:26:51.729 -> Checking connections.. WiFi Okay,  Blynk Okay
14:27:21.718 -> Checking connections.. WiFi Okay,  Blynk Okay
14:27:51.734 -> Checking connections.. WiFi Okay,  Blynk Okay
14:28:21.734 -> Checking connections.. WiFi Okay,  Blynk Okay
14:28:25.011 -> [158065] Heartbeat timeout
14:28:51.702 -> Checking connections.. WiFi Okay,  Blynk not connected
14:28:51.748 -> Attempting to connect to Blynk...
14:28:51.748 -> [184794] Connecting to blynk.cloud:80
14:28:57.955 -> Blynk.connect() attempt ended
14:28:57.955 -> Blynk connection attempt failed - not connected
14:29:21.739 -> Checking connections.. WiFi Okay,  Blynk not connected
14:29:21.739 -> Attempting to connect to Blynk...
14:29:21.739 -> [214794] Connecting to blynk.cloud:80
14:29:27.986 -> Blynk.connect() attempt ended
14:29:27.986 -> Blynk connection attempt failed - not connected
14:29:51.733 -> Checking connections.. WiFi Okay,  Blynk not connected
14:29:51.733 -> Attempting to connect to Blynk...
14:29:51.733 -> [244794] Connecting to blynk.cloud:80
14:29:57.991 -> Blynk.connect() attempt ended
14:29:57.991 -> Blynk connection attempt failed - not connected
14:30:21.717 -> Checking connections.. WiFi Okay,  Blynk not connected
14:30:21.717 -> Attempting to connect to Blynk...
14:30:21.717 -> [274794] Connecting to blynk.cloud:80
14:30:27.989 -> Blynk.connect() attempt ended
14:30:27.989 -> Blynk connection attempt failed - not connected

The time - 6 seconds - instead of the advertised 5, seems strange. Where does one more second come from? For a controller, 1 second is a huge amount of time…

Not sure what that means.
My tests were done with ESP32 core version 2.0.5

It’s also worth checking which versions of WiFi.h is being used when the sketch is compiled, and ensuring that it’s v2.0.0 one from the ESP32 Core 2.0.5 folder…

Multiple libraries were found for “WiFi.h”
Used: C:\Users\Pete Knight\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5\libraries\WiFi
Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
Not used: C:\Users\Pete Knight\Documents\Arduino\libraries\WiFiNINA
Using library WiFi at version 2.0.0 in folder: C:\Users\Pete Knight\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5\libraries\WiFi

Pete.

1 Like

I misunderstood your question.
This is what esp32 returns when called ESP.getSdkVersion()

The esp version in the board manager is 1.0.6, the newer version is not in the list.
Maybe I need to update the IDE, what version do you have?

You are probably using an old URL in the Preferences > Additional Boards Manager URLs setting.
It should be the one shown in the Stable release link of this guide…

https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html

Once you’ve done that you should see a new list of release versions in the Boards Manager.

Pete.

1 Like

Yes, that was the reason.
Now the function takes the same 6 seconds as in your tests.

15:51:20.215 -> Checking connections.. WiFi Okay,  Blynk Okay
15:51:50.194 -> Checking connections.. WiFi Okay,  Blynk Okay
15:51:53.832 -> [155419] Heartbeat timeout
15:52:20.187 -> Checking connections.. WiFi Okay,  Blynk not connected
15:52:20.187 -> Attempting to connect to Blynk...
15:52:20.234 -> [181812] Connecting to blynk.cloud:80
15:52:26.463 -> Blynk.connect() attempt ended
15:52:26.463 -> Blynk connection attempt failed - not connected
15:52:50.224 -> Checking connections.. WiFi Okay,  Blynk not connected
15:52:50.224 -> Attempting to connect to Blynk...
15:52:50.224 -> [211812] Connecting to blynk.cloud:80
15:52:56.454 -> Blynk.connect() attempt ended
15:52:56.454 -> Blynk connection attempt failed - not connected

Thank you very much for your time and help in solving the problem! :pray:

If it’s not difficult, could you say why the time is not 5 seconds, but 6, does it really take so long to exchange data via wifi? …
This question is no longer so important, I’m just asking out of curiosity and in order to understand the ongoing processes a little more…

Thanks again!

Glad it’s working!

I’m not sure. I know it’s always fairly approximate, and I guess you can reduce the number slightly to get the result you need, but obviously if you go too low then it won’t connect to Blynk before it times-out.

Pete.

1 Like

Where are these lines formed? I don’t see them in the code… :thinking:

They come from the Blynk library.

Pete.

1 Like

Hello Pete!
In this topic, you helped a lot with a problem with Blynk.connect(), when my timeout did not match the one specified in the function.
Then the problem was with the ESP32 kernel version.
It’s incredible, but this problem came back again, although I didn’t change anything - the code is the same, the controller is the same.
The kernel version is now 2.0.5. The same as it was when everything worked correctly.
I updated the version to the latest - 2.0.14. Nothing changed.
Let me remind you of the essence of the problem: when I lose the Internet (but there is Wi-Fi), I call Blynk.connect(5000) and it blocks the controller for about 30 seconds, although it should be 5 - 6 seconds.
Please tell me what else could be the problem…

void setup()
{
timer.setInterval(60 * 1000, BlynkConFunction); 
}

void loop()
{
    timer.run(); 
    if  (Blynk.connected())  
    {
        Blynk.run();
     }
}

void BlynkConFunction() 
{
      if (WiFi.status() == WL_CONNECTED)  {
  
           if (!Blynk.connected()) {
             
	       Blynk.config(auth); 
               Blynk.connect(5000); 
           }
       }
}

A new topic has been created about this issue, so to keep the conversation in one location I’m closing this topic.

The new topic is here…

Pete.