BLYNK.connect() timeout event syntax

Unfortunately, it looks like there really is a bug in the Blynk.connect(timeout ) function.
Moreover, the function does not work only if you specify a timeout, if you do not specify it, then everything is fine!
I timeout Blynk.connect(29) and every few seconds the connection status in the application changes from online to offline, while Blynk.connected() never becomes true.
If you set Blynk.connect() then everything works fine.
Here is my code:

#define BLYNK_TEMPLATE_ID		"xxxxxxx"
#define BLYNK_DEVICE_NAME		"xxxxxxx"
#define BLYNK_AUTH_TOKEN 		"xxxxxxx"
#define BLYNK_FIRMWARE_VERSION	"0.0.1" 
// #define BLYNK_PRINT Serial
#include <WiFi.h>  
#include <WiFiClient.h>  
#include <BlynkSimpleEsp32.h>
#include <Update.h>  
#include <HTTPClient.h>  
BlynkTimer timer; 
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";
bool StartDelay_148632894 = false;
bool WiFiok = false; 
bool BlynkCon = false; 
unsigned long ConnectTime_148632894;
unsigned long prevMil; 
bool Out_238044322_220347088;
bool Out_128069072_220347088;
bool Out_101447533_220347088;
String overTheAirURL = "";
BLYNK_WRITE(InternalPinOTA) 
{
    	overTheAirURL = param.asString();
    	HTTPClient http;
    	Blynk.disconnect();
    	http.begin(overTheAirURL);
    	int httpCode = http.GET();
    	if (httpCode != HTTP_CODE_OK) 
    {
        		return;
        	
    }
    	int contentLength = http.getSize();
    	if (contentLength <= 0) 
    {
        		return;
        	
    }
    	bool canBegin = Update.begin(contentLength);
    	if (!canBegin) 
    {
        		return;
        	
    }
    	Client& client = http.getStream();
    	int written = Update.writeStream(client);
    	if (written != contentLength) 
    {
        		return;
        	
    }
    	if (!Update.end()) 
    {
        		return;
        	
    }
    	if (!Update.isFinished()) 
    {
        		return;
        	
    }
    	reboot();
}
void setup()
{
   
    WiFi.begin(ssid, pass);
    Blynk.config(auth);
    timer.setInterval(35 * 1000, BlynkConFunction); 
}
void loop()
{
    if (WiFi.status() == WL_CONNECTED)  
    {
        WiFiok = true;
        Out_101447533_220347088 = true;
    }
    else  
    {
        WiFiok = false;
        Out_101447533_220347088 = false;
    }
    timer.run(); 
    if (WiFiok && Blynk.connected())  
    {
        Blynk.run();
        BlynkCon = true;
        Out_238044322_220347088 = true; 
            StartDelay_148632894  = _isTimer(ConnectTime_148632894, 1000);
        Out_128069072_220347088 = StartDelay_148632894;
    }
    else  
    {
        BlynkCon = false;
        Out_238044322_220347088 = false;
        StartDelay_148632894 = false;
        Out_128069072_220347088 = false;
    }
   
}
bool _isTimer(unsigned long startTime, unsigned long period)
{
    unsigned long currentTime;
    currentTime = millis();
    if (currentTime>= startTime) 
    {
        return (currentTime>=(startTime + period));
    }
     else 
    {
        return (currentTime >=(4294967295-startTime+period));
    }
}
void reboot()
{
    #if defined(ARDUINO_ARCH_MEGAAVR)
    	wdt_enable(WDT_PERIOD_8CLK_gc);
    #elif defined(__AVR__)
    	wdt_enable(WDTO_15MS);
    #elif defined(__arm__)
    	NVIC_SystemReset();
    #elif defined(ESP8266) || defined(ESP32)
    	ESP.restart();
    #else
    	#error "MCU reset procedure not implemented"
    #endif
    	for (; ;) 
    {
    }
}
BLYNK_CONNECTED()
{
    	ConnectTime_148632894 = millis();
    	
    	Blynk.syncAll();
    	
}
void BlynkConFunction()  
{
    if (WiFiok && !BlynkCon)
    {
        Blynk.connect(29);
    }
}

