<?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>MQTT Connection Status -22</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/91060/mqtt-connection-status--22</link><description>Hi. I am trying to develop a project that use mqtt connection using nrf9160. Currently using thingy:91 for evalutation board. I am using Mobily Network on NB IoT at Saudi Arabia. Now I manage to connect to NB-IoT Network but when it is trying to connect</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 18 Aug 2022 09:02:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/91060/mqtt-connection-status--22" /><item><title>RE: MQTT Connection Status -22</title><link>https://devzone.nordicsemi.com/thread/382187?ContentTypeID=1</link><pubDate>Thu, 18 Aug 2022 09:02:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1391d286-40f0-49d4-bc83-38e56af9c9c6</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;It looks like you&amp;#39;re doing something very similar to this sample setup, which should work:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/connectivity/networking/api/mqtt.html"&gt;http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/connectivity/networking/api/mqtt.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The first thing I notice is that you&amp;#39;re using the arrow operator where the sample uses the dot operator (client-&amp;gt;broker vs client.broker).&lt;/p&gt;
&lt;p&gt;Any reason you&amp;#39;ve done this?&lt;/p&gt;
&lt;p&gt;Since you get an error related to the input, the first thing I would do is check that the client object looks like you expect before it&amp;#39;s given to the mqtt_connect function.&lt;/p&gt;
&lt;p&gt;Try to print all the values in your client object and check that they are what you expect.&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MQTT Connection Status -22</title><link>https://devzone.nordicsemi.com/thread/382162?ContentTypeID=1</link><pubDate>Thu, 18 Aug 2022 08:07:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9b12e3ba-742d-4974-8004-afe99617abea</guid><dc:creator>azwawe</dc:creator><description>&lt;p&gt;it is on line 758, mqtt_connect(&amp;amp;client).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MQTT Connection Status -22</title><link>https://devzone.nordicsemi.com/thread/382152?ContentTypeID=1</link><pubDate>Thu, 18 Aug 2022 07:40:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f74687e3-7ac9-49a1-adb8-763c1b57208f</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;What specific function returns the error code?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MQTT Connection Status -22</title><link>https://devzone.nordicsemi.com/thread/382066?ContentTypeID=1</link><pubDate>Wed, 17 Aug 2022 13:41:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a1637a3e-3d99-4303-9650-02ff8f07d39b</guid><dc:creator>azwawe</dc:creator><description>&lt;p&gt;Thanks for your fast response.&lt;/p&gt;
&lt;p&gt;The input is only a client object.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;
#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;nrf_modem_at.h&amp;gt;
#include &amp;lt;modem/lte_lc.h&amp;gt;
#include &amp;lt;modem/location.h&amp;gt;
#include &amp;lt;date_time.h&amp;gt;
#include &amp;lt;zephyr/drivers/uart.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;usb/usb_device.h&amp;gt;
#include &amp;lt;drivers/uart.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;zephyr/net/mqtt.h&amp;gt;
#include &amp;lt;zephyr/net/socket.h&amp;gt;
#include &amp;lt;zephyr/random/rand32.h&amp;gt;

#if defined(CONFIG_NRF_MODEM_LIB)
#include &amp;lt;nrf_modem_at.h&amp;gt;
#endif

#include &amp;lt;modem/lte_lc.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;

/* Check which one is the modem */
#if defined(CONFIG_MODEM_KEY_MGMT)
#include &amp;lt;modem/modem_key_mgmt.h&amp;gt;
#endif

#if defined(CONFIG_LWM2M_CARRIER)
#include &amp;lt;lwm2m_carrier.h&amp;gt;
#endif

#include &amp;lt;dk_buttons_and_leds.h&amp;gt;

#define CONFIG_LTE_CONNECT_RETRY_DELAY_S 3
#define CONFIG_MQTT_RECONNECT_DELAY_S 5
#define CONFIG_MQTT_MESSAGE_BUFFER_SIZE 128
#define CONFIG_MQTT_PAYLOAD_BUFFER_SIZE 128
#define CONFIG_MQTT_PUB_TOPIC &amp;quot;palmlab_gps_location&amp;quot;
#define CONFIG_MQTT_SUB_TOPIC &amp;quot;palmlab_subs_location&amp;quot;
#define CONFIG_MQTT_BROKER_HOSTNAME &amp;quot;broker.emqx.io&amp;quot;
#define CONFIG_MQTT_CLIENT_ID &amp;quot;palmlab_thingy91&amp;quot;
#define CONFIG_MQTT_BROKER_PORT 1883


double latt, lon, acc, res;

