Manage delay in the Loop


I have a project running (no yet in Cayenne). It takes values from a electrical meter by modbus RTU communication via serial.
The issue is that this modbus communication requires each measurement has to be every 3 seconds, so I have a delay function in my loop with this time.

Is this a problem to work with Cayenne? How can a send this data to my board every 3 seconds? Any example please.

Thank you very much.

Hello Sir,
It is not problem for Cayenne. You can send the data with the delay inside the sending code fragment.
This is for example Arduino Code:
Cayenne.virtualWrite(V1, newValue);

Thanks, I understand.

I suposse I also could use delay funtions in the main loop. With the code.

I will try it.

Yes, you can. I also use delays. Please let us know when you test.

It works without problems.


Glad it works.

I prefer this:

#include "Timer.h"

Timer t;

void setup()
  //after cayenne begins
  //call the modbus function every 3 seconds
  t.every(3000, readMobus);
void loop()
  //let the update process run
void readModbus()
   int modResult;

   //get the modbus data in modResult

   //give it to Cayenne
  Cayenne.virtualWrite(V1, modResult);

The reason is, is that if you put a delay in your function, then nothing else in the system is happening while you wait for that delay to complete. Using the timer library, you get the same result.



1 Like