Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 7 months ago.
MQTT permanent connection
How can I leave the connection always open waiting to receive a publication? Communication is lost after a few minutes. I do not know if there is a possibility that he will always listen, like other bookstores. Example of arduino pubsubclient. I played with yield but I do not get more than a couple of minutes. Thank you
Question relating to:
2 Answers
6 years, 3 months ago.
Hi,
I'm facing exactly the same issue using easyConnect and MQTT libraries. The connection get lost after some idle time. I need to keep the connection alive. Secondly, the method client.isConnected always return true even if it is not connected to the broker.
Please guide.
6 years, 7 months ago.
The MQTT server to which you are connecting could have some information on its log as to why the connection was broken. This would be a good place to start, if you have access to the log. Or you could connect to a locally running server like Mosquitto if the log is inaccessible to you.
My guess is that it's a keep alive timeout. Keep alive is an MQTT mechanism for ensuring that the TCP stays connected, and detecting closed connections. A 'ping' message is sent by the client library at intervals specified by the keep alive interval, and it expects a response from the server. A simple way to verify this is to set the keep alive interval to 0, which turns off this mechanism, and see if the behaviour changes. If the connection stays active then you need to call yield() at sufficient intervals, depending on the keep alive interval used, to allow the 'ping' exchange to complete.