<?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>How to code one-shot mqtt publish function?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/55068/how-to-code-one-shot-mqtt-publish-function</link><description>I&amp;#39;m trying to make a one-shot mqtt function. The process is below 
 - connect - publish - disconnect 
 Then I coded like below. I show only the core code. 
 
 Do I have to execute fds_init in send_one_shot_mqtt_msg everytime it publish a message on MQTT</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 01 Jul 2022 10:10:51 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/55068/how-to-code-one-shot-mqtt-publish-function" /><item><title>RE: How to code one-shot mqtt publish function?</title><link>https://devzone.nordicsemi.com/thread/375041?ContentTypeID=1</link><pubDate>Fri, 01 Jul 2022 10:10:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:49245627-f011-4386-b8f6-d2fccd8b9f46</guid><dc:creator>Torje</dc:creator><description>&lt;p&gt;Hello everyone,&lt;/p&gt;
&lt;p&gt;I&amp;#39;m doing a routine quite like Yusuke, and would like to share my experience if others have the same issues.&lt;/p&gt;
&lt;p&gt;I am only calling the fds_init once in the setup and polling works fine. I get retained messages on subscribed topics, and get ack on sent my messages.&lt;/p&gt;
&lt;p&gt;My one-shot-function is otherwise similar to Yusukes. However, I had problems with &amp;quot;Failed to open socket, error: 23&amp;quot; after several connections (this also prevented me from getting cell location from nRF Cloud).&lt;/p&gt;
&lt;p&gt;It&lt;span&gt;&amp;nbsp;&lt;/span&gt;seems the mqtt_disconnect-function does not close the socket, and limitations here cause trouble. I am now calling mqtt_abort after mqtt_disconnect, and this&lt;span&gt;&amp;nbsp;&lt;/span&gt;solves the issue for me.&lt;/p&gt;
&lt;p&gt;Description of mqtt_abort:&lt;br /&gt;&amp;quot;API to abort MQTT connection. This will close the corresponding transport without closing the connection gracefully at the MQTT level (with disconnect message).&amp;quot;&lt;/p&gt;
&lt;p&gt;Combining these two&lt;span&gt;&amp;nbsp;&lt;/span&gt;seems to work fine, enabling &amp;quot;one shot mqtt&amp;quot; with the mqtt.c library. Another solution could be to use the BSD library (&lt;a id="" href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.4.2/nrfxlib/bsdlib/README.html"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.4.2/nrfxlib/bsdlib/README.html&lt;/a&gt;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to code one-shot mqtt publish function?</title><link>https://devzone.nordicsemi.com/thread/251089?ContentTypeID=1</link><pubDate>Thu, 21 May 2020 01:01:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:30734078-58d0-41f9-91a8-b64fc369a37b</guid><dc:creator>crasspm</dc:creator><description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;I&amp;#39;m using this one shot technique.&amp;nbsp; All is well until I reach 22 publishes, after which the code hangs in the&amp;nbsp;mqtt_transport_write function in mqtt_transport.c&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve tried all kinds of permutations of this but I can;t get past this hang up.&amp;nbsp; Any insight?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to code one-shot mqtt publish function?</title><link>https://devzone.nordicsemi.com/thread/223792?ContentTypeID=1</link><pubDate>Thu, 05 Dec 2019 08:33:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:99cb0aba-8dde-47af-86b5-b52ac7fc9138</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi Yusuke,&lt;/p&gt;
&lt;p&gt;So when you run the &lt;em&gt;mqtt_disconnect()&amp;nbsp;&lt;/em&gt;and&amp;nbsp;&lt;em&gt;poll()&lt;/em&gt; afterward, the socket will be closed as far as I know so you would need to set up a new one next time.&lt;/p&gt;
&lt;p&gt;That means that you need to run&amp;nbsp;&lt;em&gt;fds_init()&amp;nbsp;&lt;/em&gt;each time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to code one-shot mqtt publish function?</title><link>https://devzone.nordicsemi.com/thread/223755?ContentTypeID=1</link><pubDate>Thu, 05 Dec 2019 05:37:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f2a9e879-8890-47fd-a460-b13be630bd15</guid><dc:creator>Yusuke</dc:creator><description>&lt;p&gt;Hi, Martin.&lt;/p&gt;
&lt;p&gt;I mean,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;do I have to execute fds_init in send_one_shot_mqtt_msg every time a MQTT message is published?&lt;/p&gt;
&lt;p&gt;Or is it ok to execute&amp;nbsp;&lt;span&gt;fds_init&amp;nbsp; once and repeatedly use&amp;nbsp;the file descriptor?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to code one-shot mqtt publish function?</title><link>https://devzone.nordicsemi.com/thread/223641?ContentTypeID=1</link><pubDate>Wed, 04 Dec 2019 14:22:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cfa78003-78b1-40df-bf20-5d0a0bc75200</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi Yusuke,&lt;/p&gt;
&lt;p&gt;The fds_init function initializes the &amp;quot;File Descriptor Structure&amp;quot; used by the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfxlib/bsdlib/README.html?highlight=poll" rel="noopener noreferrer" target="_blank"&gt;poll function&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In that function, it either sets it up to use &lt;strong&gt;non-secure&lt;/strong&gt; TCP transport for MQTT connections&amp;nbsp;OR&amp;nbsp;&lt;strong&gt;secure&amp;nbsp;&lt;/strong&gt;&lt;span&gt;TCP transport for MQTT connections.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;This is dependent if you have enabled TLS or not in the configuration.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;e.g. set this in &lt;strong&gt;prj.conf:&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;strong&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_MQTT_LIB_TLS=y&lt;/pre&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>