Multiple handle() calls

I’m a real beginner. I’ve programmed my DraginoYun/Mega 2560 with a few for, while and case loops. Within each of these loops are system variable and sensor reads that I’d like to monitor/control. Am I correct in assuming the the thing.handle() is where the thinger.io dashboard and all my arduino thing variables are synced? So is it OK to have multiple handle() calls… for example one in each loop?

Thanks

RoboBill

You are right @RoboBill the thing.handle() call allows different things, like:

  • Handling the connectivity of your device with your network, reconnecting if it get disconnected
  • Handling the authentication with the platform
  • Attending read/write request from any device/dashboards
  • Handling keep alives to ensure is still connected to the server

So it is required to call the thing.handle() periodically to handle all these possible events over time, thus it is called inside the loop. This way, having multiple thing.handle() calls inside the loop will not change the behaviour of the program. All the variables or elements that you are controlling or displaying in the dashboards, can be updated in a single thing.handle() call. However it is important to not add delays (big ones) inside the loop, although you can still using the loop for your own code or control. If you add delays you will find a laggy response when controlling/reading the device.

Not sure if I have explained it well! Let me know if you need more details, or you have an example where you intended to use multiple calls, so we can improve the design.

Yes you explained it perfectly. And yes I do have a few delay()s but they are very short (~50msec).

My robot is controlled by quadrature encoders, an old Lidar Lite and a POZYX. So each one has its own sub-program where I will drop in the handle() function. To me that’s so simple! :+1:

RoboBill

Great! This delay is not a problem for the connection.

If you need bigger delays, you can also use the timestamp-based approach, similar to the Blink Without Delay example of Arduino, and this will not affect the behaviour of the thing.handle() call.

I cannot understand quite well why you need the handle() call in each sub-program, do you have different microcontrollers for the functions of the robot? i.e., one for encoders, one for lidar, etc.? Or is a big sketch where you call different functions controlling encoders, lidar, etc?

Yes, within each sub program are sensor variables that I want to watch or tweak. It’s probably not be the right way to program, but it works and its easy for me to follow/debug.

Ok! It is safe to call handle() multiple times if you want. However try calling handle() once to see if it you get a different behaviour. It will probably work in the same way than doing multiple calls. Notice that calling handle() will not necessarily update the variables you are monitoring :wink: