knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.82k stars 1.47k forks source link

ESP32 MQTT publish issue. TCP buffers the data instead sending one by one thus causing latency at broker. #923

Closed parthesh-hub closed 2 years ago

parthesh-hub commented 2 years ago

Hello everyone, we are currently working on project that involves IMU sensors along with ESP32 PubSubClient. We are trying to send sensor data to MQTT broker every 100ms(its fast because that's the need of the project)

The Serial Monitor says it is getting successfully sent on broker after 100ms. But on the broker it is getting published at uneven rate(some after 200ms or some after 2ms gap). There is uneven gap between messages which should be 100ms approx. We taught the issue might be on the MQTT broker but if we send the data per second then it is getting perfectly published on time.

But in our case we need faster speed (publish per 100ms).. here the ESP32 shows uncertain behavior..

We debug it and found out that it is because of TCP Buffer which accumulates multiple messages sent from esp32 and sends it to broker at once. Thus the subscriber receives the data at uncertain intervals. If we publish data after 1second it works fine but if we are publishing at fast rate(in our case 100ms, which is needed) this buffering behavior of TCP is causes issue.

Have anyone faced such issue? How to solve this? Thank you in advance :)

We are using ESP32 version : 1.0.6

fsommer1968 commented 2 years ago

Hi, check the ESP network library for TCP nodelay option. Because if have more experience with 8266 here is an explanation: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/client-class.html#setnodelay

parthesh-hub commented 2 years ago

Thanks for the reply!! We have already tried this.. but no success..

Finally we moved to websocket method using another library and that works nicely!!!

programmeddeath1 commented 2 months ago

@parthesh-hub Can you mention which library worked for you?