<?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 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/58285/nrf9160-using-mqtt-with-tls</link><description>Hi everyone, 
 I am looking at using MQTT v3.1.1 with TLS 1.2, to send data to Azure IoT Hub. 
 I tested the MQTT Simple sample and it worked perfeclty. So I am modifying this project to enable TLS. To do so I followed those threads: 
 https://devzone</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 06 Mar 2020 07:43:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/58285/nrf9160-using-mqtt-with-tls" /><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/238433?ContentTypeID=1</link><pubDate>Fri, 06 Mar 2020 07:43:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0d62b721-3920-4bde-a35d-13336ff8dbb7</guid><dc:creator>thomallain</dc:creator><description>&lt;p&gt;&lt;span&gt;Hi&amp;nbsp;&lt;/span&gt;&lt;span&gt;H&amp;aring;kon,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I have followed what you said, and I have been able to connect to Azure IoT Hub both with my computer and the nRF9160.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;First I look at using paho_mqtt to connect to Azure IoT Hub. I found out that I had to use a username&amp;nbsp;looking like this : hostname/deviceId/API_version. I also found out that as my certificates were self-signed I could not use the certificates verification, so I had to remove it (for the nRF9160 it means peer_verify=0)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Here is my python code:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="python"&gt;from paho.mqtt import client as mqtt
import ssl
import os
import time

path_to_root_cert = &amp;quot;test2_cert.pem&amp;quot;
cert_file = &amp;quot;mqttTest_cert.pem&amp;quot;
key_file = &amp;quot;mqttTest_key.pem&amp;quot;

device_id = &amp;quot;mqtt_test&amp;quot;
iot_hub_name = &amp;quot;test-aptus&amp;quot;

def on_connect(client, userdata, flags, rc):
    print(&amp;quot;Device connected with result code: &amp;quot; + str(rc))

def on_disconnect(client, userdata, rc):
    print(&amp;quot;Device disconnected with result code: &amp;quot; + str(rc))

def on_publish(client, userdata, mid):
    print(&amp;quot;Device sent message&amp;quot;)

client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311)

client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish

# Set the username but not the password on your client
client.username_pw_set(username=iot_hub_name+&amp;quot;.azure-devices.net/&amp;quot; +
                       device_id + &amp;quot;/?api-version=2018-06-30&amp;quot;, password=None)

# Set the certificate and key paths on your client

