<?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>LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89628/log-freeze-at-mqtt</link><description>Using a NRF9160DK. SDK2.0.0. Modem 1.3.2. 
 
 I have a problem when setting up a new NRF board to run with MQTT with AWS. 
 
 When the setup is running, I get LOG &amp;lt;inf&amp;gt; for modem connect and the broker IP and then when mqtt_connect is run there are no</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 11 Jul 2022 13:27:07 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89628/log-freeze-at-mqtt" /><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/376445?ContentTypeID=1</link><pubDate>Mon, 11 Jul 2022 13:27:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c5d9c189-7612-47a5-88ad-df28a14be85a</guid><dc:creator>Jensenius</dc:creator><description>&lt;p&gt;That worked! Thank you so much. Was driving me mad.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/376431?ContentTypeID=1</link><pubDate>Mon, 11 Jul 2022 12:47:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2b804b8a-b4be-4422-b803-fe73f062d499</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;It seems like your empty main loop could potentially be the cause of this issue.&lt;/p&gt;
&lt;p&gt;I would recommend adding a sleep to the main function, for instance by calling k_sleep(K_FOREVER) if you&amp;#39;re not going to do anything else in the main function, to let your other threads run.&lt;/p&gt;
&lt;p&gt;This seems to have solved the issue on our side, please let me know if this solves it for you as well,&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/376296?ContentTypeID=1</link><pubDate>Sat, 09 Jul 2022 19:06:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ff2d0ed7-218d-4fcc-8cb2-06669d90639d</guid><dc:creator>Jensenius</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/acdc.zip"&gt;devzone.nordicsemi.com/.../acdc.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Git do not want to play nice. Here the project is as a zip.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The read me is out of date, because I have migrated it from the make.bat to ncs connect via&amp;nbsp; vs.code. (hopeing it would fix the problem) I uses sdk 2.0.0 and builds to a _ns nrf9160dk.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/376180?ContentTypeID=1</link><pubDate>Fri, 08 Jul 2022 11:16:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c7c7f08f-3379-48b0-a8c4-570164dcba9d</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Hm I see, could you please share your git repo, it would be easier for us to try running the code locally that way.&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375974?ContentTypeID=1</link><pubDate>Thu, 07 Jul 2022 09:39:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8eeec65a-e716-49e2-9d79-81cb364cb7db</guid><dc:creator>Jensenius</dc:creator><description>&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1657186713666v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;This is what the terminal respons with. The log is still not responding on events hapening after.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375973?ContentTypeID=1</link><pubDate>Thu, 07 Jul 2022 09:37:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ee6f98a4-fc42-40a3-a1ed-d3748d6bc13f</guid><dc:creator>Jensenius</dc:creator><description>&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1657186572098v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;This is what I get in return. It still wont do anything after this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375965?ContentTypeID=1</link><pubDate>Thu, 07 Jul 2022 09:20:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:124c4b47-cf49-4f0b-ab1a-02e0899db7da</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Thank you!&lt;/p&gt;
&lt;p&gt;Could you try adding these lines to your prj.conf file:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;CONFIG_NET_LOG=y
CONFIG_MQTT_LOG_LEVEL_DBG=y&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375788?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 12:27:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:77713eac-ab93-412f-855f-6a6e67a65a4f</guid><dc:creator>Jensenius</dc:creator><description>&lt;p&gt;It is at aws.c by the 536 the log stops. when mqtt_connect is called.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The prj.conf file&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;# Logging interface
CONFIG_LOG=y

# Stacks and heaps
CONFIG_HEAP_MEM_POOL_SIZE=16384 
CONFIG_MAIN_STACK_SIZE=16384
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096

# UART interface
CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y

# cJSON interface
CONFIG_CJSON_LIB=y

# AT host interface
CONFIG_AT_HOST_LIBRARY=y

# Date time interface
CONFIG_DATE_TIME=y

# Use GPIO
CONFIG_GPIO=y
CONFIG_GPIO_NRFX=y
CONFIG_NRFX_GPIOTE=y

# MQTT interface
CONFIG_MQTT_LIB=y
CONFIG_MQTT_LIB_TLS=y
CONFIG_MQTT_CLEAN_SESSION=y

# Network interface
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_NATIVE=n

# Library for the LTE network
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
CONFIG_LTE_NETWORK_MODE_LTE_M=y
CONFIG_MODEM_KEY_MGMT=y
CONFIG_NRF_MODEM_LIB=y

#Enable reboot from software
CONFIG_REBOOT=y

# newlibc
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

# Use PWM
CONFIG_PWM=y
CONFIG_PWM_NRFX=y

# use ADC
CONFIG_ADC=y
CONFIG_ADC_NRFX_SAADC=y

# USE I2C
CONFIG_I2C=y
CONFIG_I2C_NRFX=y

# WatchDog interface
CONFIG_WATCHDOG=y
CONFIG_WDT_DISABLE_AT_BOOT=n

# Pin control
CONFIG_PINCTRL=y
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The main file:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;kernel.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;

#include &amp;quot;modem.h&amp;quot;
#include &amp;quot;scheduler.h&amp;quot;
#include &amp;quot;https.h&amp;quot;
#include &amp;quot;cloud.h&amp;quot;
#include &amp;quot;aws.h&amp;quot;
#include &amp;quot;watch.h&amp;quot;

/**
 * @brief Setting up the initializing functions.
 */
 void configure_system_on_start_up(void) {
     // Set the modem client id.
    modem_set_client_id();
    
    // Initialize modem module.
    modem_init();

    // Initialize the cloud.
    cloud_init(modem_get_client_id());

    // Initialize the watch.
    watch_init();

    // Set time offset.
    watch_set_offset(1);

    // Initialize the scheduler.
    scheduler_init();

    // Connect the modem to LTE network.
    modem_connect();

    // Connect to AWS.
    aws_connect();

    // Synchronize the watch.
    watch_update();
    k_sleep(K_MSEC(500));

 }

/**
 * @brief The application main entry.
 */