static K_SEM_DEFINE(location_event, 0, 1);
static K_SEM_DEFINE(lte_connected, 0, 1);
static K_SEM_DEFINE(time_update_finished, 0, 1);


/* Buffers for MQTT client. */
static uint8_t rx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
static uint8_t tx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
static uint8_t payload_buf[CONFIG_MQTT_PAYLOAD_BUFFER_SIZE];

static struct mqtt_client client;
static struct sockaddr_storage broker;

static struct pollfd fds;

#if defined(CONFIG_MQTT_LIB_TLS)
static int certificates_provision(void)
{
&amp;#160; &amp;#160; int err = 0;
&amp;#160; &amp;#160; LOG_INF(&amp;quot;Provisioning certificates&amp;quot;);
#if defined(CONFIG_NRF_MODEM_LIB) &amp;amp;&amp;amp; defined(CONFIG_MODEM_KEY_MGMT)

&amp;#160; &amp;#160; err = modem_key_mgmt_write(CONFIG_MQTT_TLS_SEC_TAG,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;CA_CERTIFICATE,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;strlen(CA_CERTIFICATE));
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_ERR(&amp;quot;Failed to provision CA certificate: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return err;
&amp;#160; &amp;#160; }

#elif defined(CONFIG_BOARD_QEMU_X86) &amp;amp;&amp;amp; defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
&amp;#160; &amp;#160; err = tls_credential_add(CONFIG_MQTT_TLS_SEC_TAG,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;TLS_CREDENTIAL_CA_CERTIFICATE,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;CA_CERTIFICATE,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;sizeof(CA_CERTIFICATE));
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_ERR(&amp;quot;Failed to register CA certificate: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return err;
&amp;#160; &amp;#160; }
#endif

&amp;#160; &amp;#160; return err;
}
#endif /* defined(CONFIG_MQTT_LIB_TLS) */


#if defined(CONFIG_LWM2M_CARRIER)
K_SEM_DEFINE(carrier_registered, 0, 1);
int lwm2m_carrier_event_handler(const lwm2m_carrier_event_t *event)
{
&amp;#160; &amp;#160; switch (event-&amp;gt;type) {
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_BSDLIB_INIT:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_BSDLIB_INIT&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_CONNECTING:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_CONNECTING&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_CONNECTED:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_CONNECTED&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_DISCONNECTING:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_DISCONNECTING&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_DISCONNECTED:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_DISCONNECTED&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_BOOTSTRAPPED:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_BOOTSTRAPPED&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_REGISTERED:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_REGISTERED&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; k_sem_give(&amp;amp;carrier_registered);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_DEFERRED:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_DEFERRED&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_FOTA_START:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_FOTA_START&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_REBOOT:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;LWM2M_CARRIER_EVENT_REBOOT&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; case LWM2M_CARRIER_EVENT_ERROR:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_ERR(&amp;quot;LWM2M_CARRIER_EVENT_ERROR: code %d, value %d&amp;quot;,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; ((lwm2m_carrier_event_error_t *)event-&amp;gt;data)-&amp;gt;code,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; ((lwm2m_carrier_event_error_t *)event-&amp;gt;data)-&amp;gt;value);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; default:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_WRN(&amp;quot;Unhandled LWM2M_CARRIER_EVENT: %d&amp;quot;, event-&amp;gt;type);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; return 0;
}
#endif /* defined(CONFIG_LWM2M_CARRIER) */

// print a new strings without a null termination
static void data_print(uint8_t *prefix, uint8_t *data, size_t len)
{
&amp;#160; &amp;#160; char buf[len + 1];

&amp;#160; &amp;#160; memcpy(buf, data, len);
&amp;#160; &amp;#160; buf[len] = 0;
&amp;#160; &amp;#160; printk(buf);
}

// publish data to configured topic
static int data_publish(struct mqtt_client *c, enum mqtt_qos qos, uint8_t *data, size_t len)
{
&amp;#160; &amp;#160; struct mqtt_publish_param param;

&amp;#160; &amp;#160; param.message.topic.qos = qos;
&amp;#160; &amp;#160; param.message.topic.topic.utf8 = CONFIG_MQTT_PUB_TOPIC;
&amp;#160; &amp;#160; param.message.topic.topic.size = strlen(CONFIG_MQTT_PUB_TOPIC);
&amp;#160; &amp;#160; param.message.payload.data = data;
&amp;#160; &amp;#160; param.message.payload.len = len;
&amp;#160; &amp;#160; param.message_id = sys_rand32_get();
&amp;#160; &amp;#160; param.dup_flag = 0;
&amp;#160; &amp;#160; param.retain_flag = 0;

&amp;#160; &amp;#160; data_print(&amp;quot;Publishing: &amp;quot;, data, len);
&amp;#160; &amp;#160; printk(&amp;quot;to topic: %s len: %u&amp;quot;,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; CONFIG_MQTT_PUB_TOPIC,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; (unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC));

&amp;#160; &amp;#160; return mqtt_publish(c, &amp;amp;param);
}

// subscribe to configured topic
static int subscribe(void)
{
&amp;#160; &amp;#160; struct mqtt_topic subscribe_topic = {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; .topic = {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; .utf8 = CONFIG_MQTT_SUB_TOPIC,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; .size = strlen(CONFIG_MQTT_SUB_TOPIC)
&amp;#160; &amp;#160; &amp;#160; &amp;#160; },
&amp;#160; &amp;#160; &amp;#160; &amp;#160; .qos = MQTT_QOS_1_AT_LEAST_ONCE
&amp;#160; &amp;#160; };

&amp;#160; &amp;#160; const struct mqtt_subscription_list subscription_list = {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; .list = &amp;amp;subscribe_topic,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; .list_count = 1,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; .message_id = 1234
&amp;#160; &amp;#160; };

&amp;#160; &amp;#160; printk(&amp;quot;Subscribing to: %s len %u&amp;quot;, CONFIG_MQTT_SUB_TOPIC,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; (unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC));

&amp;#160; &amp;#160; return mqtt_subscribe(&amp;amp;client, &amp;amp;subscription_list);
}

// get the payload to the configured topic as a client
static int publish_get_payload(struct mqtt_client *c, size_t length)
{
&amp;#160; &amp;#160; int ret;
&amp;#160; &amp;#160; int err = 0;

&amp;#160; &amp;#160; if (length &amp;gt; sizeof(payload_buf)) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; err = -EMSGSIZE;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; while (length &amp;gt; sizeof(payload_buf)) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; ret = mqtt_read_publish_payload_blocking(
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; c, payload_buf, (length - sizeof(payload_buf)));
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (ret == 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; return -EIO;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; } else if (ret &amp;lt; 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; return ret;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; length -= ret;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; ret = mqtt_readall_publish_payload(c, payload_buf, length);
&amp;#160; &amp;#160; if (ret) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return ret;
&amp;#160; &amp;#160; }
&amp;#160; &amp;#160; return err;
}

// MQTT event handler
void mqtt_evt_handler(struct mqtt_client *const c,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; const struct mqtt_evt *evt)
{
&amp;#160; &amp;#160; int err;

&amp;#160; &amp;#160; switch (evt-&amp;gt;type) {
&amp;#160; &amp;#160; case MQTT_EVT_CONNACK:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (evt-&amp;gt;result != 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;MQTT connect failed: %d&amp;quot;, evt-&amp;gt;result);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;MQTT client connected&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; subscribe();
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case MQTT_EVT_DISCONNECT:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;MQTT client disconnected: %d&amp;quot;, evt-&amp;gt;result);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case MQTT_EVT_PUBLISH: {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; const struct mqtt_publish_param *p = &amp;amp;evt-&amp;gt;param.publish;

&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;MQTT PUBLISH result=%d len=%d&amp;quot;,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; evt-&amp;gt;result, p-&amp;gt;message.payload.len);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; err = publish_get_payload(c, p-&amp;gt;message.payload.len);

&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (p-&amp;gt;message.topic.qos == MQTT_QOS_1_AT_LEAST_ONCE) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; const struct mqtt_puback_param ack = {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; .message_id = p-&amp;gt;message_id
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; };

&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; /* Send acknowledgment. */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; mqtt_publish_qos1_ack(&amp;amp;client, &amp;amp;ack);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (err &amp;gt;= 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; data_print(&amp;quot;Received: &amp;quot;, payload_buf,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; p-&amp;gt;message.payload.len);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; /* Echo back received data */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; data_publish(&amp;amp;client, MQTT_QOS_1_AT_LEAST_ONCE,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; payload_buf, p-&amp;gt;message.payload.len);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; } else if (err == -EMSGSIZE) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Received payload (%d bytes) is larger than the payload buffer &amp;quot;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;quot;size (%d bytes).&amp;quot;,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; p-&amp;gt;message.payload.len, sizeof(payload_buf));
&amp;#160; &amp;#160; &amp;#160; &amp;#160; } else {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;publish_get_payload failed: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Disconnecting MQTT client...&amp;quot;);

&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; err = mqtt_disconnect(c);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Could not disconnect: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; }
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }
&amp;#160; &amp;#160; } break;

