This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf9160 : MQTT connection failure

Hi,

Our application uses MQTT over TLS to talk to a MQTT broker.

The MQTT APIs used are like in the 'mqtt_simple' sample code.

When mqtt_connect is called with invalid client ID, we see that connect call is successful and we get MQTT event -  MQTT_EVT_CONNACK with result code 4.

So we try to redo the mqtt_connect and if this is done multiple times, we see subsequent tcp socket open calls failing.

We suspect its because the sockets used in mqtt_connect is not getting closed, we tried invoking mqtt_disconnect to close the socket in such cases , but that didn't help. mqtt_disconnect failed with return -128.

Finally when we explicitly did a close on the mqtt socket handle , the issue seems resolved, just wanted to know if this is what we should be doing in the application.

Is there any other cleanup that the application should handle.

Thanks in advance

Parents
  • Hi!

    No, closing the socket directly should not be necessary.

    MQTT event MQTT_EVT_CONNACK with result code 4 means the MQTT broker rejected the client. So after the event is handled (the event handler return) the socket will be closed automatically by the library.

    However, if you are trying to re-connect in the event handler, right after receiving MQTT_EVT_CONNACK, this will not work, as the socket will not be closed by the library. 

    Try letting the event handler return before attempting to call mqtt_connect again. For instance, set a flag in the event handler and then verify it in a loop from the main thread. One of the samples in Zephyr does something like this, see here.

    If you are still having trouble, it would be nice if you could provide some sample code from your application. It is much easier to find potential mistakes this way. I can also make the ticket private, at your request, if you would prefer to keep your code private.

    Best regards,

    Heidi

Reply
  • Hi!

    No, closing the socket directly should not be necessary.

    MQTT event MQTT_EVT_CONNACK with result code 4 means the MQTT broker rejected the client. So after the event is handled (the event handler return) the socket will be closed automatically by the library.

    However, if you are trying to re-connect in the event handler, right after receiving MQTT_EVT_CONNACK, this will not work, as the socket will not be closed by the library. 

    Try letting the event handler return before attempting to call mqtt_connect again. For instance, set a flag in the event handler and then verify it in a loop from the main thread. One of the samples in Zephyr does something like this, see here.

    If you are still having trouble, it would be nice if you could provide some sample code from your application. It is much easier to find potential mistakes this way. I can also make the ticket private, at your request, if you would prefer to keep your code private.

    Best regards,

    Heidi

Children
No Data
Related