void main(void) {

    // Initialize system.
    configure_system_on_start_up();

    // Enter main loop.
    while(1) {

        // Run client.
        /*CLI_RUN();

        // Update the outlet current.
        va_update_outlet_current();

        // Run through the outlets.
        for(uint8_t i = 0; i &amp;lt;= number_of_outlets; i++) {

            // Update state machine.
            state_update(i);
        }

        // Feed the dog.
        wdt_feed_dog();*/
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Modem.h&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#ifndef MODEM_H_
#define MODEM_H_

/**
 * @brief   Initialize the modem.
 */
void modem_init(void);

/**
 * @brief   Connect the modem to the LTE network.
 */
void modem_connect(void);

/**
 * @brief   Disconnect the modem from the LTE network.
 */
void modem_disconnect(void);

/**
 * @brief   Set the client id.
 */
void modem_set_client_id(void);

/**
 * @brief   Get the client ID.
 *
 * @return  The client ID.
 */
const uint8_t* modem_get_client_id(void);

#endif /* MODEM_H_ */&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The modem.c file:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;logging/log.h&amp;gt;
#include &amp;lt;modem/lte_lc.h&amp;gt;
#include &amp;lt;modem/modem_key_mgmt.h&amp;gt;
#include &amp;lt;nrf_modem_at.h&amp;gt;


//! Register the modem module to the logging system.
LOG_MODULE_REGISTER(Modem, 3);

//! The modem connection status flag.
static bool is_connected;

//! An array holding the client id.
static uint8_t client_id[20];

void modem_init(void) {

    int error;

    // Turn off the psm mode.
    error = lte_lc_psm_req(false);

    if(error) {

        LOG_ERR(&amp;quot;Failed to turn off PSM mode, error: %d.&amp;quot;, error);

        return;
    }

    // Turn off the edrx mode.
    error = lte_lc_edrx_req(false);

    if(error) {

        LOG_ERR(&amp;quot;Failed to turn off EDRX mode, error: %d.&amp;quot;, error);

        return;
    }

    // Initialize the LTE network.
    error = lte_lc_init();

    if(error) {

        LOG_ERR(&amp;quot;Failed to initialize the modem, error: %d.&amp;quot;, error);

        return;
    }
}

void modem_set_client_id(void) {

    // Fetch the IMEI number from the modem.
    char imei_buffer[26];
    modem_write_at_command(&amp;quot;AT+CGSN&amp;quot;, imei_buffer, sizeof(imei_buffer));

    // Add null terminator to the IMEI number.
    imei_buffer[15] = &amp;#39;\0&amp;#39;;

    // Create the client id.
    snprintf(client_id, sizeof(client_id), &amp;quot;acdc-%.*s&amp;quot;, 15, imei_buffer);

    LOG_INF(&amp;quot;Client ID: %s.&amp;quot;, log_strdup(client_id));
}

const uint8_t* modem_get_client_id(void) {

    return client_id;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;aws.h&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#ifndef AWS_H_
#define AWS_H_

#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;

#include &amp;quot;software_settings.h&amp;quot;

/**
 * @brief   Subscribe data used when packets are published on AWS.
 */
typedef struct {

    bool is_subscribed;             //!&amp;lt; Flag indicating if the topic is subscribed.
    char* topic;                    //!&amp;lt; The subscribe/publish topic.
    char* failure_message;          //!&amp;lt; The payload message when failed on AWS.
    char* success_message;          //!&amp;lt; The payload message when succeeded on AWS.
    char* qos;                      //!&amp;lt; The Quality of Service.

} aws_mqtt_status_t;

//! Event identifiers used by the @ref aws_event_callback_t.
typedef enum  {

    AWS_EVENT_NONE,                 //!&amp;lt; This ID is never used. Dummy value for completeness.
    AWS_EVENT_RESPONSE_READY,       //!&amp;lt; A complete AWS response packet is ready.

} aws_event_t;

/**
 * @brief   AWS publish information.
 */
typedef struct {

    char* topic;                    //!&amp;lt; The topic that is published to.
    uint8_t* data;                  //!&amp;lt; The data to publish.

} aws_publish_info_t;

//! Cloud event callback function type.
typedef void (*aws_event_callback_t)(const aws_event_t, const uint8_t* const);

/**
 * @brief   Initialize the AWS and MQTT broker.
 *
 * @param   _client_id  The client id.
 * @param   _callback   Function to be called when a aws event is detected.
 */
void aws_init(const uint8_t* const _client_id, const aws_event_callback_t _callback);

/**
 * @brief   Connect to the AWS MQTT broker.
 */
void aws_connect(void);

/**
 * @brief   Disconnect from the AWS MQTT broker.
 */
void aws_disconnect(void);

/**
 * @brief   Verify if the AWS MQTT broker connection is requested.
 *
 * @retval  false   MQTT broker connection is not requested.
 * @retval  true    MQTT broker connection is requested.
 */
bool aws_is_connection_requested(void);

/**
 * @brief   Run for the AWS MQTT broker events and inputs.
 *          This function shall only be called inside a thread.
 */
void aws_run(void);

/**
 * @brief   Subscribe to the configured topic.
 *
 * @param   _subscribe_topic    The topic that shall be subscribed to.
 *
 * @retval  0           Success.
 * @retval  Negative    Error.
 */
int16_t aws_subscribe(const char* const _subscribe_topic);

/**
 * @brief   Publish data on the configured topic.
 *
 * @note    The functionality is handled through a work queue.
 *
 * @param   _topic      The topic that the data shall be published to.
 * @param   _data       The data that shall be published.
 */
void aws_publish(const char* const _topic, const uint8_t* const _data);

/**
 * @brief   Get the MQTT status.
 *
 * @param   _aws_mqtt_status    The MQTT status.
 */
void aws_get_status(aws_mqtt_status_t* _aws_mqtt_status);

#endif /* AWS_H_ */

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;aws.c file: (notice defines for broker address and port)&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdio.h&amp;gt;

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;logging/log.h&amp;gt;
#include &amp;lt;net/mqtt.h&amp;gt;
#include &amp;lt;net/socket.h&amp;gt;
#include &amp;lt;random/rand32.h&amp;gt;

#include &amp;quot;aws.h&amp;quot;
#include &amp;quot;watch.h&amp;quot;

//! Register the AWS module to the logging system.
LOG_MODULE_REGISTER(AWS, 3);

//! The MQTT broker address details structure.
static struct sockaddr_storage mqtt_broker;

//! The MQTT polling file descriptor structure.
static struct pollfd mqtt_pollfd;

//! The MQTT client structure.
static struct mqtt_client client;

//! The MQTT publish mutex structure.
static struct k_mutex aws_publish_mutex;

//! The MQTT publish worker structure.
static struct k_work aws_publish_work;

//! The AWS callback function.
static aws_event_callback_t aws_callback = NULL;

//! The MQTT status when data is published on AWS.
static aws_mqtt_status_t aws_mqtt_status;

//! The MQTT client RX buffer.
static uint8_t mqtt_rx_buffer[8192];

//! The MQTT client TX buffer.
static uint8_t mqtt_tx_buffer[8192];

//! The MQTT payload buffer.
static uint8_t mqtt_payload_buffer[8192];

//! The MQTT broker connection requested flag.
static atomic_t is_connect_requested;

//! The AWS publish information.
static aws_publish_info_t aws_publish_info;

#define BROKER_HOSTNAME &amp;quot;xxxxxx&amp;quot;
#define MQTT_BROKER_PORT 8883

/**
 * @brief   Publish data to the AWS (Work).
 *
 * @param   work     The k_work structure.
 */
static void aws_publish_work_handler(struct k_work* work) {

    ARG_UNUSED(work);

    // Lock the AWS publish mutex and has it succeeded?
    if(0 != k_mutex_lock(&amp;amp;aws_publish_mutex, K_MSEC(1000U))) {

        LOG_ERR(&amp;quot;AWS publish mutex cannot be locked.&amp;quot;);

        return;
    }

    // The parameters for the published message.
    struct mqtt_publish_param mqtt_param;
    memset(&amp;amp;mqtt_param, 0, sizeof(mqtt_param));

    // Fill out the parameters.
    mqtt_param.message.topic.qos = MQTT_QOS_1_AT_LEAST_ONCE;
    mqtt_param.message.topic.topic.utf8 = aws_publish_info.topic;
    mqtt_param.message.topic.topic.size = strlen(aws_publish_info.topic);
    mqtt_param.message.payload.data = aws_publish_info.data;
    mqtt_param.message.payload.len = strlen(aws_publish_info.data);
    mqtt_param.message_id = sys_rand32_get();
    mqtt_param.dup_flag = 0;
    mqtt_param.retain_flag = 0;

    LOG_INF(&amp;quot;MQTT published to topic: %s, length: %u.&amp;quot;, log_strdup(aws_publish_info.topic), strlen(aws_publish_info.data));

    // Publish the message to the topic.
    int16_t error = mqtt_publish(&amp;amp;client, &amp;amp;mqtt_param);

    if(0 &amp;gt; error) {

        LOG_ERR(&amp;quot;Publish data failed: %d.&amp;quot;, error);
    }

    // Unlock the AWS publish mutex.
    (void)k_mutex_unlock(&amp;amp;aws_publish_mutex);
}

/**
 * @brief   Get the published MQTT payload.
 *
 * @param   _c          The MQTT client.
 * @param   _length     The length of the payload.
 *
 * @retval  0           Success.
 * @retval  Negative    Error.
 */
static int16_t aws_mqtt_publish_get_payload(struct mqtt_client* const _c, const size_t _length) {

    // Is the published payload too long?
    if(sizeof(mqtt_payload_buffer) &amp;lt; _length) {

        return -EMSGSIZE;
    }

    // Read all the published payload.
    return mqtt_readall_publish_payload(_c, mqtt_payload_buffer, _length);
}

/**
 * @brief   Handle the MQTT client events (Callback).
 *
 * @param   c       The MQTT client.
 * @param   evt     The MQTT event data.
 */
static void aws_mqtt_event_handler(struct mqtt_client* const c, const struct mqtt_evt* evt) {

    int16_t error;

    // Reset the AWS event.
    uint8_t event = AWS_EVENT_NONE;

    // Go through the event type.
    switch(evt-&amp;gt;type) {

        // Acknowledgement of connection request.
        case MQTT_EVT_CONNACK:

            // Has an error occurred?
            if(0 != evt-&amp;gt;result) {

                LOG_ERR(&amp;quot;MQTT connection failed: %d.&amp;quot;, evt-&amp;gt;result);

                break;
            }

            LOG_INF(&amp;quot;MQTT client is connected.&amp;quot;);

            // Subscribe the status topic to AWS.
            error = aws_subscribe(aws_mqtt_status.topic);

            // Has an error occurred?
            if(0 &amp;gt; error) {

                LOG_ERR(&amp;quot;Subscribe Status failed: %d.&amp;quot;, error);
            }

            // Or has it succeeded?
            else {

                LOG_INF(&amp;quot;Subscribe Status succeeded.&amp;quot;);
            }

            break;

        // Disconnection event.
        case MQTT_EVT_DISCONNECT:

            LOG_INF(&amp;quot;MQTT client is disconnected.&amp;quot;);

            atomic_set(&amp;amp;is_connect_requested, 0);

            break;

        // Publish event received when message is published on a topic client is subscribed to.
        case MQTT_EVT_PUBLISH: {

            // Read the parameters for the publish message.
            const struct mqtt_publish_param* p = &amp;amp;evt-&amp;gt;param.publish;

            LOG_INF(&amp;quot;MQTT PUBLISH result = %d, length = %d.&amp;quot;, evt-&amp;gt;result, p-&amp;gt;message.payload.len);

            // Is QoS set to MQTT_QOS_1_AT_LEAST_ONCE?
            if(MQTT_QOS_1_AT_LEAST_ONCE == p-&amp;gt;message.topic.qos) {

                // Prepare the QoS1 ack message.
                const struct mqtt_puback_param ack = {

                    .message_id = p-&amp;gt;message_id
                };

                // Send the ack message.
                error = mqtt_publish_qos1_ack(&amp;amp;client, &amp;amp;ack);

                // Has an error occurred?
                if(0 != error) {

                    LOG_ERR(&amp;quot;MQTT publish QoS1 ack failed: %d.&amp;quot;, error);
                }
            }

            // Is QoS set to MQTT_QOS_2_EXACTLY_ONCE?
            else if(MQTT_QOS_2_EXACTLY_ONCE == p-&amp;gt;message.topic.qos) {

                // Prepare the QoS2 receive message.
                const struct mqtt_pubrec_param receive = {

                    .message_id = p-&amp;gt;message_id
                };

                // Send the receive message.
                error = mqtt_publish_qos2_receive(&amp;amp;client, &amp;amp;receive);

                // Has an error occurred?
                if(0 != error) {

                    LOG_ERR(&amp;quot;MQTT publish QoS2 receive failed: %d.&amp;quot;, error);
                }
            }

            // Read the published MQTT payload.
            error = aws_mqtt_publish_get_payload(c, p-&amp;gt;message.payload.len);

            // Has an error occurred?
            if(0 != error) {

                LOG_ERR(&amp;quot;MQTT PUBLISH payload failed: %d.&amp;quot;, error);

                break;
            }

            // Has the payload been read successfully?
            LOG_INF(&amp;quot;MQTT PUBLISH topic: %s.&amp;quot;, log_strdup(p-&amp;gt;message.topic.topic.utf8));

            // Has an AWS status topic occurred?
            if(0 == strcmp(p-&amp;gt;message.topic.topic.utf8, aws_mqtt_status.topic)) {

                // Set the AWS response ready event.
                event = AWS_EVENT_RESPONSE_READY;
            }

            // Or is the response unknown?
            else {

                LOG_INF(&amp;quot;MQTT PUBLISH response is unknown.&amp;quot;);
            }

            break;
        }

        // Acknowledgement for published message with QoS 1.
        case MQTT_EVT_PUBACK:

            // Has an error occurred?
            if(0 != evt-&amp;gt;result) {

                LOG_ERR(&amp;quot;MQTT PUBACK received error: %d.&amp;quot;, evt-&amp;gt;result);

                break;
            }

            LOG_INF(&amp;quot;MQTT PUBACK received with id: %u.&amp;quot;, evt-&amp;gt;param.puback.message_id);

            break;

        // Reception confirmation for published message with QoS 2.
        case MQTT_EVT_PUBREC: {

            // Has an error occurred?
            if(0 != evt-&amp;gt;result) {

                LOG_ERR(&amp;quot;MQTT QoS2 PUBREC received error: %d.&amp;quot;, evt-&amp;gt;result);

                break;
            }

            // Read the parameters for the pubrec message.
            const struct mqtt_pubrec_param* p = &amp;amp;evt-&amp;gt;param.pubrec;

            // Prepare the release message.
            const struct mqtt_pubrel_param release = {

                .message_id = p-&amp;gt;message_id
            };

            // Send the release message.
            error = mqtt_publish_qos2_release(&amp;amp;client, &amp;amp;release);

            // Has an error occurred?
            if(0 != error) {

                LOG_ERR(&amp;quot;MQTT publish QoS2 release failed: %d.&amp;quot;, error);

                break;
            }

            LOG_INF(&amp;quot;MQTT PUBREC received with id: %u.&amp;quot;, p-&amp;gt;message_id);

            break;
        }

        // Release of published message with QoS 2.
        case MQTT_EVT_PUBREL: {

            // Has an error occurred?
            if(0 != evt-&amp;gt;result) {

                LOG_ERR(&amp;quot;MQTT QoS2 PUBREL received error: %d.&amp;quot;, evt-&amp;gt;result);

                break;
            }

            // Read the parameters for the pubrel message.
            const struct mqtt_pubrel_param* p = &amp;amp;evt-&amp;gt;param.pubrel;

            // Prepare the complete message.
            const struct mqtt_pubcomp_param complete = {

                .message_id = p-&amp;gt;message_id
            };

            // Send the complete message.
            error = mqtt_publish_qos2_complete(&amp;amp;client, &amp;amp;complete);

            // Has an error occurred?
            if(0 != error) {

                LOG_ERR(&amp;quot;MQTT publish QoS2 complete failed: %d.&amp;quot;, error);

                break;
            }

            LOG_INF(&amp;quot;MQTT PUBREL received with id: %u.&amp;quot;, p-&amp;gt;message_id);

            break;
        }

        // Confirmation to a publish release message with QoS 2.
        case MQTT_EVT_PUBCOMP: {

            // Has an error occurred?
            if(0 != evt-&amp;gt;result) {

                LOG_ERR(&amp;quot;MQTT PUBCOMP received error: %d.&amp;quot;, evt-&amp;gt;result);

                break;
            }

            LOG_INF(&amp;quot;MQTT PUBCOMP received with id: %u.&amp;quot;, evt-&amp;gt;param.pubcomp.message_id);

            break;
        }

        // Acknowledgement to a subscribe request.
        case MQTT_EVT_SUBACK:

            // Has an error occurred?
            if(0 != evt-&amp;gt;result) {

                LOG_ERR(&amp;quot;MQTT SUBACK received error: %d.&amp;quot;, evt-&amp;gt;result);

                break;
            }

            LOG_INF(&amp;quot;MQTT SUBACK received with id: %u.&amp;quot;, evt-&amp;gt;param.suback.message_id);

            aws_mqtt_status.is_subscribed = true;

            break;

        // Acknowledgment to a unsubscribe request.
        case MQTT_EVT_UNSUBACK:

            // Has an error occurred?
            if(0 != evt-&amp;gt;result) {

                LOG_ERR(&amp;quot;MQTT UNSUBACK received error: %d.&amp;quot;, evt-&amp;gt;result);

                break;
            }

            LOG_INF(&amp;quot;MQTT UNSUBACK received with id: %u.&amp;quot;, evt-&amp;gt;param.unsuback.message_id);

            aws_mqtt_status.is_subscribed = false;

            break;

        // Ping Response from server.
        case MQTT_EVT_PINGRESP:

            break;

        default:

            break;
    }

    // Has a AWS callback been set?
    if((AWS_EVENT_NONE != event) &amp;amp;&amp;amp; (NULL != aws_callback)) {

        // Set the callback function.
        aws_callback(event, mqtt_payload_buffer);
    }
}

/**
 * @brief   Initialize the MQTT broker structure.
 */
static void aws_broker_init(void) {

    // Fill out the initial address info struct.
    struct addrinfo hints = {

        .ai_flags = 0,
        .ai_family = AF_INET,
        .ai_socktype = SOCK_STREAM,
        .ai_protocol = 0,
    };

    // The resulting address info struct.
    struct addrinfo* result;
    struct addrinfo* address;

    // Convert text strings representing hostnames/IP addresses into a linked list of struct addrinfo structure.
    int error = getaddrinfo(BROKER_HOSTNAME, NULL, &amp;amp;hints, &amp;amp;result);

    // Did an error occur?
    if(0 != error) {

        LOG_ERR(&amp;quot;Getaddrinfo failed, error: %d.&amp;quot;, errno);

        return;
    }

    // Store the resulting address info.
    address = result;

    // Look for the IPv4 Address of the broker.
    while(NULL != address) {

        // Is it an IPv4 Address?
        if(sizeof(struct sockaddr_in) == address-&amp;gt;ai_addrlen) {

            // Initialize the broker structure.
            struct sockaddr_in* broker = ((struct sockaddr_in*)&amp;amp;mqtt_broker);

            // Fill out the socket IPv4 address for the broker.
            broker-&amp;gt;sin_addr.s_addr = ((struct sockaddr_in*)address-&amp;gt;ai_addr)-&amp;gt;sin_addr.s_addr;
            broker-&amp;gt;sin_family = AF_INET;
            broker-&amp;gt;sin_port = htons(MQTT_BROKER_PORT);

            // An array holding the IP address.
            char ipv4_address[NET_IPV4_ADDR_LEN];

            // Convert the IP address from internal to numeric ASCII form.
            inet_ntop(AF_INET, &amp;amp;broker-&amp;gt;sin_addr.s_addr, ipv4_address, sizeof(ipv4_address));

            LOG_INF(&amp;quot;MQTT broker is connected with IP address: %s.&amp;quot;, log_strdup(ipv4_address));

            break;
        }

        // Increment the address pointer.
        address = address-&amp;gt;ai_next;
    }

    // Free the resulting memory struct.
    freeaddrinfo(result);
}

void aws_init(const uint8_t* const _client_id, const aws_event_callback_t _callback) {

    // Set the callback function.
    aws_callback = _callback;

    // Initialize the MQTT client instance.
    mqtt_client_init(&amp;amp;client);

    // MQTT client configuration.
    client.broker = &amp;amp;mqtt_broker;
    client.evt_cb = aws_mqtt_event_handler;
    client.client_id.utf8 = _client_id;
    client.client_id.size = strlen(client.client_id.utf8);
    client.password = NULL;
    client.user_name = NULL;
    client.protocol_version = MQTT_VERSION_3_1_1;

    // MQTT buffers configuration.
    client.rx_buf = mqtt_rx_buffer;
    client.rx_buf_size = sizeof(mqtt_rx_buffer);
    client.tx_buf = mqtt_tx_buffer;
    client.tx_buf_size = sizeof(mqtt_tx_buffer);

    // MQTT transport configuration.
    client.transport.type = MQTT_TRANSPORT_SECURE;

    // The security tag.
    static sec_tag_t sec_tag_list[] = { CC_MODEM_CERTIFICATE_SEC_TAG };

    // TLS configuration for secure MQTT transports.
    struct mqtt_sec_config* tls_config = &amp;amp;(client.transport).tls.config;
    tls_config-&amp;gt;peer_verify = 2;
    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);
    tls_config-&amp;gt;sec_tag_list = sec_tag_list;
    tls_config-&amp;gt;hostname = BROKER_HOSTNAME;
    tls_config-&amp;gt;session_cache = TLS_SESSION_CACHE_DISABLED;

    // Fill out the MQTT status topic struct.
    aws_mqtt_status.is_subscribed = false;
    aws_mqtt_status.topic = (uint8_t*)_client_id;
    aws_mqtt_status.failure_message = &amp;quot;FAILURE&amp;quot;;
    aws_mqtt_status.success_message = &amp;quot;SUCCESS&amp;quot;;
    aws_mqtt_status.qos = &amp;quot;1&amp;quot;;

    // Initialize a mutex.
    k_mutex_init(&amp;amp;aws_publish_mutex);

    // Initialize a work queue.
    k_work_init(&amp;amp;aws_publish_work, aws_publish_work_handler);
}

void aws_connect(void) {

    // Is the connection already requested?
    if(1 == atomic_get(&amp;amp;is_connect_requested)) {

        return;
    }

    // Initialize the MQTT broker.
    aws_broker_init();

    // Connect to the MQTT broker.
    int error = mqtt_connect(&amp;amp;client);

    // Has an error occurred?
    if(0 != error) {

        LOG_ERR(&amp;quot;MQTT connection failed: %d.&amp;quot;, error);

        return;
    }

    // Set the MQTT transport to secure.
    mqtt_pollfd.fd = client.transport.tls.sock;
    mqtt_pollfd.events = POLLIN;

    // The connection is now requested.
    atomic_set(&amp;amp;is_connect_requested, 1);
}

// cppcheck-suppress    unusedFunction
void aws_disconnect(void) {

    // Is the connection not requested?
    if(0 == atomic_get(&amp;amp;is_connect_requested)) {

        return;
    }

    // Disconnect from the MQTT broker.
    int error = mqtt_disconnect(&amp;amp;client);

    // Has an error occurred?
    if(0 != error) {

        LOG_ERR(&amp;quot;MQTT disconnection failed: %d.&amp;quot;, error);
    }
}

bool aws_is_connection_requested(void) {

    return (bool)atomic_get(&amp;amp;is_connect_requested);
}

void aws_run(void) {

    // Wait on an event on the file descriptor.
    int error = poll(&amp;amp;mqtt_pollfd, 1,  mqtt_keepalive_time_left(&amp;amp;client));

    // Has an error occurred?
    if(0 &amp;gt; error) {

        LOG_ERR(&amp;quot;MQTT poll failed: %d.&amp;quot;, errno);

        return;
    }

    // Keep the connection alive by sending a ping request.
    error = mqtt_live(&amp;amp;client);

    // Has an error occurred?
    if((0 != error) &amp;amp;&amp;amp; (-EAGAIN != error)) {

        LOG_ERR(&amp;quot;MQTT live failed: %d.&amp;quot;, error);

        return;
    }

    // Is there data to read (returned event)?
    if(POLLIN == (mqtt_pollfd.revents &amp;amp; POLLIN)) {

        // Receive an incoming MQTT packet. The registered callback will be called with the packet content.
        error = mqtt_input(&amp;amp;client);

        // Has an error occurred?
        if(0 != error) {

            LOG_ERR(&amp;quot;MQTT input failed: %d.&amp;quot;, error);

            return;
        }
    }

    // Does the returned event contain an error condition?
    if(POLLERR == (mqtt_pollfd.revents &amp;amp; POLLERR)) {

        LOG_ERR(&amp;quot;MQTT failed with POLLERR.&amp;quot;);

        return;
    }

    // Does the returned event contain an invalid request?
    if(POLLNVAL == (mqtt_pollfd.revents &amp;amp; POLLNVAL)) {

        LOG_ERR(&amp;quot;MQTT failed with POLLNVAL.&amp;quot;);

        return;
    }
}

int16_t aws_subscribe(const char* const _subscribe_topic) {

    // Is the topic already subscribed?
    if(true == aws_mqtt_status.is_subscribed) {

        return 0;
    }

    // Create the subscribed topic.
    struct mqtt_topic topic = {

        .topic = {

            .utf8 = _subscribe_topic,
            .size = strlen(_subscribe_topic)
        },

        .qos = MQTT_QOS_1_AT_LEAST_ONCE
    };

    // Add the topic to the subscription request.
    const struct mqtt_subscription_list subscription_list = {

        .list = &amp;amp;topic,
        .list_count = 1,
        .message_id = sys_rand32_get()
    };

    LOG_INF(&amp;quot;MQTT subscribed to topic: %s&amp;quot;, log_strdup(_subscribe_topic));

    // Subscribe to the topic.
    return mqtt_subscribe(&amp;amp;client, &amp;amp;subscription_list);
}

void aws_publish(const char* const _topic, const uint8_t* const _data) {

    // Lock the AWS publish mutex and has it succeeded?
    if(0 != k_mutex_lock(&amp;amp;aws_publish_mutex, K_MSEC(CC_WORK_MUTEX_LOCK_TIMEOUT_MS))) {

        LOG_ERR(&amp;quot;AWS publish mutex cannot be locked.&amp;quot;);

        return;
    }

    // Store the AWS publish information.
    aws_publish_info.topic = (char*)_topic;
    aws_publish_info.data = (uint8_t*)_data;

    // Unlock the AWS publish mutex.
    (void)k_mutex_unlock(&amp;amp;aws_publish_mutex);

    // Submit a work item to the system work queue.
    (void)k_work_submit(&amp;amp;aws_publish_work);
}

void aws_get_status(aws_mqtt_status_t* _aws_mqtt_status) {

    // Get the AWS MQTT status struct.
    memcpy(_aws_mqtt_status, &amp;amp;aws_mqtt_status, sizeof(aws_mqtt_status));
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;cloud.h&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#ifndef CLOUD_H_
#define CLOUD_H_

//! Cloud topic types.
typedef enum &amp;#160;{

&amp;#160; &amp;#160; CLOUD_TOPIC_TYPE_GET_PCC_LOAD, &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;//!&amp;lt; The &amp;quot;Get PCC load&amp;quot; type.
&amp;#160; &amp;#160; CLOUD_TOPIC_NUMBER_OF_TYPES &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; //!&amp;lt; The number of types.

} cloud_topic_type_t;

//! Event identifiers used by the @ref cloud_event_callback_t.
typedef enum &amp;#160;{

&amp;#160; &amp;#160; CLOUD_EVENT_NONE, &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;lt; This ID is never used. Dummy value for completeness.
&amp;#160; &amp;#160; CLOUD_EVENT_GET_PCC_LOAD_PACKET_SUCCEEDED, &amp;#160; &amp;#160; &amp;#160;//!&amp;lt; A complete &amp;quot;Get PCC Load&amp;quot; packet has succeeded.
&amp;#160; &amp;#160; CLOUD_EVENT_GET_PCC_LOAD_PACKET_FAILED &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;//!&amp;lt; A complete &amp;quot;Get PCC load&amp;quot; packet has failed.

} cloud_event_t;

/**
&amp;#160;* @brief &amp;#160; Cloud publish information.
&amp;#160;*/
typedef struct {

&amp;#160; &amp;#160; char* topic; &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; &amp;#160; &amp;#160;//!&amp;lt; The topic that is published to.
&amp;#160; &amp;#160; uint8_t* data; &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; &amp;#160;//!&amp;lt; The data to publish.

} cloud_publish_info_t;

//! Event callback function type.
typedef void (*cloud_event_callback_t)(const cloud_event_t);

/**
&amp;#160;* @brief &amp;#160; Initialize the cloud.
&amp;#160;*
&amp;#160;* @param &amp;#160; _client_id &amp;#160; &amp;#160; &amp;#160;The client id.
&amp;#160;*/
void cloud_init(const uint8_t* const _client_id);

/**
&amp;#160;* @brief &amp;#160; Set the callback function.
&amp;#160;*
&amp;#160;* @param &amp;#160; _callback &amp;#160; Function to be called when an event is detected.
&amp;#160;*/
void cloud_set_callback(const cloud_event_callback_t _callback);

/**
&amp;#160;* @brief &amp;#160; Create a JSON object so it is ready for publishing.
&amp;#160;*
&amp;#160;* @note &amp;#160; &amp;#160;The function acts as an interface handling all different object types defined by @ref cloud_topic_type_t.
&amp;#160;* &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;The data parameters consist of different data depending of the type (2 set of data arrays are needed for some types).
&amp;#160;*
&amp;#160;* @param &amp;#160; _type &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; The packet type.
&amp;#160;* @param &amp;#160; _primary_data &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; The generic primary data. (Unused = NULL).
&amp;#160;* @param &amp;#160; _primary_data_length &amp;#160; &amp;#160;The length of the primary data. (Unused = NULL).
&amp;#160;* @param &amp;#160; _secondary_data &amp;#160; &amp;#160; &amp;#160; &amp;#160; The generic secondary data. (Unused = NULL).
&amp;#160;* @param &amp;#160; _secondary_data_length &amp;#160;The length of the secondary data. (Unused = NULL).
&amp;#160;*/
void cloud_create_object(const uint8_t _type,
&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;const uint8_t** const _primary_data,
&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;const uint16_t* const _primary_data_length,
&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;const uint8_t** const _secondary_data,
&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;const uint16_t* const _secondary_data_length);

/**
&amp;#160;* @brief &amp;#160; Publish the stored data to the AWS topic.
&amp;#160;*/
void cloud_publish(void);

#endif /* CLOUD_H_ */&lt;/pre&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;cloud.c:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;string.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;logging/log.h&amp;gt;
#include &amp;lt;cJSON.h&amp;gt;

#include &amp;quot;cloud.h&amp;quot;
#include &amp;quot;watch.h&amp;quot;
#include &amp;quot;aws.h&amp;quot;

//! Register the cloud module to the logging system.
LOG_MODULE_REGISTER(CLOUD, 3);

//! The callback function.
static cloud_event_callback_t cloud_callback = NULL;

//! A list of the topics that can be published to AWS MQTT.
static const char* const topics[CLOUD_TOPIC_NUMBER_OF_TYPES] = { &amp;quot;acdc/get_pcc_load&amp;quot; };

//! A list of the response types from AWS MQTT.
static const char* const response_types[CLOUD_TOPIC_NUMBER_OF_TYPES] = { &amp;quot;getPCCLoad&amp;quot; };

//! The cloud publish information.
static cloud_publish_info_t cloud_publish_info;

/**
 * @brief   Find the response type index from the type string.
 *
 * @param   _type       The type in string.
 *
 * @retval  0x00-0xFE   The type index.
 * @retval  0xFF        No type is found.
 */
static uint8_t cloud_find_response_type_index(const char* const _type) {

    // Loop through the response types.
    for(uint16_t i = 0; i &amp;lt; sizeof(response_types); i++) {

        // Has the response type been found, then return the index.
        if(0 == strcmp(response_types[i], _type)) {

            return i;
        }
    }

    // If the response type is not found, return 0xFF.
    return 0xFF;
}

/**
 * @brief   Handle the PCC load result from AWS.
 *
 * @param   _aws_mqtt_status    The AWS MQTT status parameters.
 * @param   _results            The JSON object containing the results.
 *
 * @retval  false               An error has occurred.
 * @retval  true                No error has occurred.
 */
static bool cloud_handle_pcc_load_result(const aws_mqtt_status_t _aws_mqtt_status, const cJSON* const _results) {

    // Loop through the results array.
    for(uint16_t i = 0; i &amp;lt; cJSON_GetArraySize(_results); i++) {

        // Get the next array item.
        cJSON* result = cJSON_GetArrayItem(_results, i);

        // Get the status code.
        cJSON* status_code = cJSON_GetObjectItem(result, &amp;quot;statusCode&amp;quot;);

        // Has a success message been received?
        if(0 == strcmp(status_code-&amp;gt;valuestring, _aws_mqtt_status.success_message)) {

            // Get the id and PCC Load.
            cJSON* id = cJSON_GetObjectItem(result, &amp;quot;id&amp;quot;);
            cJSON* pcc_load = cJSON_GetObjectItem(result, &amp;quot;pccLoad&amp;quot;);

            LOG_INF(&amp;quot;PCC load Result(%s): %s.&amp;quot;, log_strdup(id-&amp;gt;valuestring), log_strdup(pcc_load-&amp;gt;valuestring));

            // Set the PCC load.
            va_set_pcc_load((uint16_t)atoi(pcc_load-&amp;gt;valuestring));
        }

        // Has a failure message been received?
        else {

            // Get the id and message.
            cJSON* id = cJSON_GetObjectItem(result, &amp;quot;id&amp;quot;);
            cJSON* message = cJSON_GetObjectItem(result, &amp;quot;message&amp;quot;);

            LOG_INF(&amp;quot;PCC load Result(%s): %s&amp;quot;, log_strdup(id-&amp;gt;valuestring),
                    log_strdup(message-&amp;gt;valuestring));

            return false;
        }
    }

    return true;
}

/**
 * @brief       Handle events from the AWS.
 *
 * @param       _aws_event      Event generated by AWS.
 * @param       _data           Received data.
 */
static void cloud_event_handler(const aws_event_t _aws_event, const uint8_t* const _data) {

    // Reset the event.
    uint8_t event = CLOUD_EVENT_NONE;

    // Go through the event.
    switch(_aws_event) {

        case AWS_EVENT_RESPONSE_READY: {

            // Get the AWS MQTT status parameters.
            aws_mqtt_status_t aws_mqtt_status;
            aws_get_status(&amp;amp;aws_mqtt_status);

            // Create the JSON object from the received data.
            cJSON* response = cJSON_Parse(_data);

            // Fetch the packet type object from the response.
            cJSON* type = cJSON_GetObjectItem(response, &amp;quot;type&amp;quot;);

            LOG_INF(&amp;quot;AWS response type: %s.&amp;quot;, log_strdup(type-&amp;gt;valuestring));

            // Go through the response type.
            switch(cloud_find_response_type_index(type-&amp;gt;valuestring)) {

                case CLOUD_TOPIC_TYPE_GET_PCC_LOAD: {

                    // A &amp;quot;Get PCC load&amp;quot; packet has been received.
                    event = CLOUD_EVENT_GET_PCC_LOAD_PACKET_SUCCEEDED;

                    // Fetch the pccLoadResults from the response.
                    cJSON* pcc_load_results = cJSON_GetObjectItem(response, &amp;quot;pccLoadResults&amp;quot;);

                    // Handle the PCC load result.
                    if(false == cloud_handle_pcc_load_result(aws_mqtt_status, pcc_load_results)) {

                        // An error has occurred.
                        event = CLOUD_EVENT_GET_PCC_LOAD_PACKET_FAILED;
                    }

                    break;
                }

                default:

                    break;
            }

            // Remove the cJSON object.
            cJSON_Delete(response);

            break;
        }

        default:

            break;
    }

    // Has a Cloud callback been set?
    if((CLOUD_EVENT_NONE != event) &amp;amp;&amp;amp; (NULL != cloud_callback)) {

        // Set the callback function.
        cloud_callback(event);
    }
}

/**
 * @brief       Create &amp;quot;Add Response&amp;quot; JSON object.
 *
 * @param       _response       The final JSON object.
 */
static void cloud_create_add_response(cJSON* _response) {

    // Get the AWS MQTT status parameters.
    aws_mqtt_status_t aws_mqtt_status;
    aws_get_status(&amp;amp;aws_mqtt_status);

    // Add the elements to the response object.
    cJSON_AddItemToObject(_response, &amp;quot;topic&amp;quot;, cJSON_CreateString(aws_mqtt_status.topic));
    cJSON_AddItemToObject(_response, &amp;quot;failureMessage&amp;quot;, cJSON_CreateString(aws_mqtt_status.failure_message));
    cJSON_AddItemToObject(_response, &amp;quot;successMessage&amp;quot;, cJSON_CreateString(aws_mqtt_status.success_message));
    cJSON_AddItemToObject(_response, &amp;quot;qos&amp;quot;, cJSON_CreateString(aws_mqtt_status.qos));
}

/**
 * @brief   Run the cloud functionality.
 *          This function shall only be called inside a thread.
 */
static void cloud_run(void) {

    while(1) {

        // Wait until connection is requested.
        if(false == aws_is_connection_requested()) {

            k_sleep(K_MSEC(10));

            continue;
        }

        // Keep the AWS MQTT broker connection alive and polling for new events.
        aws_run();
    }
}

//! Statically define and initialize the cloud thread.
K_THREAD_DEFINE(cloud_thread, 8192,
                cloud_run, NULL, NULL, NULL,
                K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);

void cloud_init(const uint8_t* const _client_id) {

    // Initialize the AWS.
    aws_init(_client_id, cloud_event_handler);
}

// cppcheck-suppress    unusedFunction
void cloud_set_callback(const cloud_event_callback_t _callback) {

    // Set the callback function.
    cloud_callback = _callback;
}

void cloud_create_object(const uint8_t _type,
                         const uint8_t** const _primary_data,
                         const uint16_t* const _primary_data_length,
                         const uint8_t** const _secondary_data,
                         const uint16_t* const _secondary_data_length) {

    // Get the current date and time.
    watch_t watch;
    bool watch_status = watch_get(&amp;amp;watch);

    // Is the watch valid?
    if(false == watch_status) {
        LOG_ERR(&amp;quot;Watch is invalid.&amp;quot;);
        return;
    }

    // Create a JSON object holding the complete event.
    cJSON* event_data = cJSON_CreateObject();

    // Go through the type of the cloud publish.
    switch(_type) {

        case CLOUD_TOPIC_TYPE_GET_PCC_LOAD: {

            // Add the Charging load id string to the final event object.
            cJSON_AddItemToObject(event_data, &amp;quot;chargingLoadId&amp;quot;, cJSON_CreateString(&amp;quot;9b605eca-b857-42d3-a824-ef5ca15e8a74&amp;quot;));

            break;
        }

        default:

            break;
    }

    // Create an object holding the response.
    cJSON* response = cJSON_CreateObject();

    // Add the response to the object.
    cloud_create_add_response(response);

    // Add the response object to the final event object.
    cJSON_AddItemToObject(event_data, &amp;quot;response&amp;quot;, response);

    // Store the AWS publish data.
    cloud_publish_info.topic = (char*)topics[_type];
    cloud_publish_info.data = cJSON_Print(event_data);

    // Free the cJSON object.
    cJSON_free(event_data);

    // Remove the cJSON object.
    cJSON_Delete(event_data);
}

void cloud_publish(void) {

    // Publish the data to the AWS.
    aws_publish(cloud_publish_info.topic, cloud_publish_info.data);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;schedular_h:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#ifndef SCHEDULER_H_
#define SCHEDULER_H_

/**
 * @brief   Initialize the scheduler.
 */
void scheduler_init(void);

#endif /* SCHEDULER_H_ */&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;schedular.c:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdio.h&amp;gt;

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;logging/log.h&amp;gt;

#include &amp;quot;scheduler.h&amp;quot;
#include &amp;quot;watch.h&amp;quot;
#include &amp;quot;cloud.h&amp;quot;

//! Register the scheduler module to the logging system.
LOG_MODULE_REGISTER(Scheduler, 3);

/**
 * @brief   Handle the scheduler events (Callback).
 *
 * @param   event   The scheduler event.
 * @param   watch   The current date and time.
 */
static void scheduler_event_handler(const scheduler_event_t event, const watch_t* const watch) {

    // Look through the event.
    switch(event) {

        // Event SCHEDULER_EVENT_TICK_OCCURRED occurred.
        case SCHEDULER_EVENT_TICK_OCCURRED: {


            // Create JSON object and get it published to the topic.
            cloud_create_object(CLOUD_TOPIC_TYPE_GET_PCC_LOAD, NULL, NULL, NULL, NULL);
            cloud_publish();

            break;
        }

        // Event SCHEDULER_EVENT_INVALID occurred.
        case SCHEDULER_EVENT_INVALID:

            LOG_ERR(&amp;quot;Date &amp;amp; time is invalid.&amp;quot;);

            break;

        default:

            break;
    }
}

void scheduler_init(void) {

    // Set the scheduler callback.
    watch_set_scheduler_callback(scheduler_event_handler);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375765?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 11:49:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:63ee7451-992f-411a-9924-d56ce9c25117</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Pasted here is fine, I&amp;#39;m most interested in seeing your c files and your prj.conf file&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375748?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 11:13:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9f65edc4-cc15-48e3-a007-789c9f48a934</guid><dc:creator>Jensenius</dc:creator><description>&lt;p&gt;Do you want the code pasted here or maybe a git link or some other way?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375688?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 08:24:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7cf29d09-2e5a-428f-bfe2-2940a8cdd608</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Excuse me? I&amp;#39;m not sure what you mean here.&lt;/p&gt;
&lt;p&gt;It would be easier for me to help you figure out what the issue is if you could please share your code.&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375671?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 07:37:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4815f40-60f4-430d-a8fa-51fbb8b51c3c</guid><dc:creator>Jensenius</dc:creator><description>&lt;p&gt;I have written to you for how you want the code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LOG freeze at mqtt</title><link>https://devzone.nordicsemi.com/thread/375592?ContentTypeID=1</link><pubDate>Tue, 05 Jul 2022 14:02:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6adf018c-9b40-4110-9c0d-48f9bfe304b6</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Hello!&lt;/p&gt;
&lt;p&gt;Could you please share the code you&amp;#39;re using so I could try to reproduce on my side?&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>