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

Blynk.run() or Blynk.connect()?

#1

Just an explanation for a detail that I did not find an explanation for:

I saw that someone uses Blynk.run () in the Loop while others use Blynk.connect () in Setup.
In the documentation I did not find a clear explanation of the difference in practical terms of these two calls. All my sketches work properly without Blynk.connect () in the setup but with Blynk.run () in the Loop. Thanks to those who give me this info.

0 Likes

#2

Come on! how can it be? Blynk’s docs aren’t 100% complete, but regarding those basics everything is there:
http://docs.blynk.cc/#blynk-firmware-connection-management-blynkrun

http://docs.blynk.cc/#blynk-firmware-connection-management-blynkconnect

From myself I can add, that Blynk.connect() is the preferred (for me, at least) method for establishing connection with Blynk server.

1 Like

#3

do not understand me wrong … :slight_smile:
it’s all 100% written but I do not understand.

favorite so I think “alternative”?

I do not believe. From what I understand Blynk.connect() starts the connection to the server and returns the connection status (true if connected, false if disconnected). At this point Blynk.run() could also be not inserted in the loop? And viceversa? Does Blynk.run() even manage to start the connection to the server as well as manage all the processes between devices, servers and apps? It would seem so since I have never used Blynk.connect() but only Blynk.run().

0 Likes

#4

But I do believe you are using Blynk.begin(), which I’m not.

So we do have:

  • Blynk.config () - for setting the connection details,
  • Blynk.connect() - for establishing connection,
  • Blynk.connected() - for checking the connection to server
  • Blynk.run() - for managing Blynk “stuff”. If Blynk.connect WAS NOT called earlier, it tries to connect as well.
  • Blynk.begin() - blocking function which does the following: Establishes network connection, sets up Blynk connection details, and TRIES to connect to Blynk server… Till the end of the world, if it can’t reach the server.

Yes, instead of Blynk.begin()

0 Likes

#5

to add to the confusion. After lots of reading and testing I ended up with this core ‘connection’ setup:

BlynkTimer timer;

void reconnectBlynk() {
  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) {
      Serial.println("Reconnected");
    }
    else {
      Serial.println("Not reconnected");
    }
  }
}


void setup() {
  Blynk.begin(auth, ssid, password, server, port);
  while (Blynk.connect() == false) {  }   // Wait until connected
  timer.setInterval(60 * 1000, reconnectBlynk);  // check every minute if still connected to server
}

void loop() {
  timer.run(); 
  if(Blynk.connected()) { Blynk.run(); }
}

note is completely borrowed from several contributors on this forum!

0 Likes

#6

@Franchelli re: Blynk.run() or Blynk.connect()?

You are not comparing apples to apples here…

Blynk.run() is an essential housekeeping command that keeps the background library “running”.

Blynk.connect() is an optional command that, used in conjunction with other connection management commands, can allow purposeful disconnection and reconnection to the server, along with semi or automatic reconnection routines to handle outside network interruptions.

0 Likes

#7

Thanks Gunner (and to everyone else) as usual extremely exhaustive.
The difference that you have clearly illustrated is not shown clearly in the documentation and it would be a good idea to add these few lines of explanation.

0 Likes

#8

Correct. The documentation is 50% terrible and should simply refer everyone to this forum.

0 Likes

#9

@vshymanskyy FYI.

0 Likes

#10

So what is the consensus of the question?
Have been trying to find out if calling Blynk.run() is sufficient to handle server disconnect?
Does Blynk.run() also connect/reconnect to the server or is it only managing housekeeping? I have not been able to verify one or the other and the documentation is not clear on this point.

Blynk.run() - for managing Blynk “stuff”. If Blynk.connect WAS NOT called earlier, it tries to connect as well.

So if I use Blynk.begin(auth, ssid, pass); in my setup, would Blynk.run() be enough in a reconnect function or should I use Blynk.connect():?

Blynk.run() is an essential housekeeping command that keeps the background library “running”.

0 Likes

#11

Why reopen an old topic for this again, it was also recently referenced in another current topic… Does one need to know the formula for gasoline to know that you need a constant flow of it to run a car?

We need to read ALL the documentation, often repeatedly and even after many years of using Blynk :wink: As well as compare differing situations and commands to understand the hows, whys and whens of many commands.

This leads to learning experiential information that may be beyond what is in the Docs… Good Documentation takes time and Blynk is constantly in development, so probably not a lot of time for both, thus hands on learning is a necessity.

Blynk.run() must be called as frequently as possible as it is the heart of the background Blynk process in your sketch.

That is basically it.

But, yes, it will try to connect if the server is not already connected. This is mentioned in the docs in reference to Blynk.config(). But there are many other situations in which you would need to use a different command like Blynk.connect() as you may purposely NOT have Blynk.run() active as you may be disconnected while still wanting running code - C++ Blynk - Code Examples for Basic Tasks (Work in Progress)

If you need further info, please create a new topic with details of your specific code and/or needs of assistance in your project. Thank you.

1 Like

closed #12
0 Likes