&amp;#160; &amp;#160; case MQTT_EVT_PUBACK:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (evt-&amp;gt;result != 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;MQTT PUBACK error: %d&amp;quot;, evt-&amp;gt;result);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;PUBACK packet id: %u&amp;quot;, evt-&amp;gt;param.puback.message_id);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case MQTT_EVT_SUBACK:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (evt-&amp;gt;result != 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;MQTT SUBACK error: %d&amp;quot;, evt-&amp;gt;result);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;SUBACK packet id: %u&amp;quot;, evt-&amp;gt;param.suback.message_id);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case MQTT_EVT_PINGRESP:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (evt-&amp;gt;result != 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;MQTT PINGRESP error: %d&amp;quot;, evt-&amp;gt;result);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; default:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Unhandled MQTT event type: %d&amp;quot;, evt-&amp;gt;type);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; }
}

// initialize the broker
static int broker_init(void)
{
&amp;#160; &amp;#160; int err;
&amp;#160; &amp;#160; struct addrinfo *result;
&amp;#160; &amp;#160; struct addrinfo *addr;
&amp;#160; &amp;#160; struct addrinfo hints = {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; .ai_family = AF_INET,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; .ai_socktype = SOCK_STREAM
&amp;#160; &amp;#160; };

&amp;#160; &amp;#160; err = getaddrinfo(CONFIG_MQTT_BROKER_HOSTNAME, NULL, &amp;amp;hints, &amp;amp;result);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;getaddrinfo failed: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return -ECHILD;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; addr = result;

&amp;#160; &amp;#160; /* Look for address of the broker. */
&amp;#160; &amp;#160; while (addr != NULL) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; /* IPv4 Address. */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (addr-&amp;gt;ai_addrlen == sizeof(struct sockaddr_in)) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; struct sockaddr_in *broker4 =
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; ((struct sockaddr_in *)&amp;amp;broker);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; char ipv4_addr[NET_IPV4_ADDR_LEN];

&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; broker4-&amp;gt;sin_addr.s_addr =
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; ((struct sockaddr_in *)addr-&amp;gt;ai_addr)
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; -&amp;gt;sin_addr.s_addr;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; broker4-&amp;gt;sin_family = AF_INET;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; broker4-&amp;gt;sin_port = htons(CONFIG_MQTT_BROKER_PORT);

&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; inet_ntop(AF_INET, &amp;amp;broker4-&amp;gt;sin_addr.s_addr,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; ipv4_addr, sizeof(ipv4_addr));
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;IPv4 Address found %s&amp;quot;, log_strdup(ipv4_addr));

