How to check the connection with the MQTT broker before publishing? – Nordic Q&A – Nordic DevZone
I am using the MQTT example from NCS 2.3.0 (samples/net/mqtt), and I notice that I have an issue when I have a long publishing interval. My application needs to be low power, so I disable keepalive and stay in PSM (Power Save Mode) between publications.
But sometimes when I try to publish, I receive:
mqtt_helper: Socket error: POLLERR
mqtt_helper: Connection was unexpectedly closed
So I end up losing the data I tried to publish, even though the device connects to the MQTT broker afterwards, as they are not sent and are no longer on the bus for another attempt to send.
How could I verify the connection with the MQTT broker before attempting to publish the data to avoid the mentioned issues?
My goal is to publish the data if the device is connected and, if it is not, initiate the connection with the broker, and once the connection is established, perform the publish.
Read more here: Source link