client.tls_set(ca_certs=path_to_root_cert, certfile=cert_file, keyfile=key_file,
               cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
# Connect as before
client.connect(iot_hub_name+&amp;quot;.azure-devices.net&amp;quot;, port=8883)

client.loop_start()
client.publish(&amp;quot;devices/&amp;quot; + device_id + &amp;quot;/messages/events/&amp;quot;, &amp;quot;{id=123}&amp;quot;, qos=1)
time.sleep(10) # wait
client.loop_stop() #stop the loop&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I then&amp;nbsp;added those changes to my nRF9160 project, and I added a publish call to sent a message to Azure.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Here is my main :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;uart.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

#include &amp;lt;net/mqtt.h&amp;gt;
#include &amp;lt;net/socket.h&amp;gt;
#include &amp;lt;lte_lc.h&amp;gt;
#include &amp;lt;certificates.h&amp;gt;

#if defined(CONFIG_BSD_LIBRARY)
#include &amp;quot;nrf_inbuilt_key.h&amp;quot;
#endif

//#include CONFIG_CERTIFICATES_FILE

#if defined(CONFIG_LWM2M_CARRIER)
#include &amp;lt;lwm2m_carrier.h&amp;gt;
#endif

#define MQTT_USERNAME &amp;quot;test-aptus.azure-devices.net/mqtt_test/?api-version=2018-06-30&amp;quot;
struct mqtt_utf8 name;

//#define CONFIG_NRF_CLOUD_SEC_TAG 1
#define NRF_CLOUD_HOSTNAME CONFIG_MQTT_BROKER_HOSTNAME
//#define NRF_CLOUD_SEC_TAG  CONFIG_NRF_CLOUD_SEC_TAG

static sec_tag_t sec_tag_list[] = {CONFIG_SEC_TAG};

/* Buffers for MQTT client. */
static u8_t rx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
static u8_t tx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
static u8_t payload_buf[CONFIG_MQTT_PAYLOAD_BUFFER_SIZE];

/* The mqtt client struct */
static struct mqtt_client client;

/* MQTT Broker details. */
static struct sockaddr_storage broker;

/* Connected flag */
static bool connected;

/* File descriptor */
static struct pollfd fds;

#if defined(CONFIG_BSD_LIBRARY)

/**@brief Recoverable BSD library error. */
void bsd_recoverable_error_handler(uint32_t err)
{
	printk(&amp;quot;bsdlib recoverable error: %u\n&amp;quot;, (unsigned int)err);
}

/**@brief Irrecoverable BSD library error. */
void bsd_irrecoverable_error_handler(uint32_t err)
{
	printk(&amp;quot;bsdlib irrecoverable error: %u\n&amp;quot;, err);

	__ASSERT_NO_MSG(false);
}

#endif /* defined(CONFIG_BSD_LIBRARY) */



/**@brief Function to print strings without null-termination
 */
static void data_print(u8_t *prefix, u8_t *data, size_t len)
{
	char buf[len + 1];
	memcpy(buf, data, len);
	buf[len] = 0;
	printk(&amp;quot;%s%s\n&amp;quot;, prefix, buf);
}

/**@brief Function to publish data on the configured topic
 */
static int data_publish(struct mqtt_client *c, enum mqtt_qos qos,
	u8_t *data, size_t len)
{
	struct mqtt_publish_param param;

	param.message.topic.qos = qos;
	param.message.topic.topic.utf8 = CONFIG_MQTT_PUB_TOPIC;
	param.message.topic.topic.size = strlen(CONFIG_MQTT_PUB_TOPIC);
	param.message.payload.data = data;
	param.message.payload.len = len;
	param.message_id = sys_rand32_get();
	param.dup_flag = 0;
	param.retain_flag = 0;

	data_print(&amp;quot;Publishing: &amp;quot;, data, len);
	printk(&amp;quot;to topic: %s len: %u\n&amp;quot;,
		CONFIG_MQTT_PUB_TOPIC,
		(unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC));

	return mqtt_publish(c, &amp;amp;param);
}

/**@brief Function to subscribe to the configured topic
 */
static int subscribe(void)
{
	struct mqtt_topic subscribe_topic = {
		.topic = {
			.utf8 = CONFIG_MQTT_SUB_TOPIC,
			.size = strlen(CONFIG_MQTT_SUB_TOPIC)
		},
		.qos = MQTT_QOS_1_AT_LEAST_ONCE
	};

	const struct mqtt_subscription_list subscription_list = {
		.list = &amp;amp;subscribe_topic,
		.list_count = 1,
		.message_id = 1234
	};

	printk(&amp;quot;Subscribing to: %s len %u\n&amp;quot;, CONFIG_MQTT_SUB_TOPIC,
		(unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC));

	return mqtt_subscribe(&amp;amp;client, &amp;amp;subscription_list);
}

/**@brief Function to read the published payload.
 */
static int publish_get_payload(struct mqtt_client *c, size_t length)
{
	u8_t *buf = payload_buf;
	u8_t *end = buf + length;

	if (length &amp;gt; sizeof(payload_buf)) {
		return -EMSGSIZE;
	}

	while (buf &amp;lt; end) {
		int ret = mqtt_read_publish_payload(c, buf, end - buf);

		if (ret &amp;lt; 0) {
			int err;

			if (ret != -EAGAIN) {
				return ret;
			}

			printk(&amp;quot;mqtt_read_publish_payload: EAGAIN\n&amp;quot;);

			err = poll(&amp;amp;fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));
			if (err &amp;gt; 0 &amp;amp;&amp;amp; (fds.revents &amp;amp; POLLIN) == POLLIN) {
				continue;
			} else {
				return -EIO;
			}
		}
		if (ret == 0) {
			return -EIO;
		}

		buf += ret;
	}

	return 0;
}

/**@brief MQTT client event handler
 */
void mqtt_evt_handler(struct mqtt_client *const c,
		      const struct mqtt_evt *evt)
{
	int err;
	printk(&amp;quot;mqtt event handler called \n&amp;quot;);
	switch (evt-&amp;gt;type) {
	case MQTT_EVT_CONNACK:
		if (evt-&amp;gt;result != 0) {
			printk(&amp;quot;MQTT connect failed %d\n&amp;quot;, evt-&amp;gt;result);
			break;
		}

		connected = true;
		printk(&amp;quot;[%s:%d] MQTT client connected!\n&amp;quot;, __func__, __LINE__);
		subscribe();
		break;

	case MQTT_EVT_DISCONNECT:
		printk(&amp;quot;[%s:%d] MQTT client disconnected %d\n&amp;quot;, __func__,
		       __LINE__, evt-&amp;gt;result);

		connected = false;
		break;

	case MQTT_EVT_PUBLISH: {
		const struct mqtt_publish_param *p = &amp;amp;evt-&amp;gt;param.publish;

		printk(&amp;quot;[%s:%d] MQTT PUBLISH result=%d len=%d\n&amp;quot;, __func__,
		       __LINE__, evt-&amp;gt;result, p-&amp;gt;message.payload.len);
		err = publish_get_payload(c, p-&amp;gt;message.payload.len);
		if (err &amp;gt;= 0) {
			data_print(&amp;quot;Received: &amp;quot;, payload_buf,
				p-&amp;gt;message.payload.len);
			/* Echo back received data */
			data_publish(&amp;amp;client, MQTT_QOS_1_AT_LEAST_ONCE,
				payload_buf, p-&amp;gt;message.payload.len);
		} else {
			printk(&amp;quot;mqtt_read_publish_payload: Failed! %d\n&amp;quot;, err);
			printk(&amp;quot;Disconnecting MQTT client...\n&amp;quot;);

			err = mqtt_disconnect(c);
			if (err) {
				printk(&amp;quot;Could not disconnect: %d\n&amp;quot;, err);
			}
		}
	} break;

	case MQTT_EVT_PUBACK:
		if (evt-&amp;gt;result != 0) {
			printk(&amp;quot;MQTT PUBACK error %d\n&amp;quot;, evt-&amp;gt;result);
			break;
		}

		printk(&amp;quot;[%s:%d] PUBACK packet id: %u\n&amp;quot;, __func__, __LINE__,
				evt-&amp;gt;param.puback.message_id);
		break;

	case MQTT_EVT_SUBACK:
		if (evt-&amp;gt;result != 0) {
			printk(&amp;quot;MQTT SUBACK error %d\n&amp;quot;, evt-&amp;gt;result);
			break;
		}

		printk(&amp;quot;[%s:%d] SUBACK packet id: %u\n&amp;quot;, __func__, __LINE__,
				evt-&amp;gt;param.suback.message_id);
		break;

	default:
		printk(&amp;quot;[%s:%d] default: %d\n&amp;quot;, __func__, __LINE__,
				evt-&amp;gt;type);
		break;
	}
}