&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; } else {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;ai_addrlen = %u should be %u or %u&amp;quot;,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; (unsigned int)addr-&amp;gt;ai_addrlen,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; (unsigned int)sizeof(struct sockaddr_in),
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; (unsigned int)sizeof(struct sockaddr_in6));
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; addr = addr-&amp;gt;ai_next;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; freeaddrinfo(result);
&amp;#160; &amp;#160; return err;
}

#if defined(CONFIG_NRF_MODEM_LIB)
#define IMEI_LEN 15
#define CGSN_RESPONSE_LENGTH (IMEI_LEN + 6 + 1) /* Add 6 for \r\nOK\r\n and 1 for \0 */
#define CLIENT_ID_LEN sizeof(&amp;quot;nrf-&amp;quot;) + IMEI_LEN
#else
#define RANDOM_LEN 10
#define CLIENT_ID_LEN sizeof(CONFIG_BOARD) + 1 + RANDOM_LEN
#endif /* defined(CONFIG_NRF_MODEM_LIB) */

// get the client id information
static const uint8_t* client_id_get(void)
{
&amp;#160; &amp;#160; static uint8_t client_id[MAX(sizeof(CONFIG_MQTT_CLIENT_ID),
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;CLIENT_ID_LEN)];

&amp;#160; &amp;#160; if (strlen(CONFIG_MQTT_CLIENT_ID) &amp;gt; 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; snprintk(client_id, sizeof(client_id), &amp;quot;%s&amp;quot;,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;CONFIG_MQTT_CLIENT_ID);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; goto exit;
&amp;#160; &amp;#160; }

#if defined(CONFIG_NRF_MODEM_LIB)
&amp;#160; &amp;#160; char imei_buf[CGSN_RESPONSE_LENGTH + 1];
&amp;#160; &amp;#160; int err;

&amp;#160; &amp;#160; err = nrf_modem_at_cmd(imei_buf, sizeof(imei_buf), &amp;quot;AT+CGSN&amp;quot;);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Failed to obtain IMEI, error: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; goto exit;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; imei_buf[IMEI_LEN] = &amp;#39;\0&amp;#39;;

