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