/**@brief Resolves the configured hostname and
 * initializes the MQTT broker structure
 */
static void broker_init(void)
{
	int err;
	struct addrinfo *result;
	struct addrinfo *addr;
	struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = SOCK_STREAM
	};

	err = getaddrinfo(CONFIG_MQTT_BROKER_HOSTNAME, NULL, &amp;amp;hints, &amp;amp;result);
	if (err) {
		printk(&amp;quot;ERROR: getaddrinfo failed %d\n&amp;quot;, err);

		return;
	}

	addr = result;
	err = -ENOENT;

	/* Look for address of the broker. */
	while (addr != NULL) {
		/* IPv4 Address. */
		if (addr-&amp;gt;ai_addrlen == sizeof(struct sockaddr_in)) {
			struct sockaddr_in *broker4 =
				((struct sockaddr_in *)&amp;amp;broker);
			char ipv4_addr[NET_IPV4_ADDR_LEN];

			broker4-&amp;gt;sin_addr.s_addr =
				((struct sockaddr_in *)addr-&amp;gt;ai_addr)
				-&amp;gt;sin_addr.s_addr;
			broker4-&amp;gt;sin_family = AF_INET;
			broker4-&amp;gt;sin_port = htons(CONFIG_MQTT_BROKER_PORT);

			inet_ntop(AF_INET, &amp;amp;broker4-&amp;gt;sin_addr.s_addr,
				  ipv4_addr, sizeof(ipv4_addr));
			printk(&amp;quot;IPv4 Address found %s\n&amp;quot;, ipv4_addr);

			break;
		} else {
			printk(&amp;quot;ai_addrlen = %u should be %u or %u\n&amp;quot;,
				(unsigned int)addr-&amp;gt;ai_addrlen,
				(unsigned int)sizeof(struct sockaddr_in),
				(unsigned int)sizeof(struct sockaddr_in6));
		}

		addr = addr-&amp;gt;ai_next;
		break;
	}

	/* Free the address. */
	freeaddrinfo(result);
}

/**@brief Initialize the MQTT client structure
 */
static void client_init(struct mqtt_client *client)
{
	mqtt_client_init(client);

	broker_init();

	/* MQTT client configuration */
	client-&amp;gt;broker = &amp;amp;broker;
	client-&amp;gt;evt_cb = mqtt_evt_handler;
	client-&amp;gt;client_id.utf8 = (u8_t *)CONFIG_MQTT_CLIENT_ID;
	client-&amp;gt;client_id.size = strlen(CONFIG_MQTT_CLIENT_ID);
        //printk(&amp;quot;clientId %s \n&amp;quot;, CONFIG_MQTT_CLIENT_ID);
	client-&amp;gt;password = NULL;
        name.size = strlen(MQTT_USERNAME);
        name.utf8 = (u8_t *)MQTT_USERNAME;
	client-&amp;gt;user_name= &amp;amp;name;
	client-&amp;gt;protocol_version = MQTT_VERSION_3_1_1;

	/* MQTT buffers configuration */
	client-&amp;gt;rx_buf = rx_buffer;
	client-&amp;gt;rx_buf_size = sizeof(rx_buffer);
	client-&amp;gt;tx_buf = tx_buffer;
	client-&amp;gt;tx_buf_size = sizeof(tx_buffer);

	/* MQTT transport configuration */
        #if defined(CONFIG_MQTT_LIB_TLS)
          struct mqtt_sec_config *tls_config = &amp;amp;client-&amp;gt;transport.tls.config;
    
          client-&amp;gt;transport.type = MQTT_TRANSPORT_SECURE;
    
          tls_config-&amp;gt;peer_verify = 0;
          tls_config-&amp;gt;cipher_count = 0;
          tls_config-&amp;gt;cipher_list = NULL;
          tls_config-&amp;gt;sec_tag_count = ARRAY_SIZE(sec_tag_list); //0;//
          tls_config-&amp;gt;sec_tag_list = sec_tag_list; //NULL;//
          tls_config-&amp;gt;hostname = CONFIG_MQTT_BROKER_HOSTNAME;
          #else /* MQTT transport configuration */
          client-&amp;gt;transport.type = MQTT_TRANSPORT_NON_SECURE;
          #endif /* defined(CONFIG_MQTT_LIB_TLS) */
          
}

