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

Wemos D1 R2 lagging #658

Open Georgeeden opened 5 years ago

Georgeeden commented 5 years ago

Hello, I currently have a esp8266 12-E hosting the broker/server using UMQTTbroker and when I look at the mqtt topic data0 on my pc using mqtt lenses the feed is very fast and there is no lag but when I use pubsub on the wemos d1 r2 it seems to struggle and get stuck behind, is there a way to make it be able to accept up to 20 messages per second from the mqtt topic and also if it gets behind just go straight to the latest message rather than getting hung up on old messages?

Many Thanks

knolleary commented 5 years ago

It depends on what processing you're doing with each message. There's nothing in the client to help.

There is no way in mqtt to skip messages you are subscribed to; you have to handle them.

Georgeeden commented 5 years ago

https://pastebin.com/uFNXfxVk this is the code that I am using on the esp8266 I am using to receive the MQTT messages, at the moment the broker is sending the message every 200ms and the message it is sending is 000000000 that changes to 000255000. Not sure if that gives you any more information, just after running it for about a minute it takes 8-15 seconds to catch up on all the messages although MQTTLens is always in real-time (this shows it isn't a problem on the broker side)

knolleary commented 5 years ago

The only two suggestions I have are:

  1. Try removing the serial printing from the callback so you remove unnecessary work

  2. Try adding a delay(10) before the call to client.loop. I have heard of problems being caused by calling client.loop it a tight loop like you have it - it hits the network client pretty hard to stops it handling traffic as efficiently as the client is constantly checking if it is connected. A short delay might actually help the throughput.