9 Replies Latest reply on Jun 8, 2017 1:08 AM by l.bertelli_1513841

    Reconnect After MQTT Disconnect SDK 3.7.0-3

    byokum_2033861

      How can my application reestablish an MQTT connection after receiving an MQTT disconnect event?  I've tried everything I can think of, and just about everything causes a reboot when I try to reestablish the connection.  All the Cypress examples (shadow.c in particular) don't seem to deal with a disconnect event.

        • 1. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
          rash

          In our application we are checking "wiced_mqtt_disconnect" in "aws_mqtt_conn_close" function. This will create " WICED_MQTT_EVENT_TYPE_DISCONNECTED" event in the call back function. Now it is USER depended on how to use this event.

          1 of 1 people found this helpful
          • 2. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
            byokum_2033861

            rash

            The problem is not when we call "wiced_mqtt_disconnect".  Any kind of network issue, such as unplugging the router's Internet connection, causes the WICED_MQTT_EVENT_TYPE_DISCONNECTED event to be generated.  Once this occurs, I have not found any way to reestablish the connection.  I see a continuous stream of these WICED_MQTT_EVENT_TYPE_DISCONNECTED events, about once per second, and any attempt to reopen, or close then reopen, the MQTT connection results in a device reset.  Ideally, I would think that the MQTT stack should try to automatically reconnect.  This doesn't appear to be happening, so I am looking for a way to maintain the connection to AWS IoT reliably.

            • 3. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
              jmartin_1809706

              well I get around it by just rebooting unless I'm expecting a disconnect.

               

                  switch ( event->type )

                  {

                  case WICED_MQTT_EVENT_TYPE_DISCONNECTED:

                         printf("WICED_MQTT_EVENT_TYPE_DISCONNECTED\r\n");

                         if (ignore_disconnect==0)

                         {

                            WPRINT_APP_INFO(("Restarting the device...\n"));

                            wiced_framework_reboot();

                          }

                  break;

              1 of 1 people found this helpful
              • 4. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
                vtunr

                Hi,

                 

                I figured out a way to reconnect MQTT. If you use TLS, use 3.7.0-3 otherwise you'll end up with a memory leak at the reconnection.

                You probably won't be able to apply the patch since I changed a lot of thing in my working tree, but you can look at the diff.

                 

                You just have to call when you catch the event disconnected. (it probably can be integrated in the mqtt lib)

                mqtt_network_deinit(&(((mqtt_connection_t*) mqtt_obj)->socket));
                mqtt_connection_deinit((mqtt_connection_t*) mqtt_obj);

                 

                And then just call your function that wrap around wiced_mqtt_connect and it should work without reboot, and without any memory leak.

                 

                Regards,

                 

                vtunr

                2 of 2 people found this helpful
                • 5. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
                  mifo

                  Thanks for sharing the work around vtunr

                   

                  adding a few others that may be interested: jmartin_1809706 byokum_2033861 rash DaveStude djjw grga abers axel.lin_1746341 vsha

                  • 6. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
                    byokum_2033861

                    I made the changes to mqtt_connection, mqtt_manager, and mqtt_network as shown in your diff, and added the calls to mqtt_network_deinit and mqtt_connection_deinit.  I am still unable to reconnect to AWS IoT after the disconnect event occurs.  When I try to reconnect, I am still experiencing a reboot.  I've attached the relevant code I'm using.  I've tried various combinations of calls when the disconnect occurs, starting with simply calling aws_mqtt_conn_open all the way up to trying to completely shutdown everything and restarting mqtt from the beginning.  Nothing seems to work.  Do you have any ideas?

                    • 7. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
                      akolamb1

                      jmartin_1809706 can't believe it but for lack of additional information, this is what i am resorting to. echoing axel.lin_1746341 in a different post, this probably is used in production by many. mifo if you can focus some light on this issue and get developers to provide a solution, it will make be sleep better! Is this problem specific to BCM94343W or repeatable for other WiFi modules?

                      • 8. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
                        nishal

                        Hi,

                         

                        I am using SDK_4.1.0. In this SDK the above attached patch is not there. Is it required to apply this patch?

                         

                        Thanks in advance.

                        • 9. Re: Reconnect After MQTT Disconnect SDK 3.7.0-3
                          l.bertelli_1513841

                          Hi,

                          I had the same issue, the TLS stack has a memory leak in some place.

                          You can try the solution in every mqtt connection consume about 8k ram

                          Simply add the wiced tls deinit call in the MQTT deinit:

                           

                          wiced_result_t mqtt_network_deinit( mqtt_socket_t *socket )

                          {

                              wiced_tcp_unregister_callbacks( &socket->socket );

                              mqtt_network_disconnect( socket );

                              if ( socket->socket.tls_context != NULL )

                              {

                                  wiced_tls_reset_context( socket->socket.tls_context );

                                  wiced_tls_deinit_identity( &socket->tls_identity ); // add this call

                              }

                              if ( socket->socket.tls_context != NULL )

                              {

                                  wiced_tls_deinit_root_ca_certificates( );

                              }

                              wiced_tcp_delete_socket( &socket->socket );

                              return WICED_SUCCESS;

                          }

                           

                          Hope this helps

                          Lorenzo

                          2 of 2 people found this helpful