<?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>Azure IoT Hub (MQTT) disconnect when using UART in</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/115193/azure-iot-hub-mqtt-disconnect-when-using-uart-in</link><description>Hi, 
 I&amp;#39;m running into a strange issue with an application based on the azure_iot_hub sample using ncs v2.5.0 running on the nRF9160DK. Everything works fine when I run the code relevant for connecting to the Azure IoT Hub and sending messages in isolation</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 14 Oct 2024 11:47:09 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/115193/azure-iot-hub-mqtt-disconnect-when-using-uart-in" /><item><title>RE: Azure IoT Hub (MQTT) disconnect when using UART in</title><link>https://devzone.nordicsemi.com/thread/506090?ContentTypeID=1</link><pubDate>Mon, 14 Oct 2024 11:47:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:47b83a84-2fd6-427a-891b-9b7967c2d0b0</guid><dc:creator>WoutWG</dc:creator><description>&lt;p&gt;Thanks Charlie. Since I found a way to get it to work, I probably won&amp;#39;t dig much deeper on finding the root cause unless I bump into it. I&amp;#39;ll close the ticket for now.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Azure IoT Hub (MQTT) disconnect when using UART in</title><link>https://devzone.nordicsemi.com/thread/506074?ContentTypeID=1</link><pubDate>Mon, 14 Oct 2024 11:21:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4ef19250-5957-4041-bc8d-0f96788e2213</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Wout,&lt;/p&gt;
&lt;p&gt;Sorry for the late reply.&lt;span style="font-family:inherit;"&gt;I did not see clear difference between the two codes.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It seems to me that&amp;nbsp;azure_iot_hub_init run&amp;nbsp;after or&amp;nbsp;before cfg generated makes the difference, but the offical azure iot hub does use the first way.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I suggest you put all the codes flat inside main and verfiy the order. The azure IoT hub could be a good referece for you to compare the difference.&lt;/p&gt;
&lt;p&gt;Let me know if you need more help or have any update.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Azure IoT Hub (MQTT) disconnect when using UART in</title><link>https://devzone.nordicsemi.com/thread/505618?ContentTypeID=1</link><pubDate>Wed, 09 Oct 2024 15:56:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc661fe6-d0f9-4ec6-ad34-70b44427ed8e</guid><dc:creator>WoutWG</dc:creator><description>&lt;p&gt;Hi Charlie,&lt;/p&gt;
&lt;p&gt;Sorry for the delay, I bunkered in to drill down on the issue and try to find a root cause. I believe I&amp;#39;m pretty close, but it&amp;#39;s still not making much sense to me.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;It turns out the issue above is not related to UART, but the disconnect can be triggered by anything coming after connecting to the Azure server. It just happened to be that the uart calls came after connecting to the server. After spending hours comparing with the azure_iot_hub example and trying out different things, these are my current findings.&lt;/p&gt;
&lt;p&gt;In my own application, I created a separate file to handle all the networking (called network.c). I had moved all the functionality from the azure_iot_hub example to that file and called a network_init() function to set up the connection and another network_connect_to_server() function to connect to the Azure server.&lt;/p&gt;
&lt;p&gt;It turns out that the culprit is moving the azure_iot_hub_connect(&amp;amp;cfg) (and initializing the cfg struct) outside of the main function, even though I called network_connect_to_server() (which contains this functionality) from the main function in exactly the same way.&lt;/p&gt;
&lt;p&gt;So to put it in code, this doesn&amp;#39;t work&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/** main.c **/

int main(void)
{
    /* ...*/
    network_connect_to_server();
    
    /* Doing something else after this and sending periodic messages results in MQTT error and Azure disconnect */
}

