knolleary / pubsubclient

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

Serial blocking with too frequent calls to client.loop() & reconnect()? #241

Open hatfieldr opened 7 years ago

hatfieldr commented 7 years ago

I am using an ESP-01(AT command set) connected to an arduino Meg2560 using hardware serial at 115200 baud. It was fairly easy to get it to connect to WiFi using WiFiEsp.h & to an MQTT broker but I have been tearing my hair out for the last week as no messages were received! The problem was solved by calling: if (!client.connected()) reconnect(); & client.loop(); every 100ms (non blocking) instead of every loop. I presume there is no problem if sketches are flashed to the ESP (no serial connection); I also presume that if the ESP is connected via a serial port & the functions are called every loop the serial connection is overwhelmed with status messages, blocking incoming data. Is this correct? I am a newbie using ESP & MQTT Is there any disadvantage calling these functions every 100 ms?

zhoufusong commented 7 years ago

I get the same problem. and I cannot receive MQTTPINGRESP after send MQTTPINGREQ so it cause to disconnect when the time is beyond keep alive time

himanshu-hearthacker commented 6 years ago

hey did you find any solution to that. My code blocks at mqtt.loop() function . Probably in the readPacket() function.

EmilyNerdGirl commented 6 years ago

Same issue here too. Was doing some work with a PMS5003 interfaced via serial and took awhile to figure out it was client loop that was causing my problems.

himanshu-hearthacker commented 6 years ago

my code stuck at _client->read() in readByte() function. It happens after 2-3 hours of operation