How often to call MQTT client.loop()

In many examples using the PubSubClient library, the MQTT client.loop() function is placed in the main 'void loop()' section of the sketch.

I have a sketch where the main 'loop' section is running at more than 100x per second if there is no action from connected sensors. So in my case the MQTT client.loop() is called at very high frequency.

Would it be better to reduce the number of MQTT client.loop() calls ? How often should it be called ?

No. Call it as quickly as possible.

If you want the system to be as responsive as possible than call it as often as possible.

Why not ?

How many times per second does it run if you call MQTT client.loop() from loop()?

Does calling client.loop() cause network traffic ?

Quote from

"I would not recommend calling it in a tight loop without any delay at all - I have seen that cause a mini denial-of-service on the network hardware as it constantly checks to see if there's anything to do."

Have you had a problem? What's the minimum update rate need such that your project's performance is acceptable? If you're worried about it, you could always include a millis()-based timer that throttles the update rate without slowing down the rest of your loop.

I have a dedicated FreeRTOS task on ESP32 that calls loop() every 100ms. That's fast enough for the particular project. So, it depends on what's needed.

If you do decide to implement a delay between calls to client.loop() then don't be tempted to use the delay() function

That's why I wrote:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.