/** network.c **/
int network_connect_to_server()
{
    int err;
    char hostname[128] = CONFIG_AZURE_IOT_HUB_HOSTNAME;
    char device_id[128] = CONFIG_AZURE_IOT_HUB_DEVICE_ID;
    struct azure_iot_hub_config cfg = {
        .device_id = {
            .ptr = device_id,
            .size = strlen(device_id),
        },
        .hostname = {
            .ptr = hostname,
            .size = strlen(hostname),
        },
        // .use_dps = true,
    };
    LOG_INF(&amp;quot;Device ID: %s&amp;quot;, device_id);
    LOG_INF(&amp;quot;Host name: %s&amp;quot;, hostname);

    err = azure_iot_hub_init(azure_event_handler);
    if (err)
    {
        LOG_ERR(&amp;quot;Azure IoT Hub could not be initialized, error: %d&amp;quot;, err);
        return err;
    }

    LOG_INF(&amp;quot;Azure IoT Hub library initialized&amp;quot;);

    err = azure_iot_hub_connect(&amp;amp;cfg);
    if (err &amp;lt; 0)
    {
        LOG_ERR(&amp;quot;azure_iot_hub_connect failed: %d&amp;quot;, err);
        return err;
    }

    LOG_INF(&amp;quot;Connection request sent to IoT Hub&amp;quot;);
    return err;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;But this does&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/** main.c **/

int main(void)
{
    /* ...*/
    network_init_iot_hub();
    
    char hostname[128] = CONFIG_AZURE_IOT_HUB_HOSTNAME;
    char device_id[128] = CONFIG_AZURE_IOT_HUB_DEVICE_ID;
    struct azure_iot_hub_config cfg = {
        .device_id = {
            .ptr = device_id,
            .size = strlen(device_id),
        },
        .hostname = {
            .ptr = hostname,
            .size = strlen(hostname),
        },
        // .use_dps = true,
    };
    LOG_INF(&amp;quot;Device ID: %s&amp;quot;, device_id);
    LOG_INF(&amp;quot;Host name: %s&amp;quot;, hostname);
    
    err = azure_iot_hub_connect(&amp;amp;cfg);
    if (err &amp;lt; 0)
    {
        LOG_ERR(&amp;quot;azure_iot_hub_connect failed: %d&amp;quot;, err);
        return err;
    }
    
    LOG_INF(&amp;quot;Connection request sent to IoT Hub&amp;quot;);
    
    /* Doing something else after this and sending periodic messages works fine */
}

/** network.c **/
int network_init_iot_hub()
{
    int err;

    err = azure_iot_hub_init(azure_event_handler);
    if (err)
    {
        LOG_ERR(&amp;quot;Azure IoT Hub could not be initialized, error: %d&amp;quot;, err);
        return err;
    }

    LOG_INF(&amp;quot;Azure IoT Hub library initialized&amp;quot;);
    return err;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Do you have any idea why that could be?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Azure IoT Hub (MQTT) disconnect when using UART in</title><link>https://devzone.nordicsemi.com/thread/505153?ContentTypeID=1</link><pubDate>Mon, 07 Oct 2024 11:07:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1d78dcc0-02bc-499f-9bcb-e12e18ca8b61</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Do you run something time comsumming/blocking in work hanlder? This may cause other time sensitive threads like MQTT commincation stack to fail.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.7.0/page/zephyr/kernel/services/threads/workqueue.html#workqueue_best_practices"&gt;https://docs.nordicsemi.com/bundle/ncs-2.7.0/page/zephyr/kernel/services/threads/workqueue.html#workqueue_best_practices&lt;/a&gt;&amp;nbsp;many give you some inspiration.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Azure IoT Hub (MQTT) disconnect when using UART in</title><link>https://devzone.nordicsemi.com/thread/505137?ContentTypeID=1</link><pubDate>Mon, 07 Oct 2024 09:59:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1345b175-dc9b-42cf-9cb4-462cacfab575</guid><dc:creator>WoutWG</dc:creator><description>&lt;p&gt;Hi Charlie,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you getting back to me. I appreciate that it&amp;#39;s not easy to support finding the cause of this without having the full context. I&amp;#39;ll see if I can isolate the uart part more or find a more minimal example that I can share to have a go at yourself.&lt;/p&gt;
&lt;p&gt;I thought that setting up a dedicated work_queue (which I did) already uses a separate thread and that the work_queue object just helps to abstract the workings of the thread a bit. Am I wrong in thinking that? Would setting up an actual separate thread with it&amp;#39;s own main function behave differently than having a dedicated work_queue?&lt;/p&gt;
&lt;p&gt;Best,&lt;/p&gt;
&lt;p&gt;Wout&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Azure IoT Hub (MQTT) disconnect when using UART in</title><link>https://devzone.nordicsemi.com/thread/505115?ContentTypeID=1</link><pubDate>Mon, 07 Oct 2024 08:17:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:99a305a0-a98e-4a4e-a5b1-6c8a9a4f0ffd</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Wout,&lt;/p&gt;
&lt;p&gt;Thanks for checking with us about this issue.&lt;/p&gt;
&lt;p&gt;It is hard to see the cause just from your description. You may try to run your uart part of application form a seperate thread, this will help to isolate it with MQTT module better.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>