I think you’re mis-understanding the purpose of the timeout in the Blynk.connect(timeout) function and getting it mixed-up with the functionality of Blynk.connected()`

The timeout simply specifies approximately how long the device will spend trying to connect to Blynk before it gives-up and allows the next line of code to be processed.

It has nothing to do with how long the device will wait from the last successful ping of the Blynk server before the device realises that it is offline.
As I explained in my last post, during this period Blynk.connected() will return true even if the connection was severed immediately after the last ping.

If you look at the Device Info screen in the web console you’ll see the Heartbeat Interval, which seems to default to 45 seconds. From memory, I think it’s this that dictates how long it takes before Blynk.connected() returns false

I did some experiments a while ago, and made some notes, and I’ve just been looking back to try to find these.

One sketch I had contained this piece of code…

int blynk_timeout_seconds = 1; // Timeout value for the Blynk.connect(timeout) command. The library default is 18 seconds

void Connect_To_Blynk()
{
  if (WiFi.status() == WL_CONNECTED)
  {
    // 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);

which seems to imply that the timeout period in the Blynk.connect(timeout)` command is in milliseconds, not seconds.
This was a piece of test code that may or may not have worked as expected, and I don’t have any hardware available where I am at the moment to test this, but it might e worth investigating.

Maybe you should try a simple sketch with some serial print commands and turn-on the timestamp in the serial monitor to see what sort of results you get when attempting to connect to Blynk without a WiFi connection.

Pete.

2 Likes

I understand this very well.

I’ve been doing this for many hours now, the Blynk.connect(timeout) function works if you don’t specify a timeout.
If you specify a timeout (in seconds), then the controller constantly connects and disconnects from the server, with an interval of several seconds, and so on ad infinitum.
It’s impossible to get Blynk to work at all, if you specify a timeout in the function (any value). If you do not specify a timeout, then everything works fine!

Now that you have given an example of the fact that the timeout may be indicated not by seconds, but by milliseconds, I will check this version. I proceeded from the fact that the time is in seconds, because I read about it here on the forum, but the documentation does not say anything about it at all …
I will definitely check and post the result.
Thank you.

P.S.
By the way, regarding your notes that the default value in the library is 18 sec, my measurements have a default value of about 20 sec. It’s clearly not 30 as stated in the documentation.

I wrote the simplest possible code.
And after doing some simple testing, I’m convinced that the Blynk.connect(timeout) function contains a bug.

#define BLYNK_TEMPLATE_ID           "xxxxxx"
#define BLYNK_DEVICE_NAME           "xxxxxx"
#define BLYNK_AUTH_TOKEN            "xxxxxx"

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

BlynkTimer timer; // timer for Blynk Connection Function

bool WiFiok = false; // connection wifi
bool BlynkCon = false; // connection Blynk

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";

void setup()
{
  Serial.begin(9600);

  WiFi.begin(ssid, pass);
  Blynk.config(auth); 
  timer.setInterval(30 * 1000, BlynkConFunction);// connection to the server - period 35 sec
}


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



void BlynkConFunction() // Blynk Connection Function
{
    if (WiFi.status() == WL_CONNECTED)  {
      WiFiok = true; 
      Serial.println("Wifi_OK");
      if (!Blynk.connected()) {
          Serial.println("Connecting_to_Blynk...");
          BlynkCon = Blynk.connect(25); // works only if no timeout is specified!
          if (BlynkCon) {
            Serial.println("Blynk_connected");
          }
          else {
            Serial.println("Blynk_FAIL");
            }
      }
      else {
          Serial.println("Blynk_OK");
      }
  }
    else  {
      WiFiok = false;
      BlynkCon = false;
      Serial.println("Wifi_FAIL");
    }
}

I made three tests:
1 - without specifying a timeout in the function - everything works fine, the timeout is about 18 seconds;
2 - with timeout (25) - nothing works.
3 - indicating the timeout in ms. (3000) - works if there is internet. If you turn off the Internet, then the real timeout is 18 seconds, not 3 seconds, as configured!
All this is clearly visible in the screenshots that I give.



if (Blynk.connect(4000)) { //4s seconds for timeout 
  if (Blynk.connected()) {
    client.flush();
    R_DEBUG("Blynk connected OK");
    return true;
  }
  else {
    R_DEBUG("ERROR.Blynk login");
    return false;
  }
}
else {
  R_DEBUG("ERROR.Blynk cant authenticate");
  return false;
}

This is working for me since several months.

I think the bug is actually in your code, and the way that you’re using flags instead of simply doing the WiFi and Blynk connected tests each time they are needed.

You’ll see three places where there is a comment that says “Commented-out for testing”.
These if statements that are commented-out would normally prevent the sketch from attempting to connect to Blynk is there is no WiFi connection, but to prove that the Blynk.connect(timeout) functionality works correctly, we need toa allow the sketch to go ahead and attempt the connection even when the WiFi connection has been turned-off