&amp;#160; &amp;#160; snprintk(client_id, sizeof(client_id), &amp;quot;nrf-%.*s&amp;quot;, IMEI_LEN, imei_buf);
#else
&amp;#160; &amp;#160; uint32_t id = sys_rand32_get();
&amp;#160; &amp;#160; snprintk(client_id, sizeof(client_id), &amp;quot;%s-%010u&amp;quot;, CONFIG_BOARD, id);
#endif /* !defined(NRF_CLOUD_CLIENT_ID) */

exit:
&amp;#160; &amp;#160; printk(&amp;quot;client_id = %s&amp;quot;, log_strdup(client_id));

&amp;#160; &amp;#160; return client_id;
}

// initialize client for MQTT
static int client_init(struct mqtt_client *client)
{
&amp;#160; &amp;#160; int err;

&amp;#160; &amp;#160; mqtt_client_init(client);

&amp;#160; &amp;#160; err = broker_init();
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Failed to initialize broker connection&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return err;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; /* MQTT client configuration */
&amp;#160; &amp;#160; client-&amp;gt;broker = &amp;amp;broker;
&amp;#160; &amp;#160; client-&amp;gt;evt_cb = mqtt_evt_handler;
&amp;#160; &amp;#160; client-&amp;gt;client_id.utf8 = client_id_get();
&amp;#160; &amp;#160; client-&amp;gt;client_id.size = strlen(client-&amp;gt;client_id.utf8);
&amp;#160; &amp;#160; client-&amp;gt;password = NULL;
&amp;#160; &amp;#160; client-&amp;gt;user_name = NULL;
&amp;#160; &amp;#160; client-&amp;gt;protocol_version = MQTT_VERSION_3_1_1;

&amp;#160; &amp;#160; /* MQTT buffers configuration */
&amp;#160; &amp;#160; client-&amp;gt;rx_buf = rx_buffer;
&amp;#160; &amp;#160; client-&amp;gt;rx_buf_size = sizeof(rx_buffer);
&amp;#160; &amp;#160; client-&amp;gt;tx_buf = tx_buffer;
&amp;#160; &amp;#160; client-&amp;gt;tx_buf_size = sizeof(tx_buffer);

&amp;#160; &amp;#160; /* MQTT transport configuration */
&amp;#160; &amp;#160; #if defined(CONFIG_MQTT_LIB_TLS)
&amp;#160; &amp;#160; &amp;#160; &amp;#160; struct mqtt_sec_config *tls_cfg = &amp;amp;(client-&amp;gt;transport).tls.config;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; static sec_tag_t sec_tag_list[] = { CONFIG_MQTT_TLS_SEC_TAG };

&amp;#160; &amp;#160; &amp;#160; &amp;#160; LOG_INF(&amp;quot;TLS enabled&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; client-&amp;gt;transport.type = MQTT_TRANSPORT_SECURE;

&amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;peer_verify = CONFIG_MQTT_TLS_PEER_VERIFY;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;cipher_count = 0;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;cipher_list = NULL;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;sec_tag_count = ARRAY_SIZE(sec_tag_list);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;sec_tag_list = sec_tag_list;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;hostname = CONFIG_MQTT_BROKER_HOSTNAME;

&amp;#160; &amp;#160; &amp;#160; &amp;#160; #if defined(CONFIG_NRF_MODEM_LIB)
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;session_cache = IS_ENABLED(CONFIG_MQTT_TLS_SESSION_CACHING) ?
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; TLS_SESSION_CACHE_ENABLED :
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; TLS_SESSION_CACHE_DISABLED;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; #else
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; /* TLS session caching is not supported by the Zephyr network stack */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; tls_cfg-&amp;gt;session_cache = TLS_SESSION_CACHE_DISABLED;

&amp;#160; &amp;#160; &amp;#160; &amp;#160; #endif
&amp;#160; &amp;#160; #else
&amp;#160; &amp;#160; &amp;#160; &amp;#160; client-&amp;gt;transport.type = MQTT_TRANSPORT_NON_SECURE;
&amp;#160; &amp;#160; #endif

&amp;#160; &amp;#160; return err;
}

// initialize file descriptor structure that will be used by poll
static int fds_init(struct mqtt_client *c)
{
&amp;#160; &amp;#160; if (c-&amp;gt;transport.type == MQTT_TRANSPORT_NON_SECURE) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; fds.fd = c-&amp;gt;transport.tcp.sock;
&amp;#160; &amp;#160; } else {
#if defined(CONFIG_MQTT_LIB_TLS)
&amp;#160; &amp;#160; &amp;#160; &amp;#160; fds.fd = c-&amp;gt;transport.tls.sock;
#else
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return -ENOTSUP;
#endif
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; fds.events = POLLIN;

&amp;#160; &amp;#160; return 0;
}

