Why not call Blynk.run(); inside an ISR

I am planning to use blynk on an arduino mega 2560 with a w5500 Ethernet shield to collect some sensor data and send it to a remote dashboard on Blynk server. after looking at all the example codes from the internet and documentations i found that the recommended way of using blynk is to use the timer function built in to do some timely tasks and keep the void loop clean with only blynk.run and timer.run.
but i don’t understand why don’t we write all our code inside the void loop and periodically call blynk write and blynk run functions inside an ISR. here is a representation of what I’m trying to do.

void setup()
{
 //setup
}

void loop()
{
//set a timer isr such that every 250ms there is a timer interrupt

//user code
//update all the sensor data to an array temp_variable[n];
}

ISR timer_interrupt()
{
 Blynk.virtualwrite(temp_variable[0];
 Blynk.virtualwrite(temp_variable[1];
.
.
 Blynk.virtualwrite(temp_variable[n];
 Blynk.run();
}

please tell me if something wrong in this approach.
also, I have tried doing this but found out the blynk is somehow messing with my timers and the code is not working as expected.

details:
• Hardware model + communication type: Arduino mega 2560 with w5500 Ethernet Shield
• Blynk server
• Blynk Library version: V1.2.0

@sachinmc Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Well, first of all doing things like Blynk.virtualWrite inside an ISR breaks all of the ISR rules.
ISRs should be as short and snappy as possible, and you should avoid things like serial print commands.

In your example you still need a timer to take your temperature readings, as this isn’t being done inside your ISR, so a better structure would be to take your temperature readings then write the values to Blynk.

Blynk.run needs to be in the void loop for a number of reasons…

  1. Each time Blynk.run is executed it checks to see if the server has sent new data to the device. This can be things like a button push, or other datastream change. Having this test execut d hundreds of times per second helps with the latency of these widget/datastream changes.

  2. When Blynk.run is execute it also ch cos to see if it’s time to send another heartbeat message to the Blynk server to let the server know that the device is still alive. If these heartbeats are missed then the Blynk server will close the Always On connection to the device, and mark it as offline.

Is there any reason why you have a problem with the normal BlynkTimer approach?

Pete.

got it. but I have some questions.

  1. how much time does a Blynk.virtualwrite takes. if this time varies, what does it depend on
    2.what does the Blynk.run actually do, how much time does it takes, what is the time limitation between two successive call of Blynk.run,
    3.does any of these Blynk functions hang the code because of any reason if yes how to avoid it.
  2. does Blynk uses any hardware timers or IRS, so that I should avoid using them for any other purpose in my code.

CPU speed, free memory, WiFi speed, internet latency, etc etc.

I’ve already explained the important bits.

It depends on what you’re trying to achieve, and what heartbeat settings you are using.

No, not as far as I know.

Do you mean ISRs? If so then the Blynk libraries don’t but some of the example sketches do.

Pete.

Hi, thank you for your inputs.
I continued coding as per your inputs but now I’m facing a problem.

void setup()
{
  Serial.begin(115200);
 
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Blynk.begin(BLYNK_AUTH_TOKEN);
  
  timer.setInterval(1000L, _1secTimerEvent);
  timer.setTimeout(33, [](){
  timer.setInterval(500L, _500msTimerEvent);
  });
  timer.setTimeout(33, [](){
  timer.setInterval(100L, _100msTimerEvent);
  });    
}

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

//--------------------Blynk timers----------------------------
void _500msTimerEvent()
{
  //display refresh_________
 
  //________________________
}

void _1secTimerEvent()
{
  //push status to blynk
  Serial.print("Blnk=");
  Serial.println(Blynk.connected());
  Blynk.virtualWrite(V0,status_name[status]);
}

void _100msTimerEvent()
{
   //automation stages
   if(status_counter++ == 10)
   {
    status_counter=0;
    status++;
    if(status > 5) status=0;
   }
}
//----------------------------------------------------------

this is a cutdown version of my code with sufficient information.
what im trying is to run some automation application inside the blynk timer functions which are set for 1sec,500ms,100ms time
and I want this automation to run unaffected even if blynk disconnects.
but what I observed is when Blynk got disconnected and it will keep on trying to reconnect, during this time the timer functions are not triggered at right intervals and this is messing up my automation.
i think blynk is hanging the code flow when it is trying to reconnect to the servers. please help with this.

In that case you shouldn’t be doing this…

because Blynk.begin is a blocking function.

Instead you should be managing your own internet connection, then using Blynk.config() and Blynk.connect()

You should also be putting Blynk.run() inside an if Blynk.connected() logic test and potentially declaring an optional timeout period in the Blynk.connect() command and using a timer to occasionally test if Blynk.connected() is false and attempting a re-connection.

Pete.

can you please elaborate a bit more on your workaround?

If you search the forum for the keywords I used you will find lots of info.

Pete.

are these two functions non-blocking? what do you think the maximum execution time of a non-blocking function. do you think it is possible to reestablish the connection with a function which runs for say less than 10ms, but I will be calling it in the loop which is obviously running verry fast.

If you do some searching you’ll know that Blynk.connect(timeout) blocks until the timeout period ends, which is around 18 seconds by default.

Absolutely not.

No you won’t, you’ll be…

Pete.