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

Location of blynk.run;


#1

• 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).


#2

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)
  {
    Blynk.run();
    timer.run();
    disconnects = 0;  
  }
  else    // try 2 times to reconnect and reboot ESP8266 if it fails
  {
    ++disconnects;
    if(disconnects > 2)
    {
      Serial.println(F("ESP8266 is rebooting"));
      delay(3000);
      ESP.reset();
      delay(5000);    
    }
  }
}

#3

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?


#4

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.


#5

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


#6

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


#7

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?


#8

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).


#9

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?


#10

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


#11

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

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

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

Right?


#12

Not quite.

timer.setInterval(8000L, keepblynkrunoutofloopfunction);

void keepblynkrunoutofloopfunction()
{
   Blynk.run();
   // anything else you want to put here
}

#13

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


#14

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


#15

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?


#16

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


#17

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?


#18

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.


#19

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?


#20

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.