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.
#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…
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…
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
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…
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!
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…
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.
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…