Handling MQTT library in intermittent wifi

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ViRa_3651841
Level 1
Level 1
First like given

Hi all,

We've been developing using the WICED platform for several months now, and overall are happy with the functionality. However, one key thing prevent us from taking this solution to production is the handling of intermittent wifi. For instance, customers will not always have perfect wifi. So in the case where wifi connection goes in and out (testing by rebooting our router) what is the correct way to handle the MQTT library? We are using wiced_network_register_link_callback() function to monitor the wifi link - so the wifi connection is automatically restored without any action on the application side. However, once the wifi link is up again, no MQTT function calls work. I've tried disconnecting/deinitializing the MQTT library and re-initializing it, but this doesn't seem to work (the mqtt_disconnected event is also never received in the mqtt callback function).

Would appreciate any input on recommended ways to handle the MQTT library in such a case. Is there something we should be doing in the wifi link callback functions? The main use case we are worried about is when we are publishing several large MQTT messages and the wifi connection drops for a few seconds.

Thanks,

0 Likes
1 Solution

Hello,

The MQTT disconnect event is generated when a request is raised to close the MQTT connection by calling mqtt_conn_close(). If the wifi link drops for some reason, the mqtt lib will return WICED_ERROR due to timeout. The wifi link down event is not linked to mqtt events.

To generate a MQTT disconnect event because of wifi link down event, we might have to generate the event in wifi link down callback function which triggers an event to MQTT lib.

It would make more sense to create a new event for the above case to differentiate the reason for disconnection.(Whether we have explicitely called mqtt_conn_close or the connection dropped because of wifi disconnection)

View solution in original post

3 Replies
PriyaM_16
Moderator
Moderator
Moderator
250 replies posted 100 replies posted 50 replies posted

May I ask the sequence of calls you are using for disconnecting/deinitializing the MQTT connection? Have you tried mqtt_conn_close() followed by wiced_mqtt_deinit() ?

Thanks for your response PriyaM_16

Yes, I am using the mqtt_conn_close() followed by wiced_mqtt_deinit()

Under normal usage, I am able to easily bring the network up and down repeatedly. However, when the wifi connection drops momentarily, the mqtt_publish function times out. After this, if I call mqtt_conn_close, then this function also times out. The MQTT_DISCONNECTED event is never received in my callback function. I think this is the crux of the issue. Let me know if you have any ideas you'd like me to try out.

Edit: Just wanted to add one thing - I reduced my keep alive timeout to 5s so that any momentary wifi connection drop is enough to disconnect the MQTT library. In this case, the MQTT_DISCONNECTED event is received and I'm able to reconnect successfully. However, a keep alive of 5s is not viable for production so looking to keep my original keep alive value of 300s.

Edit2: Is there something we should be doing in the wifi link up and link down callbacks to inform the MQTT library that the wifi link is down?

0 Likes

Hello,

The MQTT disconnect event is generated when a request is raised to close the MQTT connection by calling mqtt_conn_close(). If the wifi link drops for some reason, the mqtt lib will return WICED_ERROR due to timeout. The wifi link down event is not linked to mqtt events.

To generate a MQTT disconnect event because of wifi link down event, we might have to generate the event in wifi link down callback function which triggers an event to MQTT lib.

It would make more sense to create a new event for the above case to differentiate the reason for disconnection.(Whether we have explicitely called mqtt_conn_close or the connection dropped because of wifi disconnection)