knolleary / pubsubclient

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

Problem of maintaining the connection with the mqtt broker #763

Open maiquiISA7 opened 4 years ago

maiquiISA7 commented 4 years ago

Hello, congratulations on your work!

I'm having a problem maintaining the connection with the mqtt broker. The connection is terminated during data transmission. I'm using a sim800 and Esp8266, the WiFi works normally.

Do I need to change any keepalive or timeout parameters?

Erriez commented 4 years ago

Can you enable debugging and post the output of your serial terminal?

Are you using multiple devices? I noticed that my Mosquito MQTT server disconnected every few minutes when using multiple ESP8266 devices with the same client ID.

This was a solution for me, but works only if you have multiple devices:

String clientId = "ESP8266Client-" + String(random(0xffff), HEX);
client.connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD);
baqwas commented 4 years ago

Hello @Erriez,

I can confirm your previous experience with clients from multiple devices. I've never lost connection (to an internal broker) after ensuring unique client ids are in use (and disabling the SD card, if any).

Kind regards.

jov58 commented 3 years ago

My solution was to add a call to loop() in the callback function. The initial collection of topics after a new subscription was way faster and prevented disconnections.