BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Does if(Blynk.connected()) cause a signifiant delay in a loop function?

I am a noob programmer. I found the magic of programming only in the third year of university when I discovered Arduino. Ever since I learn from examples, so I don’t have a deep understanding of programming.

I am trying to understand how these things are working…

What is exactly happening when checking:

if(Blynk.connected()){
}

Does it run a function that returns a boolean or it just checks the result of that function that runs only limited times in a period of time? Where I can find that function?

If that function is actually running every single time, then how does it check the connection? does it ping the server?

what about:

if (WiFi.status() != WL_CONNECTED)

Thank you!

The C++ Blynk library is talking to the server all the time. This includes pings to check that the server is still visible to the device.
The Blynk.connected internal function call returns a Boolean that indicates if - at the point when it is called - the device is connected to the server.

The WIFi.status function call does a very similar thing, it checks if the Wi-Fi library is connected or not, but as you can see, the value returned is not a Boolean.
The WiFi library isn’t unique to Blynk, it’s well documented if you google it.

Pete.

Your relative answer somehow made me think about the heartbeat setting used in some of my projects, without actually understanding it.

So the connection is monitored using a heartbeat mechanism. It is checking every 10 seconds by default + another 5 seconds if it does not get the answer (the documentation says). But all this is happening with every Blynk.run() ? or every Blynk.command?

I think I managed to measure how long does it takes for the commands specified on my previous message to run:

The following command takes about 1 microsecond or less:

if(Blynk.connected()){
}

The following commandit takes about 55 microseconds:

if (WiFi.status() != WL_CONNECTED)

I used the following function to measure it. Should it be accurate?

void measureThis()
{
unsigned int time = 0;
time = micros();

if (WiFi.status() != WL_CONNECTED){
}
else{
}

time = micros() - time;
Serial.println(time);
}

This tells me that the result from the following

if(Blynk.connected()){

is already stored somewhere and it does not actually check the connection every time when it is running.

So, there should not be any problem in using that in the loop, but can the status can be outdated?

Well, Heisenberg’s uncertainty principle states that the act of measuring a system has an effect on that system. Your measureThis function obviously takes time to execute and the Serial.print functions are fairly slow, so your results are a worst-case timing scenario.

I guess so, but why do you ask?

If you want to know more about how the Blynk code works then you can take a look through the libraries and find the relevant code.

Pete.

I realized that the connection status is not verified at the moment of checking and I was wondering what is happening when the mcu tries to send data when it is offline but it does not know that yet. Is there any feedback mechanism for this situation?

I don’t know, why don’t you try it?

Pete.

Blynk.run() is part of the library that does the housekeeping, probably including sending out a heartbeat as frequently as abled, the Server does all the time checking of said heartbeat and initiates the disconnect if needed.

It gets lost in the tubes…never to be seen again…

Only if you program somthing in… but then it risks a cyclic loop, kind of like that irritating Windows message, “Are you sure you got it? Are you really sure? Are you really really sure?”

3 Likes