/**@brief Initialize the file descriptor structure used by poll.
 */
static int fds_init(struct mqtt_client *c)
{
	if (c-&amp;gt;transport.type == MQTT_TRANSPORT_NON_SECURE) {
		fds.fd = c-&amp;gt;transport.tcp.sock;
	} else {
#if defined(CONFIG_MQTT_LIB_TLS)
		fds.fd = c-&amp;gt;transport.tls.sock;
#else
		return -ENOTSUP;
#endif
	}

	fds.events = POLLIN;

	return 0;
}

/**@brief Configures modem to provide LTE link. Blocks until link is
 * successfully established.
 */
static void modem_configure(void)
{
#if defined(CONFIG_LTE_LINK_CONTROL)
	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
		/* Do nothing, modem is already turned on
		 * and connected.
		 */
	} else {
#if defined(CONFIG_LWM2M_CARRIER)
		/* Wait for the LWM2M_CARRIER to configure the modem and
		 * start the connection.
		 */
		printk(&amp;quot;Waitng for carrier registration...\n&amp;quot;);
		k_sem_take(&amp;amp;carrier_registered, K_FOREVER);
		printk(&amp;quot;Registered!\n&amp;quot;);
#else /* defined(CONFIG_LWM2M_CARRIER) */
		int err;
		printk(&amp;quot;LTE Link Connecting ...\n&amp;quot;);
		err = lte_lc_init_and_connect();
		__ASSERT(err == 0, &amp;quot;LTE link could not be established.&amp;quot;);
		printk(&amp;quot;LTE Link Connected!\n&amp;quot;);
#endif /* defined(CONFIG_LWM2M_CARRIER) */
	}
#endif /* defined(CONFIG_LTE_LINK_CONTROL) */
}

/* Provisions root CA certificate using nrf_inbuilt_key API */
static int provision_certificate(void)
{
#if defined(CONFIG_PROVISION_CERTIFICATES)
#if defined(CONFIG_BSD_LIBRARY)
	{
		int err;

		/* Delete certificates */
		nrf_sec_tag_t sec_tag = (nrf_sec_tag_t) sec_tag_list[0];

		for (nrf_key_mgnt_cred_type_t type = 0; type &amp;lt; 5; type++) {
			printk(&amp;quot;Deleting certs sec_tag: %d\n&amp;quot;, sec_tag);
			err = nrf_inbuilt_key_delete(sec_tag, type);
			printk(&amp;quot;nrf_inbuilt_key_delete(%u, %d) =&amp;gt; result=%d\n&amp;quot;,
				sec_tag, type, err);
		}

#if defined(CA_CERTIFICATE)
		/* Provision CA Certificate. */
		printk(&amp;quot;Write ca certs sec_tag: %d\n&amp;quot;, sec_tag);
		err = nrf_inbuilt_key_write(sec_tag,
			NRF_KEY_MGMT_CRED_TYPE_CA_CHAIN,
			CA_CERTIFICATE,
			strlen(CA_CERTIFICATE));
		if (err) {
			printk(&amp;quot;CA_CERTIFICATE err: %d\n&amp;quot;, err);
			return err;
		}
#endif
#if defined (CLIENT_PRIVATE_KEY)
		/* Provision Private Certificate. */
		printk(&amp;quot;Write private cert sec_tag: %d\n&amp;quot;, sec_tag);
		err = nrf_inbuilt_key_write(
			sec_tag,
			NRF_KEY_MGMT_CRED_TYPE_PRIVATE_CERT,
			CLIENT_PRIVATE_KEY,
			strlen(CLIENT_PRIVATE_KEY));
		if (err) {
			printk(&amp;quot;CLIENT_PRIVATE_KEY err: %d\n&amp;quot;, err);
			return err;
		}
#endif
#if defined(CLIENT_PUBLIC_CERTIFICATE)
		/* Provision Public Certificate. */
		printk(&amp;quot;Write public cert sec_tag: %d\n&amp;quot;, sec_tag);
		err = nrf_inbuilt_key_write(
			sec_tag,
			NRF_KEY_MGMT_CRED_TYPE_PUBLIC_CERT,
			CLIENT_PUBLIC_CERTIFICATE,
			strlen(CLIENT_PUBLIC_CERTIFICATE));
		if (err) {
			printk(&amp;quot;CLIENT_PUBLIC_CERTIFICATE err: %d\n&amp;quot;,err);
			return err;
		}
	}
#endif
#else
	{
		int err;
		err = tls_credential_add(CONFIG_SEC_TAG,
			TLS_CREDENTIAL_CA_CERTIFICATE,
			NRF_CLOUD_CA_CERTIFICATE,
			sizeof(NRF_CLOUD_CA_CERTIFICATE));
		if (err &amp;lt; 0) {
			printk(&amp;quot;Failed to register ca certificate: %d\n&amp;quot;,err);
			return err;
		}
		err = tls_credential_add(CONFIG_SEC_TAG,
			TLS_CREDENTIAL_PRIVATE_KEY,
			NRF_CLOUD_CLIENT_PRIVATE_KEY,
			sizeof(NRF_CLOUD_CLIENT_PRIVATE_KEY));
		if (err &amp;lt; 0) {
			printk(&amp;quot;Failed to register private key: %d\n&amp;quot;,err);
			return err;
		}
		err = tls_credential_add(CONFIG_SEC_TAG,
			TLS_CREDENTIAL_SERVER_CERTIFICATE,
			NRF_CLOUD_CLIENT_PUBLIC_CERTIFICATE,
			sizeof(NRF_CLOUD_CLIENT_PUBLIC_CERTIFICATE));
		if (err &amp;lt; 0) {
			printk(&amp;quot;Failed to register public certificate: %d\n&amp;quot;,err);
			return err;
		}

	}
#endif /* defined(CONFIG_BSD_LIBRARY) */
#endif /* defined(CONFIG_PROVISION_CERTIFICATES) */

	return 0;
}

