<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nrf9160 : MQTT connection failure</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/66372/nrf9160-mqtt-connection-failure</link><description>Hi, 
 Our application uses MQTT over TLS to talk to a MQTT broker. 
 The MQTT APIs used are like in the &amp;#39;mqtt_simple&amp;#39; 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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 29 Sep 2020 16:11:15 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/66372/nrf9160-mqtt-connection-failure" /><item><title>RE: nrf9160 : MQTT connection failure</title><link>https://devzone.nordicsemi.com/thread/272126?ContentTypeID=1</link><pubDate>Tue, 29 Sep 2020 16:11:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:43345927-5da7-4d06-bf48-3e1d78ed1e30</guid><dc:creator>Heidi</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;No, closing the socket directly should not be necessary.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;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.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Try letting the event handler return before attempting to call mqtt_connect again.&amp;nbsp;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 &lt;a href="https://github.com/zephyrproject-rtos/zephyr/blob/master/samples/net/mqtt_publisher/src/main.c#L364"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;If you are still having trouble, it would be nice if you could provide&amp;nbsp;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.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Heidi&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>