Avoid the Void (or how I learned to love the timer)


I’m fairly new to Blynk so, please bear with me.

I have read in various places that I should avoid placing other things in the Loop Void,
Is this a hard and fast rule? I have lots of other code that I need to execute before Blynk does it’s job.
Is there a way around this at all?
I’ll be using an Arduino or ESP8266 by the way.

Thank you.

As a general rule it is best to keep the loop() as clear as possible. Usually only including the BLYNK and TIMER calls. I have seen and heard of others putting things in the loop(), besides those two things, and getting things to work. Personally I do not, and haven’t run into a situation where it was necessary (yet). To keep the loop() clear the use of SimpleTimer becomes necessary. I also suspect that with SimpleTimer, and the use of the BLYNK Configuration, and Connection Management commands you could accomplish what you are after. That is, execute some code before connecting to BLYNK.

1 Like

Not for advanced users.
The main thing you want to avoid is sending thousands of Blynk.virtualWrite() per second (the cloud limit is 100 per sec from memory).
So you can happily run loops that involve no virtual writes (whether directly triggered or triggered by interrupt).
You can also use virtual writes in the loop as long as you code a flag to send it once, then reset aftet X seconds.

The only hard and fast rule is:
Avoid looping blynk commands and you will be fine.

The purpose of keeping the void loop() relatively clear is the same reason for also keeping any other function loop free of delays and/or endless looping… because Blynk needs to be constantly “kept in the loop” so to speak via the Blynk.run() command.

Aside from the initial setup and the alternate scenario where Blynk.config() is used instead of Blynk.begin(), in said setup… Blynk is always running in a background subloop, called upon by the Blynk.run() command. That command is always needed in the main void loop() because that loop itself is part of the Arduino’s (and ESP’s) core loop. So really, Blynk is ALWAYS needing to do it’s job… interrupt it for more than a few seconds, communication breaks down and things stop working.

In traditional Arduino code, all the primary work was done in the void loop(), so that is where all your code went with the occasional foray outside to another loop that could technically run as long as it needed to without real issue.

However when the ESP8266 came along, it added another background loop to the whole scenario, the WiFi communications loop. So between Blynk and ESP, we now have the almost polar opposite rule of of keeping the void loop() clear. And instead running everything possible in separately timed loops, called upon in the main void loop() via timer.run() commands.

Well… at least that is how I see it :stuck_out_tongue_winking_eye:


I’m having fun with a project that is reasonably complex, a battery powered bare ATmega328p that is sleeping most of the time, and periodically wakes up and powers on (by a MOSFET) a circuitry with a Wemos and a couple of sensors.
The Wemos collects the data and sends them to a local Blynk server. The Android app even has an interactive menu for configuration and calibrations, all is done in Blynk.
After data collection, Wemos goes sleeping and Atmega powers off all the circuitry, before going to sleep again.

All communication is managed by virtual variables, no Blynk.run() loop, as Wemos is powered off at every cycle, so the program “lives” in the start() routine, and “void loop()” is never executed.

So I can confirm that Blynk.run()-in-the-void-loop() is not a dogma ( although it is the best way to learn), and some kinds of applications, even complex ones, can run fine without keeping a continuous connection with the server (at the price of some tradeoff, of course).

Not relying on such “central loop schema” may require some try-and-miss, as I couldn’t find much information about this unorthodox usage style, but hey, this is just part of the fun!

1 Like

Thanks for the info guys, much appreciated.

I think I get the gist of what you are saying.

Would it be ok if I enclosed the code I needed to run in it’s own void and then called that void from the timer?


Yes, that is one of the usual ways to run a timer function. Here is a very abbreviated example of a timed function loop

void setup()
timer.setInterval(60000L, sendUptime);  // Up-Time timer 
}  // END Setup loop

void sendUptime()  // Up-time loop
  upTime = millis() / 60000;
  Blynk.virtualWrite(V6, upTime);  // Send minutes Up-Time to display.
}  // END UpTime loop

void loop()
  timer.run();  // Check timer
}  // END Void loop
1 Like

That’s great.

I’ll give it a try when i’m back in the office tomorrow.

Thanks again everyone.

1 Like