void main(void)
{
	int err;
        uint8_t pub[4] = {0x01, 0x02, 0x03, 0x04};

	printk(&amp;quot;The MQTT simple sample started\n&amp;quot;);

        int err_provision = provision_certificate();
        if (err_provision != 0) {
          printk(&amp;quot;ERROR: nct_provision failure %d\n&amp;quot;, err_provision);
          return;
        }
        printk(&amp;quot;err_provision = %d\n&amp;quot;, err_provision);

	modem_configure();

	client_init(&amp;amp;client);

	err = mqtt_connect(&amp;amp;client);
	if (err != 0) {
		printk(&amp;quot;ERROR: mqtt_connect %d\n&amp;quot;, err);
		return;
	}

	err = fds_init(&amp;amp;client);
	if (err != 0) {
		printk(&amp;quot;ERROR: fds_init %d\n&amp;quot;, err);
		return;
	}

        
	while (1) {
                data_publish(&amp;amp;client,1,pub,4);

		err = poll(&amp;amp;fds, 1, K_SECONDS(10));
		if (err &amp;lt; 0) {
			printk(&amp;quot;ERROR: poll %d\n&amp;quot;, errno);
			break;
		}
                printk(&amp;quot;After Poll\n&amp;quot;);
		err = mqtt_live(&amp;amp;client);
		if (err != 0) {
			printk(&amp;quot;ERROR: mqtt_live %d\n&amp;quot;, err);
			break;
		}
                

		if ((fds.revents &amp;amp; POLLIN) == POLLIN) {
			err = mqtt_input(&amp;amp;client);
			if (err != 0) {
				printk(&amp;quot;ERROR: mqtt_input %d\n&amp;quot;, err);
				break;
			}
		}

		if ((fds.revents &amp;amp; POLLERR) == POLLERR) {
			printk(&amp;quot;POLLERR\n&amp;quot;);
			break;
		}

		if ((fds.revents &amp;amp; POLLNVAL) == POLLNVAL) {
			printk(&amp;quot;POLLNVAL\n&amp;quot;);
			break;
		}
	}
	printk(&amp;quot;Disconnecting MQTT client...\n&amp;quot;);

	err = mqtt_disconnect(&amp;amp;client);
	if (err) {
		printk(&amp;quot;Could not disconnect MQTT client. Error: %d\n&amp;quot;, err);
	}
}

&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Here is my log :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;***** Booting Zephyr OS build v2.0.99-ncs1 *****
The MQTT simple sample started
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 0) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 1) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 2) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 3) =&amp;gt; result=2
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 4) =&amp;gt; result=2
Write ca certs sec_tag: 2
Write private cert sec_tag: 2
Write public cert sec_tag: 2
err_provision = 0
LTE Link Connecting ...
LTE Link Connected!
IPv4 Address found 13.95.15.251
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:187] MQTT client connected!
Subscribing to: /my/subscribe/topic len 19
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:237] SUBACK packet id: 1234
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 16457
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 18574
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 22424
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 24013
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 27520
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 30390
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;It is not perfect yet as it sent a lot of message before disconnect but I have at least communication working.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1583480599167v1.png" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you very much for you help&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;EDIT :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;To decrease the number of message I sent I have added a IF statement on the polling, within which I call the publish function.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;while (1) {
		err = poll(&amp;amp;fds, 1, K_SECONDS(10));
		if (err &amp;lt; 0) {
			printk(&amp;quot;ERROR: poll %d\n&amp;quot;, errno);
			break;
		}
                if (err==0){
                  data_publish(&amp;amp;client,1,pub,4);
                }
                printk(&amp;quot;After Poll\n&amp;quot;);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;***** Booting Zephyr OS build v2.0.99-ncs1 *****
