3 Replies Latest reply on Aug 9, 2019 4:19 AM by PriyaM_16

    Handling MQTT library in intermittent wifi


      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.



        • 1. Re: Handling MQTT library in intermittent wifi

          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() ?

          1 of 1 people found this helpful
          • 2. Re: Handling MQTT library in intermittent wifi

            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?

            • 3. Re: Handling MQTT library in intermittent wifi


              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)

              1 of 1 people found this helpful