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…