The MQTT simple sample started
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 0) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 1) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 2) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 3) =&amp;gt; result=2
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 4) =&amp;gt; result=2
Write ca certs sec_tag: 2
Write private cert sec_tag: 2
Write public cert sec_tag: 2
err_provision = 0
LTE Link Connecting ...
LTE Link Connected!
IPv4 Address found 13.95.15.251
After Poll
mqtt event handler called 
[mqtt_evt_handler:187] MQTT client connected!
Subscribing to: /my/subscribe/topic len 19
After Poll
mqtt event handler called 
[mqtt_evt_handler:237] SUBACK packet id: 1234
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 55663
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 6776
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 30147
Publishing: &#x1;&#x2;&#x3;&#x4;
to topic: devices/mqtt_test/messages/events/ len: 34
After Poll
After Poll
mqtt event handler called 
[mqtt_evt_handler:227] PUBACK packet id: 55055&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/237736?ContentTypeID=1</link><pubDate>Tue, 03 Mar 2020 12:47:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71a238ab-3cf5-4b13-a77d-6f79d0a26101</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
[quote user="thomallain"]If peer_verify=2 : I got&amp;nbsp;ERROR: mqtt_connect -45 when I try to connect (both with J-Link RTT Viewer and the debug terminal)[/quote]
&lt;p&gt;&amp;nbsp;This makes sense if you do not have a root CA to verify the hostname against.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="thomallain"]If peer_verify= 1 : In my J-Link RTT Viewer, I have nothing after the &amp;quot;IPv4 Address found 13.95.15.251&amp;quot; statement. But when I use the debug terminal from Segger Embedded Studio, I got :[/quote]
&lt;p&gt;-57 -&amp;gt; errno.h::ENOTCONN&lt;/p&gt;
&lt;p&gt;-61 -&amp;gt; errno.h::ECONNREFUSED.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you tried to connect using your generated certificates on a PC, using mosquitto_sub / mosquitto_pub, to see if you connect successfully ?&lt;/p&gt;
&lt;p&gt;There&amp;#39;s a guide on using certificates here and on stack overflow:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/"&gt;http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/34491799/secured-ssl-connection-with-mosquitto-broker"&gt;https://stackoverflow.com/questions/34491799/secured-ssl-connection-with-mosquitto-broker&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/237644?ContentTypeID=1</link><pubDate>Tue, 03 Mar 2020 09:13:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:74b2263c-f9ac-4824-8f04-89970c688757</guid><dc:creator>thomallain</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;H&amp;aring;kon,,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I have totally removed and reinstalled the ncs/nrf repo.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;With your code and with my code with TLS (no certificates) and with hostname :&amp;nbsp;test.mosquitto.org, I got the same log as you, with the mqtt_event_handler working.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now with my certificates for Azure IoT Hub :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;- If peer_verify=2 : I got&amp;nbsp;ERROR: mqtt_connect -45 when I try to connect (both with J-Link RTT Viewer and the debug terminal)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;- If peer_verify= 1 : In my J-Link RTT Viewer, I have nothing after the &amp;quot;IPv4 Address found 13.95.15.251&amp;quot; statement. But when I use the debug terminal from Segger Embedded Studio, I got :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;IPv4 Address found 13.95.15.251
After Poll
mqtt event handler called 
MQTT connect failed 3
After Poll
mqtt event handler called 
MQTT connect failed -61
ERROR: mqtt_input -57
Disconnecting MQTT client...
Could not disconnect MQTT client. Error: -57&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The only differences between my Azure Mqtt code and the test on mosquitto is that in prj.conf I added&amp;nbsp;&lt;strong&gt;:&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_MQTT_CLIENT_ID=&amp;quot;mqtt_test&amp;quot;
CONFIG_MQTT_BROKER_HOSTNAME=&amp;quot;test-aptus.azure-devices.net&amp;quot;

CONFIG_PROVISION_CERTIFICATES=y
CONFIG_CERTIFICATES_FILE=&amp;quot;certificates.h&amp;quot;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And in my main.c, I changed the configuration to :&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;          tls_config-&amp;gt;peer_verify = 2; // or 1
          tls_config-&amp;gt;cipher_count = 0;
          tls_config-&amp;gt;cipher_list = NULL;
          tls_config-&amp;gt;sec_tag_count = ARRAY_SIZE(sec_tag_list); //0;
          tls_config-&amp;gt;sec_tag_list = sec_tag_list; //NULL;
          tls_config-&amp;gt;hostname = CONFIG_MQTT_BROKER_HOSTNAME;&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/237477?ContentTypeID=1</link><pubDate>Mon, 02 Mar 2020 13:59:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1a8e95d-6105-4670-ab81-4373cae1c381</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;I tested with both v1.1.0 and v1.2.0, and get this behavior:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;***** Booting Zephyr OS build v2.0.99-ncs1 *****
