Blynk.run() and Blynk.connected()

I’m using an Adafruit HUZZAH32 Feather card and Visual Code, PlatformIO with platform = espressif32 and framework = arduino. lib_deps = blynkkk/Blynk@^1.1.0. App on an iPhone 11 running iOS 16.3.

I’ve detected a situation where Blynk.run() returns true, and the WiFi is still connected, but HW is not connected to blynk.cloud:80. Blynk.connected() returns false although. My question is whether Blynk.run() should return true when blynk.cloud is not connected? This is a easy fix as long as you can call Blynk.connected(), but still.

I don’t understand this comment.
is this a typo, did you really mean Blynk.run() ?

If it wasn’t an error then can you explain more about this?

Pete.

From the documentation:
Connection Management
Blynk.run()
This function should be called frequently to process incoming commands and perform housekeeping of Blynk connection.

“Housekeeping” is a very generic term, but I would assume that part of it is to maintain the connection with blynk.cloud. Maintaining a heartbeat which feeds a watchdog may be among this. If this connection is broken, I would define this as a condition where the “housekeeper” should give an alert by Blynk.run() returning false. For me this is about the integrity of Blynk.run(). Blynk.run() returning true should indicate that the connection with blynk.cloud is ok. This is not always the case.

I understand what Blynk.run() does, its just that I’ve never seen a situation where anyone uses the returned value for anything.

A quick look in BlynkAPI.h revbeals this:

/**
    * Performs Blynk-related housekeeping
    * and processes incoming commands
    *
    * @param available  True if there is incoming data to process
    *                   Only used when user manages connection manually.
    */
   bool run(bool available = false);

So it would seem that Blynk.run() returns true when there is incoming data from the server which needs to be processed.

I can see why you’d say that it doesn’t make sense for Blynk.run() to return true when there is no connection to the server, but I guess that the returned parameter is only really used internally within the library, and was never really intended as a publicly useable value. I’ve searched the forum and can only fine two examples of code that uses “= Blynk.run()”
One was a person asking whether in a future release a parameter could be returned to indicate when the device has finished sending data to the server.
The other appears to be someone who was new to Blynk coding and didn’t understand how to use Blynk.run() should be used.

So, this situation where Blynk.run() returns true even where there is no server connection is probably an oversight (bug if you like) in the library, but something that has never been picked-up before because nobody ever uses this functionality.

If you think about it, the returned value has no really functionality anyway, as far as we are concerned. If Blynk.run() flags-up that additional internal processing, such as triggering a BLYNK_WRITE() function is required then this is done internally by the library. No action is needed by us when this occurs.

I’m not really sure what prompted you to use the returned value from Blynk.run(), but maybe if you want back to basics and explained what it is you are trying to achieve then we would be able to point you towards a better solution.

Pete.

Thank you for your good service @PeteKnight ! You seem to be available for the community 24/7. I appreciate that. The reason for trying to use the return value from Blynk.run() was pure curiosity. I just looked at the declaration of Blynk.run() and saw it was declared as bool. Then I looked at the code within BlynkProtocol.h and found that it actually checked for a connection.

bool BlynkProtocol<Transp>::run(bool avail)
{
    BLYNK_RUN_YIELD();

    if (state == DISCONNECTED) {
        return false;
    }

Not just that, but it even does a reconnection if not already connected. And, // Update connection status after running commands as it is written in a comment within the run() code.

Then I compared with the Blynk.connected():

bool connected() const { return state == CONNECTED; }

So, that’s why I tried to use the Blynk.run() return value. But now I know I can not rely on this return value without really knowing why not? I’m now using as Blynk.connected() to check if there is a need for a reconnect calling Blynk.connect(). But still, Blynk.run() could have done all this as part of the “housekeeping”.

My suggestion is to change the code from bool Blynk.run() to void Blynk.run().