Location of blynk.run;

• Hardware model + communication type: ESP8266 with hardware serial connected to HC12 radio
• Smartphone OS Android version 7.0
• Blynk server
• Blynk Library version 0.5.4

I suspect ESP8266 exception 28 reboots (a few times per day) occur due to wifi connection interruption and blynk.run in the main loop going in a infinite loop until the connection is re-established.
Currently blynk.run is located in my main loop, but I only need to send data to the blynk server every 20 minutes. This is done in a separate function called for that purpose.
Would it be ok to locate the blynk.run instruction in that loop right before data is to be transmitted to the blynk servers?
Currently there is no need to receive commands from the smart phone app (yet).

No because you would miss the hearbeat of 20 or 30 seconds.
Try something like this as covered by Connection Management in the docs:

unsigned int disconnects = 0;

void loop()
  bool result = Blynk.connected();  // this will check if ESP8266 is connect to server, OK
  if(result == true)
    disconnects = 0;  
  else    // try 2 times to reconnect and reboot ESP8266 if it fails
    if(disconnects > 2)
      Serial.println(F("ESP8266 is rebooting"));

Hi thank you for that answer!
In the main loop I need to continually check for radio messages received over the hardware serial RX input. Will this blynk timer not interfere with this message checking?
Also: do I need to reboot the ESP8266 if the wifi connection is interrupted even for a few seconds?

Shouldn’t interfere with message checking.

I prefer to reboot my ESP if it fails to connect to the server but you can try a new WiFi connection etc.

If you really don’t want run in your loop() you could use BlynkTimer (aka Arduino SimpleTimer) at say 8 second intervals to call Blynk.run(). You would need timer.run() in the loop() etc but it will not interfere with message checking.

I get a compilation error: ‘timer’ was not declared in this scope.

Take a look at the PUSH DATA example for all the requirements needed to use BlynkTimer.

Great!! Thanks for that input!

Instead of rebooting the ESP8266, is it ok to add Blynk.connect(); after ++disconnects; in the else statement?

Also: what interval is timer.run() set in above example?

Yes you could try Blynk.connect().

I can’t remember if heartbeat timeout is 10, 20 or 30 seconds so I would use 8000L for interval (8s).

In the pushdata example, the timer interval is linked to a function to be executed: timer.setInterval(1000L, myTimerEvent);
How is the interval set in the above code (your example), when no separate function it to be executed?

You wanted to have Blynk.run() outside the loop() so that would be the separate function, understand?

ah ok! So I should write (in the setup):

timer.setInterval(8000L, blynk.run());

And leave blink.run(); right before timer.run();


Not quite.

timer.setInterval(8000L, keepblynkrunoutofloopfunction);

void keepblynkrunoutofloopfunction()
   // anything else you want to put here

And where should I call timer.run() ?
Should I use your example above (your first answer)? Without the Blynk.run() statement?

It stays in loop() and it just tells the MCU to run any timers at the required intervals.

In your first answer you included a check to verify Blynk connection to the Blynk server. Can I still use that loop, but without the Blynk.run statement because as I understand it now that is called by the timer.run() function?

You can check for server connection in loop() or in keepblynkrunoutofloopfunction()
Your choice.

Ok, but is it right to use the example you gave in your first answer above in order to verify server connection? I would like to use that because it is so neat.
And if so, I presume that Blynk.run() needs to be left out because it is being done in void keepblynkrunoutofloopfunction(): is that right?

Most people never move Blynk.run() from loop() as it’s a vital part of using Blynk. You should be able to use my code in loop() with the Blynk.run() elsewhere.

The issue I have is that my ESP8266 code should keep running without any reboot (because I store historical data for program use); any reboot requires a one hour restore time due to readings from remote sensors needing to be reloaded.
If I can avoid that by checking blynk server connections without needing to reboot, as well as performing these checks without the ESP8266 waiting beyond the watchdog timer expiring, than I would like to go that route.
And your initial example fulfills that need, with the understanding I have that Blynk.run() is delegated to the keepblynkrunoutofloopfunction()
So anytime timer.run() is called, Blynk.run() is called too; if the server is connected; if not, then 2 reconnect attempts are performeed without rebooting the ESP8266.
I hope this is correct reasoning?

timer.run() is a scheduler and will only perform a Blynk.run(), in your revised sketch, if the 8 second cycle has completed.
It only takes a fraction of a second for these functions to execute so I wouldn’t worry too much about them.
I can understand that you will not want to reboot the ESP in your circumstances.