The MQTT simple sample started
LTE Link Connecting ...
+CEREG: 2,&amp;quot;76C1&amp;quot;,&amp;quot;014ACE00&amp;quot;,7,0,0,&amp;quot;11100000&amp;quot;,&amp;quot;11100000&amp;quot;
+CEREG: 1,&amp;quot;76C1&amp;quot;,&amp;quot;014ACE00&amp;quot;,7,,,&amp;quot;11100000&amp;quot;,&amp;quot;00001111&amp;quot;
LTE Link Connected!
IPv4 Address found 137.135.83.217
[mqtt_evt_handler:190] MQTT client connected!
Subscribing to: my/subscribe/topic len 18
After Poll
[mqtt_evt_handler:240] SUBACK packet id: 1234
After Poll&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s my source for reference:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-0683360d7afe4b7799fd4c1548c43424/mqtt_5F00_simple.zip"&gt;devzone.nordicsemi.com/.../mqtt_5F00_simple.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Please note that the .peer_verify shall be &amp;#39;2&amp;#39; in an actual product, as we are currently not performing hostname verification.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you test this and see if you still do not get connected and suback callback?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/237428?ContentTypeID=1</link><pubDate>Mon, 02 Mar 2020 12:19:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e2d43419-8f30-48de-96b0-e0a5cef7b3e4</guid><dc:creator>thomallain</dc:creator><description>&lt;p&gt;&lt;span&gt;Hi&amp;nbsp;&lt;/span&gt;&lt;span&gt;H&amp;aring;kon,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I also got the time out if I try with test.mosquito.org. Yes it seems to be down&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I did not remove the printk calls from the mqtt_evt_handler. I also tried with mqtt.eclipse.org and I have the same thing : the mqtt_event handler is never called&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/237408?ContentTypeID=1</link><pubDate>Mon, 02 Mar 2020 11:43:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e84ed9b0-5587-4107-b3f6-72bacba0cf54</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The mqtt_simple sample will just connect, and subscribe to a given topic. Not much more will happen there, unless you publish to the specific topic that the nrf is subscribed to.&lt;/p&gt;
&lt;p&gt;Poll will stand there for CONFIG_MQTT_KEEPALIVE seconds before sending a keep-alive, so it might not do much unless you wait 60 seconds (default value), where then should print &amp;quot;after poll&amp;quot; again.&lt;/p&gt;
&lt;p&gt;I did this exact setup with mqtt_simple (.peer_verify = 1 for testing purposes), connecting to mqtt.eclipse.org port 8883, and I&amp;#39;m not seeing the same failure as you.&lt;/p&gt;
&lt;p&gt;Note that I am not testing with test.mosquitto.org. It seems to be down? I get a timed out request back when I try to connect to port 8883, which I also get if I use mosquitto_sub on my PC.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you removed the prints in the&amp;nbsp;mqtt_evt_handler() ? You do not seem to get any mqtt-related prints from that one.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;PS: I see that you&amp;#39;re using ncs v1.1.0. We have released v1.2.0, but this sample should work on both.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/236802?ContentTypeID=1</link><pubDate>Thu, 27 Feb 2020 11:25:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bcf8a881-66eb-4de4-8810-6fd45092ddef</guid><dc:creator>thomallain</dc:creator><description>&lt;p&gt;Hi &lt;span&gt;H&amp;aring;kon,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Q1 : I have change the main stack size and memory pool size as you showed. And the issue is stil there&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Q2: I have the Root CA, the Client CA and the Client Key in a header file certificates.h. It looks like this :&lt;pre class="ui-code" data-mode="c_cpp"&gt;
#define CLIENT_PRIVATE_KEY \
&amp;quot;-----BEGIN RSA PRIVATE KEY-----\n&amp;quot; \
&amp;quot;MIIEowIBAAKCAQEAmHoUn2xBQf9+9QQYyxZZtsmJYw/N5z1cO4yt6S2bpIeynfU8\n&amp;quot; \
...
&amp;quot;dP7W0GsXt3aYNSdfTjcp7cw62gEvJ64BI+FKxzpbocnetWCeyUaB\n&amp;quot; \
&amp;quot;-----END RSA PRIVATE KEY-----\n&amp;quot;



#define CLIENT_PUBLIC_CERTIFICATE \
&amp;quot;-----BEGIN CERTIFICATE-----\n&amp;quot; \
&amp;quot;MIICvDCCAaSgAwIBAgIEAQhZ6DANBgkqhkiG9w0BAQsFADAQMQ4wDAYDVQQDDAVU\n&amp;quot; \
...
&amp;quot;o5ym/Cy1VLs28we6Uk3FgbSQy8aBHV8JplbapPaLYQ8=\n&amp;quot; \
&amp;quot;-----END CERTIFICATE-----\n&amp;quot;