// configure modem that provide LTE Link. It will block any information until the connection established successfully.
static int modem_configure(void)
{
&amp;#160; &amp;#160; #if defined(CONFIG_LTE_LINK_CONTROL)
&amp;#160; &amp;#160; &amp;#160; &amp;#160; /* Turn off LTE power saving features for a more responsive demo. Also,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; * request power saving features before network registration. Some
&amp;#160; &amp;#160; &amp;#160; &amp;#160; * networks rejects timer updates after the device has registered to the
&amp;#160; &amp;#160; &amp;#160; &amp;#160; * LTE network.
&amp;#160; &amp;#160; &amp;#160; &amp;#160; */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Disabling PSM and eDRX&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; lte_lc_psm_req(false);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; lte_lc_edrx_req(false);

&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; /* Do nothing, modem is already turned on
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; * and connected.
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; } else {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; #if defined(CONFIG_LWM2M_CARRIER)
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; /* Wait for the LWM2M_CARRIER to configure the modem and
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; * start the connection.
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Waitng for carrier registration...&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; k_sem_take(&amp;amp;carrier_registered, K_FOREVER);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Registered!&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; #else /* defined(CONFIG_LWM2M_CARRIER) */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; int err;

&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Connecting to NB-IOT Network.&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; err = lte_lc_init_and_connect();
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Failed to establish NB-IOT connection: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; %C2%A0 &amp;#160; &amp;#160; return err;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; }
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Connected to the network!&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; #endif /* defined(CONFIG_LWM2M_CARRIER) */
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }
&amp;#160; &amp;#160; #endif /* defined(CONFIG_LTE_LINK_CONTROL) */
&amp;#160; &amp;#160; return 0;
}

// set date time for event handling
static void date_time_evt_handler(const struct date_time_evt *evt)
{
&amp;#160; &amp;#160; k_sem_give(&amp;amp;time_update_finished);
}

// event handling for LTE connection
static void lte_event_handler(const struct lte_lc_evt *const evt)
{
&amp;#160; &amp;#160; switch (evt-&amp;gt;type) {
&amp;#160; &amp;#160; case LTE_LC_EVT_NW_REG_STATUS:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if ((evt-&amp;gt;nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME) ||
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;(evt-&amp;gt;nw_reg_status == LTE_LC_NW_REG_REGISTERED_ROAMING)) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Connected to NB-IOT\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; k_sem_give(&amp;amp;lte_connected);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; default:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; }
}

// get handling location event
static void location_event_handler(const struct location_event_data *event_data)
{
&amp;#160; &amp;#160; switch (event_data-&amp;gt;id) {
&amp;#160; &amp;#160; case LOCATION_EVT_LOCATION:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Got location:\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot; &amp;#160;method: %s\n&amp;quot;, location_method_str(event_data-&amp;gt;location.method));
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot; &amp;#160;latitude: %.06f\n&amp;quot;, event_data-&amp;gt;location.latitude);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot; &amp;#160;longitude: %.06f\n&amp;quot;, event_data-&amp;gt;location.longitude);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot; &amp;#160;accuracy: %.01f m\n&amp;quot;, event_data-&amp;gt;location.accuracy);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot; &amp;#160;Google maps URL: https://maps.google.com/?q=%.06f,%.06f\n\n&amp;quot;,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; event_data-&amp;gt;location.latitude, event_data-&amp;gt;location.longitude);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; char temp_latt[12];
&amp;#160; &amp;#160; &amp;#160; &amp;#160; char temp_long[12];
&amp;#160; &amp;#160; &amp;#160; &amp;#160; char temp_acc[12];
&amp;#160; &amp;#160; &amp;#160; &amp;#160; char result[50];

&amp;#160; &amp;#160; &amp;#160; &amp;#160; gcvt(event_data-&amp;gt;location.latitude, 12, temp_latt);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; latt = atof(temp_latt);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; gcvt(event_data-&amp;gt;location.longitude, 12, temp_long);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; lon = atof(temp_long);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; gcvt(event_data-&amp;gt;location.accuracy, 12, temp_acc);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; acc = atof(temp_acc);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; res = 200;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; sprintf(result, &amp;quot;%f %f %f&amp;quot;, latt, lon, acc);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; int result_len = strlen(result);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(result);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Publishing message to MQTT Broker.&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; data_publish(&amp;amp;client, MQTT_QOS_1_AT_LEAST_ONCE,
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; result, result_len);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Done publishing.&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case LOCATION_EVT_TIMEOUT:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Retrieving location timed out\n\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case LOCATION_EVT_ERROR:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Retrieving location failed\n\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case LOCATION_EVT_GNSS_ASSISTANCE_REQUEST:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Retrieving location assistance requested (A-GPS). Not doing anything.\n\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; case LOCATION_EVT_GNSS_PREDICTION_REQUEST:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Retrieving location assistance requested (P-GPS). Not doing anything.\n\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;

