Reconnect After MQTT Disconnect SDK 3.7.0-3

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

cross mob
BrYo_2033861
Level 1
Level 1
First like received

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.

0 Likes
9 Replies
Anonymous
Not applicable

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.

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.

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;

lock attach
Attachments are accessible only for community members.

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

MichaelF_56
Moderator
Moderator
Moderator
250 sign-ins 25 comments on blog 10 comments on blog

Thanks for sharing the work around vtunr

adding a few others that may be interested: jmartinbyokumrashdstudejiodjjwgangiabersaxel.linvsha

0 Likes
lock attach
Attachments are accessible only for community members.

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?

0 Likes
Anonymous
Not applicable

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.

0 Likes
Anonymous
Not applicable

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

Anonymous
Not applicable

jmartin​ can't believe it but for lack of additional information, this is what i am resorting to. echoing axel.lin​ in a different post, this probably is used in production by many. mwf_mmfae​​ 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?

0 Likes