#define CA_CERTIFICATE \
&amp;quot;-----BEGIN CERTIFICATE-----\n&amp;quot; \
&amp;quot;MIICtjCCAZ6gAwIBAgIJAIjqRXrEC+UGMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNV\n&amp;quot; \
...
&amp;quot;xCYJngfVEThKYGTCyCC09GsG6uWcRDfnrwo=\n&amp;quot; \
&amp;quot;-----END CERTIFICATE-----\n&amp;quot;
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I now answer first Q4 as I did it before Q3 and it showed me interesting things&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Q4: Yes I can connect to mosquitto and wait for the event with the modification you showed. The first time I got the mqtt event handler being triggered with :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;[mqtt_evt_handler:169] MQTT client connected!
[mqtt_evt_handler:219] SUBACK packet id: 1234&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;But not the other times. I changed the clien Id and restarted the the DK but nothing appears. So I added a printk call after the poll function and I got :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt; The MQTT simple sample started

err_provision = 0

LTE Link Connecting ...

LTE Link Connected!
 
IPv4 Address found 5.196.95.208
 
After Poll&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I print &amp;quot;After Poll&amp;quot; just after the poll function&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Q3 : Now I am back with my DNS, and the certificates. I am testing with peer-verify =1and I am looking at were it is hanging out. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I have left the &amp;quot;After Poll&amp;quot; printk call, and I have added a &amp;quot;CONNECTED&amp;quot; printk call after the mqtt_connect function and a &amp;quot;FDS init done&amp;quot; after the fds_init function like here :&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;err = mqtt_connect(&amp;amp;client);
	if (err != 0) {
		printk(&amp;quot;ERROR: mqtt_connect %d\n&amp;quot;, err);
		return;
	}
        printk(&amp;quot;\n CONNECTED\n&amp;quot;);

	err = fds_init(&amp;amp;client);
	if (err != 0) {
		printk(&amp;quot;ERROR: fds_init %d\n&amp;quot;, err);
		return;
	}
        printk(&amp;quot;FDS init done \n&amp;quot;);
	while (1) {
		err = poll(&amp;amp;fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));
		if (err &amp;lt; 0) {
			printk(&amp;quot;ERROR: poll %d\n&amp;quot;, errno);
			break;
		}
                printk(&amp;quot;After Poll&amp;quot;);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;And I got this :&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;***** Booting Zephyr OS build v2.0.99-ncs1 *****
The MQTT simple sample started
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 0) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 1) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 2) =&amp;gt; result=0
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 3) =&amp;gt; result=2
Deleting certs sec_tag: 2
nrf_inbuilt_key_delete(2, 4) =&amp;gt; result=2
Write ca certs sec_tag: 2
Write private cert sec_tag: 2
Write public cert sec_tag: 2
err_provision = 0
LTE Link Connecting ...
LTE Link Connected!
IPv4 Address found 13.95.15.251
clientId mqtt_test 

 CONNECTED
FDS init done 
After poll
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;So it seems the connection is successfull but that I got no response from server like with the test on mosquitto&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Using MQTT with TLS</title><link>https://devzone.nordicsemi.com/thread/236445?ContentTypeID=1</link><pubDate>Wed, 26 Feb 2020 08:58:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:09c55c6e-bea6-415a-ba67-c940f629cb37</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Q1: How large are your certificates?&lt;/p&gt;
&lt;p&gt;They are casted from const to non-const, meaning that they&amp;#39;ll be stacked. Increasing the main stack size might be beneficial (double the heap as well, testing purposes):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_MAIN_STACK_SIZE=8192
CONFIG_HEAP_MEM_POOL_SIZE=4096&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Q2: Do you chain your CA root? Ie: having one root auth. certificate and a second one? The .peer_verify value of 2 indicates that you shall verify the hostname, while 1 states optional, and 0 is do not verify.&lt;/p&gt;
&lt;p&gt;If your broker requires key-pair + CA, you need to provide a CA to the host you&amp;#39;re connecting to + a root CA (ie: a chained CA certificate)&lt;/p&gt;
&lt;p&gt;In C format, it&amp;#39;ll look like this:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/55393/nrf9160-adding-intermediate-certificates-to-the-tls-engine/225697#225697"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/55393/nrf9160-adding-intermediate-certificates-to-the-tls-engine/225697#225697&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Q3: When you&amp;#39;re setting .peer_verify=1, does it hang forever? Does it print anything? Have you checked if the firmware is stuck somewhere?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Q4: Have you checked if TLS section is working, by for instance connecting to mosquitto? You can connect to mosquitto,&amp;nbsp;&lt;a href="https://test.mosquitto.org/"&gt;https://test.mosquitto.org/&lt;/a&gt;, on port 8883 without any certificates to see if your TLS setup itself is working. In this case, you can set peer_verify=1, and&amp;nbsp;tls_config-&amp;gt;sec_tag_count =&amp;nbsp;0 , tls_config-&amp;gt;sec_tag_list = NULL&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>