&amp;#160; &amp;#160; default:
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Retrieving location: Unknown event\n\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; k_sem_give(&amp;amp;location_event);
}

static void location_event_wait(void)
{
&amp;#160; &amp;#160; k_sem_take(&amp;amp;location_event, K_FOREVER);
}
&amp;#160;
static void location_with_fallback_get(void)
{
&amp;#160; &amp;#160; int err;
&amp;#160; &amp;#160; struct location_config config;
&amp;#160; &amp;#160; enum location_method methods[] = {LOCATION_METHOD_GNSS, LOCATION_METHOD_CELLULAR};

&amp;#160; &amp;#160; location_config_defaults_set(&amp;amp;config, ARRAY_SIZE(methods), methods);
&amp;#160; &amp;#160; config.methods[0].gnss.timeout = 1;
&amp;#160; &amp;#160; config.methods[1].cellular.timeout = 30;

&amp;#160; &amp;#160; printk(&amp;quot;Requesting location with short GNSS timeout to trigger fallback to cellular...\n&amp;quot;);

&amp;#160; &amp;#160; err = location_request(&amp;amp;config);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Requesting location failed, error: %d\n&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; location_event_wait();
}


static void location_default_get(void)
{
&amp;#160; &amp;#160; int err;

&amp;#160; &amp;#160; printk(&amp;quot;Requesting location with the default configuration...\n&amp;quot;);

&amp;#160; &amp;#160; err = location_request(NULL);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Requesting location failed, error: %d\n&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; location_event_wait();
}

static void location_gnss_low_accuracy_get(void)
{
&amp;#160; &amp;#160; int err;
&amp;#160; &amp;#160; struct location_config config;
&amp;#160; &amp;#160; enum location_method methods[] = {LOCATION_METHOD_GNSS};

&amp;#160; &amp;#160; location_config_defaults_set(&amp;amp;config, ARRAY_SIZE(methods), methods);
&amp;#160; &amp;#160; config.methods[0].gnss.accuracy = LOCATION_ACCURACY_LOW;

&amp;#160; &amp;#160; printk(&amp;quot;Requesting low accuracy GNSS location...\n&amp;quot;);

&amp;#160; &amp;#160; err = location_request(&amp;amp;config);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Requesting location failed, error: %d\n&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; location_event_wait();
}

/**
&amp;#160;* @brief Retrieve location with GNSS high accuracy.
&amp;#160;*/
static void location_gnss_high_accuracy_get(void)
{
&amp;#160; &amp;#160; int err;
&amp;#160; &amp;#160; struct location_config config;
&amp;#160; &amp;#160; enum location_method methods[] = {LOCATION_METHOD_GNSS};

&amp;#160; &amp;#160; location_config_defaults_set(&amp;amp;config, ARRAY_SIZE(methods), methods);
&amp;#160; &amp;#160; config.methods[0].gnss.accuracy = LOCATION_ACCURACY_HIGH;

&amp;#160; &amp;#160; printk(&amp;quot;Requesting high accuracy GNSS location...\n&amp;quot;);

&amp;#160; &amp;#160; err = location_request(&amp;amp;config);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Requesting location failed, error: %d\n&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; location_event_wait();
}

/**
&amp;#160;* @brief Retrieve location periodically with GNSS as first priority and cellular as second.
&amp;#160;*/
static void location_gnss_periodic_get(void)
{
&amp;#160; &amp;#160; int err;
&amp;#160; &amp;#160; printk(&amp;quot;retrieving location.&amp;quot;);
&amp;#160; &amp;#160; struct location_config config;
&amp;#160; &amp;#160; enum location_method methods[] = {LOCATION_METHOD_GNSS, LOCATION_METHOD_CELLULAR};

&amp;#160; &amp;#160; location_config_defaults_set(&amp;amp;config, ARRAY_SIZE(methods), methods);
&amp;#160; &amp;#160; config.interval = 30;

&amp;#160; &amp;#160; printk(&amp;quot;Requesting 30s periodic GNSS location with cellular fallback...\n&amp;quot;);

&amp;#160; &amp;#160; err = location_request(&amp;amp;config);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Requesting location failed, error: %d\n&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return;
&amp;#160; &amp;#160; }
}

