<?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>Philosophy using MQTT in NRF9160 with other codes</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/92830/philosophy-using-mqtt-in-nrf9160-with-other-codes</link><description>I am taking mqqt_simple of NRF9160 as reference for this question, 
 This is questing regarding, what is recommendation / Philosophy of using MQTT in NRF9160 with other codes e.g. sensor data or gps data or machine learning etc. 
 
 here is my understanding</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 29 Oct 2022 01:27:25 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/92830/philosophy-using-mqtt-in-nrf9160-with-other-codes" /><item><title>RE: Philosophy using MQTT in NRF9160 with other codes</title><link>https://devzone.nordicsemi.com/thread/393097?ContentTypeID=1</link><pubDate>Sat, 29 Oct 2022 01:27:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8bd3f746-55f8-4d22-95cc-674f09b6919b</guid><dc:creator>Desh</dc:creator><description>&lt;p&gt;HI H&amp;aring;kon&lt;/p&gt;
&lt;p&gt;Thanks for your response. I am testing as per your suggestions.&lt;/p&gt;
&lt;p&gt;Will git back if I see any issues.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Philosophy using MQTT in NRF9160 with other codes</title><link>https://devzone.nordicsemi.com/thread/390644?ContentTypeID=1</link><pubDate>Thu, 13 Oct 2022 12:26:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a1b1edcd-a965-48a9-af11-b1e7ff5b2356</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]&lt;p&gt;Once connection is made to MQTT server, program will enter while loop and continuously poll for incoming data, as shown below.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;span style="color:rgba(153, 51, 102, 1);"&gt;err = poll(&amp;amp;fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));&lt;/span&gt;&amp;nbsp;&lt;/span&gt; // for most of the time main loop will be stuck here. &lt;/p&gt;
&lt;p&gt;This loop will timeout after CONFIG_MQTT_KEEPALIVE,during this time code will come out of poll section thus we can run other routines in the code.&lt;/p&gt;
&lt;p&gt;In addition if you have interrupt routine, then also poll will exiting and during this time code in ISR and other code in main loop will run.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Multi-thread may be option but still the same result as thread running poll function will block other thread until it times out or&amp;nbsp; ISR occurs.&lt;/p&gt;[/quote]
&lt;p&gt;poll() does not block, it will yield for other threads.&lt;/p&gt;
&lt;p&gt;It&amp;#39;ll &amp;quot;block&amp;quot; the originating thread until it finishes (ie. sleep period expires or data is received), but all other threads are free to be scheduled.&lt;/p&gt;
[quote user=""]Code above works as provided but when MQTT code has to work will other section code , e.g. polling other sensor every second or some longer time consuming routing, then poll routine will block/consume most part of while loop time. So how to get round this problem.[/quote]
&lt;p&gt;You can use a work queue, or a dedicated thread for your other logic:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/zephyr/kernel/services/threads/workqueue.html"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/zephyr/kernel/services/threads/workqueue.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>