#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

#define BLYNK_PRINT Serial

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

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

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();   
  }
}

The connection attempt times-out after 5 seconds if we boot-up with the WiFi turned off…

18:07:00.334 -> Wi-Fi connection - attempt # 14 of 15
18:07:00.814 -> Failed to connect to Wi-Fi
18:07:00.814 -> Attempting to connect to Blynk...
18:07:00.814 -> [24600] Connecting to blynk.cloud:80
18:07:05.835 -> Blynk.connect() attempt ended                    << Times-out after 5 seconds
18:07:05.835 -> Blynk connection attempt failed - not connected

If I boot-up with WiFi turned on, then disconnect it after around 45 seconds I get this…

18:11:08.107 -> Wi-Fi connection - attempt # 2 of 15
18:11:08.623 -> Wi-Fi connection - attempt # 3 of 15
18:11:09.140 -> Wi-Fi connection - attempt # 4 of 15
18:11:09.623 -> Wi-Fi connection - attempt # 5 of 15
18:11:10.141 -> Wi-Fi connection - attempt # 6 of 15
18:11:11.339 -> Wi-Fi connection - attempt # 7 of 15
18:11:11.855 -> Successfully connected to Wi-Fi
18:11:11.855 -> Attempting to connect to Blynk...
18:11:11.855 -> [4309] Connecting to blynk.cloud:80
18:11:11.890 -> [4368] Ready (ping: 13ms).
18:11:11.993 -> Blynk.connect() attempt ended
18:11:11.993 -> Blynk connection attempt succeeded - connected
18:11:41.847 -> Checking connections.. WiFi Okay,  Blynk Okay    << 1st test, WiFi is still on
18:12:11.869 -> Checking connections.. WiFi not connected        << 2nd test, WiFi is now off
18:12:11.869 -> Attempting to connect to Blynk...
18:12:11.869 -> [64309] Connecting to blynk.cloud:80
18:12:16.870 -> Blynk.connect() attempt ended                    << Times-out after 5 seconds
18:12:16.870 -> Blynk connection attempt failed - not connected
18:12:41.860 -> Checking connections.. WiFi not connected        << 3rd test, WiFi is still off
18:12:41.860 -> Attempting to connect to Blynk...
18:12:41.860 -> [94309] Connecting to blynk.cloud:80
18:12:46.846 -> Blynk.connect() attempt ended                    << Times-out after 5 seconds
18:12:46.846 -> Blynk connection attempt failed - not connected

Pete.

I’m sorry I haven’t been able to reply for a few days.

There is a big difference between the definitions of “no wifi connection” and “no internet”.
In my screenshots above, I indicated that I was turning off the Internet, and not turning off the wifi connection.
Yes, indeed, if you break the Wi-Fi connection, then the specified timeout - 5 seconds - is true.
But! If you leave the router turned on and the wifi is active, but you break the Internet connection, then the timeout will always be 18 seconds.
This is significant! It is important for me that when the Internet connection is broken, my controller does not freeze for a long time, but performs other important functions (except for Blynk).

That is why I say that the function Blynk.connect(timeout)contains a bug.

I’m not saying it doesn’t work. I’m saying that when the internet connection is broken, the actual timeout in the function is 18 seconds, not the time you specify.

Try to check first if DNS servers are correctly configured, before trying to connect Blynk. This will give you a signal of DHCP working correctly.
Maybe using Wifi.config before Wifi.begin in your program
https://www.arduino.cc/reference/en/libraries/wifi/wifi.config/

I have no problem connecting to the Blynk server.
I specifically break the Internet connection in order to check the real timeout in the Blynk.connected(timeout) function, and it does not match the set one.
Therefore, I don’t understand where the DNS and DHCP have to do with it …

I’ve done some more testing with the example sketch I posted above in post #31, this time leaving the wireless access point switched-on and pulling the LAN cable out of the back. This keeps the WiFi connection between the device and the WAP, but kills the internet connection.

It still works as expected.
I’ve added comments , which look like this: <<< my comment in here >>> to explain what is happening at each stage…