int main(void)
{
&amp;#160; &amp;#160; int err;
&amp;#160; &amp;#160; printk(&amp;quot;Starting firmwre.\n&amp;quot;);
&amp;#160; &amp;#160; uint32_t connect_attempt = 0;
&amp;#160; &amp;#160; printk(&amp;quot;checking time settings.\n&amp;quot;);
&amp;#160; &amp;#160; if (IS_ENABLED(CONFIG_DATE_TIME)) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; date_time_register_handler(date_time_evt_handler);
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; do {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;configuring modem.\n&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; err = modem_configure();
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;.&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; k_sleep(K_SECONDS(CONFIG_LTE_CONNECT_RETRY_DELAY_S));
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }
&amp;#160; &amp;#160; } while (err);

&amp;#160; &amp;#160; printk(&amp;quot;Modem Connected.\n&amp;quot;);
&amp;#160; &amp;#160; printk(&amp;quot;Connecting to NB-IOT Signals.\n&amp;quot;);

&amp;#160; &amp;#160; lte_lc_init();
&amp;#160; &amp;#160; lte_lc_register_handler(lte_event_handler);
&amp;#160; &amp;#160; printk(&amp;quot;Connected to NB-IOT Signals.&amp;quot;);

&amp;#160; &amp;#160; /* Enable PSM. */
&amp;#160; &amp;#160; // lte_lc_psm_req(true);
&amp;#160; &amp;#160; // lte_lc_connect();
&amp;#160; &amp;#160; // k_sem_take(&amp;amp;lte_connected, K_FOREVER);
&amp;#160; &amp;#160; // printk(&amp;quot;Enabling PSM.&amp;quot;);

do_connect:
&amp;#160; &amp;#160; if (connect_attempt++ &amp;gt; 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;.&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; k_sleep(K_SECONDS(1));
&amp;#160; &amp;#160; }
&amp;#160; &amp;#160; printk(&amp;quot;\n&amp;quot;);
&amp;#160; &amp;#160; err = mqtt_connect(&amp;amp;client);
&amp;#160; &amp;#160; if (err != 0) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;mqtt connection status : %d\n&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; goto do_connect;
&amp;#160; &amp;#160; }

&amp;#160; &amp;#160; printk(&amp;quot;Connected to MQTT server.\n&amp;quot;);
&amp;#160; &amp;#160; printk(&amp;quot;Starting program..\n&amp;quot;);
&amp;#160; &amp;#160; k_sleep(K_SECONDS(CONFIG_MQTT_RECONNECT_DELAY_S));

&amp;#160; &amp;#160; while (1) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;re-doing program&amp;quot;);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; err = location_init(location_event_handler);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Initializing the Location library failed, error: %d\n&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; return -1;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; err = mqtt_live(&amp;amp;client);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; if ((err != 0) &amp;amp;&amp;amp; (err != -EAGAIN)) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;ERROR: mqtt_live: %d&amp;quot;, err);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; break;
&amp;#160; &amp;#160; &amp;#160; &amp;#160; }

&amp;#160; &amp;#160; &amp;#160; &amp;#160; location_with_fallback_get();
&amp;#160; &amp;#160; &amp;#160; &amp;#160; location_default_get();
&amp;#160; &amp;#160; &amp;#160; &amp;#160; location_gnss_low_accuracy_get();
&amp;#160; &amp;#160; &amp;#160; &amp;#160; location_gnss_high_accuracy_get();
&amp;#160; &amp;#160; &amp;#160; &amp;#160; location_gnss_periodic_get();
&amp;#160; &amp;#160; }
&amp;#160; &amp;#160; err = mqtt_disconnect(&amp;amp;client);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; printk(&amp;quot;Could not disconnect MQTT.&amp;quot;);
&amp;#160; &amp;#160; }
&amp;#160; &amp;#160; goto do_connect;
}&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Thanks.&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MQTT Connection Status -22</title><link>https://devzone.nordicsemi.com/thread/381903?ContentTypeID=1</link><pubDate>Wed, 17 Aug 2022 07:51:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d2705e8d-973e-4f7f-a9a3-1ce7d39bcb73</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Hello!&lt;/p&gt;
&lt;p&gt;Error 22 means &amp;quot;invalid argument&amp;quot;:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-nrfxlib/blob/main/nrf_modem/include/nrf_errno.h"&gt;https://github.com/nrfconnect/sdk-nrfxlib/blob/main/nrf_modem/include/nrf_errno.h&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I would recommend checking the inputs you&amp;#39;re giving to the function that returns this error.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>