09:01:10.945 ->     ___  __          __
09:01:10.945 ->    / _ )/ /_ _____  / /__
09:01:10.945 ->   / _  / / // / _ \/  '_/
09:01:10.945 ->  /____/_/\_, /_//_/_/\_\
09:01:10.945 ->         /___/ v1.1.0 on ESP8266
09:01:10.945 -> 
09:01:10.945 ->  #StandWithUkraine    https://bit.ly/swua
09:01:10.945 -> 
09:01:10.945 -> 
09:01:10.945 -> Connecting to Wi-Fi...
09:01:10.945 -> Wi-Fi connection - attempt # 1 of 15
09:01:11.422 -> Wi-Fi connection - attempt # 2 of 15
09:01:11.941 -> Wi-Fi connection - attempt # 3 of 15
09:01:12.419 -> Wi-Fi connection - attempt # 4 of 15
09:01:12.931 -> Wi-Fi connection - attempt # 5 of 15
09:01:13.442 -> Wi-Fi connection - attempt # 6 of 15
09:01:14.640 -> Wi-Fi connection - attempt # 7 of 15
09:01:15.158 -> Successfully connected to Wi-Fi
09:01:15.158 -> Attempting to connect to Blynk...
09:01:15.158 -> [4312] Connecting to blynk.cloud:80
09:01:15.193 -> [4350] Ready (ping: 16ms).
09:01:15.262 -> Blynk.connect() attempt ended
09:01:15.262 -> Blynk connection attempt succeeded - connected
09:01:45.144 -> Checking connections.. WiFi Okay,  Blynk Okay
09:02:15.139 -> Checking connections.. WiFi Okay,  Blynk Okay
09:02:45.172 -> Checking connections.. WiFi Okay,  Blynk Okay

<<< LAN plug pulled at around 09:03:00 Blynk.connected() still returns true, because no heartbeat timeout yet... >>>

09:03:15.162 -> Checking connections.. WiFi Okay,  Blynk Okay
09:03:45.173 -> Checking connections.. WiFi Okay,  Blynk Okay

 <<< Heartbeat interval for this device is 45 seconds, heartbeat times-out around 48 seconds after the internet connection is cut... >>>

09:03:48.289 -> [157430] Heartbeat timeout

<< Now reports that WiFi is connected but Blynk is not - this is correct... >>>

09:04:15.154 -> Checking connections.. WiFi Okay,  Blynk not connected

<<< Because Blynk is not connected the sketch attempts to reconnect to Blynk. This fails, because no internet connection. Connection attempt times-out after 6.220 seconds... >>>

09:04:15.154 -> Attempting to connect to Blynk...
09:04:15.154 -> [184311] Connecting to blynk.cloud:80
09:04:21.383 -> Blynk.connect() attempt ended
09:04:21.383 -> Blynk connection attempt failed - not connected

<<< Attempts to connect to Blynk again, this time the timeout is 6.235 seconds... >>>

09:04:45.170 -> Checking connections.. WiFi Okay,  Blynk not connected
09:04:45.170 -> Attempting to connect to Blynk...
09:04:45.170 -> [214311] Connecting to blynk.cloud:80
09:04:51.405 -> Blynk.connect() attempt ended
09:04:51.405 -> Blynk connection attempt failed - not connected

<<< Attempts to connect to Blynk again, this time the timeout is 6.233 seconds... >>>

09:05:15.165 -> Checking connections.. WiFi Okay,  Blynk not connected
09:05:15.165 -> Attempting to connect to Blynk...
09:05:15.165 -> [244311] Connecting to blynk.cloud:80
09:05:21.398 -> Blynk.connect() attempt ended
09:05:21.398 -> Blynk connection attempt failed - not connected

The timeout periods are slightly longer than the 5 seconds specified in the Blynk.connect(timeout) command, but I think that’s to be expected.

Pete.

@Serg_Grn would you go to Blynk Console > Your Device > Device Info and tell me what it shows for Board Type and Heartbeat Interval ?

Pete.

BOARD TYPE: ESP32
HEARTBEAT INTERVAL: 45

I don’t understand why your code example works and mine doesn’t. The code is very simple.
And why if you break the connection to the Internet, the timeout from 5 seconds turns into 18 seconds - I don’t understand either …
Maybe there is a difference in the libraries for esp8266 and esp32?

I re-tested with an ESP32 and the results were the same.

I 've not sent a lot of time un-picking your code, but I don’t like the way that it uses flags instead of simply doing the tests of whether WiFi and Blynk are connected.

Does my code work for you and produce the same results?

Pete.

I can only check it tomorrow.

Is there any violation in periodically writing the status to a variable, and then using it in a loop? It seemed to me that this was a simple and convenient solution … And I did not find anything in the documentation that would contradict this.

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…