<?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>Buffer JSON data and publish through MQTT nrf9160</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/71505/buffer-json-data-and-publish-through-mqtt-nrf9160</link><description>Hi Dev Team, 
 I have an application wherein I am publishing my sensor data every hour through MQTT using a Thingy91. 
 I am storing my sensor data value in a JSON format through cJSON library. I need to read this value every 10 mins, buffer it and send</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 17 Feb 2021 16:18:38 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/71505/buffer-json-data-and-publish-through-mqtt-nrf9160" /><item><title>RE: Buffer JSON data and publish through MQTT nrf9160</title><link>https://devzone.nordicsemi.com/thread/294936?ContentTypeID=1</link><pubDate>Wed, 17 Feb 2021 16:18:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6e42132b-62ef-4607-b945-84b4b37aec6d</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Didrik,&lt;/p&gt;
&lt;p&gt;Thanks for the information regarding this. Helped me understand more about these allocations &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Adeel.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffer JSON data and publish through MQTT nrf9160</title><link>https://devzone.nordicsemi.com/thread/294929?ContentTypeID=1</link><pubDate>Wed, 17 Feb 2021 16:01:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0309d851-9387-4e5e-ba78-427f7c60a7ed</guid><dc:creator>Didrik Rokhaug</dc:creator><description>[quote user="Adeel"]What can be the maximum value of these configurations ? and how do they effect the memory ?[/quote]
&lt;p&gt;&amp;nbsp;You are obviously limited by the amount of RAM in the system.&lt;/p&gt;
&lt;p&gt;CONFIG_MAIN_STACK_SIZE sets the stack size for the main thread, but there are often other threads as well. The sum of all the stack sizes, and the heap size should not be more than the amount of RAM in the system.&lt;/p&gt;
&lt;p&gt;However, you might be able to set higher numbers, as long as you don&amp;#39;t use too much RAM. If a stack or the heap starts to grow into a different area, weird things can happen (but porbably you just get a crash. Regardless, don&amp;#39;t do that).&lt;/p&gt;
&lt;p&gt;When it comes to the heap, the memory allocation algorithm will also be a consideration. Depending on the algorithm, increasing the heap size might not let you allocate &lt;em&gt;more&lt;/em&gt; items, just &lt;em&gt;bigger&lt;/em&gt; items.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffer JSON data and publish through MQTT nrf9160</title><link>https://devzone.nordicsemi.com/thread/294779?ContentTypeID=1</link><pubDate>Wed, 17 Feb 2021 09:52:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e70b7644-b194-477d-a544-ec18b2e7de7a</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Didrik,&lt;/p&gt;
&lt;p&gt;Thanks for the suggestion. I tried it and it worked. I had just one more question to ask you regarding the 2 configurations in prj.conf file .&lt;/p&gt;
&lt;p&gt;I have it like this in my sample.&lt;/p&gt;
&lt;p&gt;CONFIG_MAIN_STACK_SIZE=32768&lt;/p&gt;
&lt;p&gt;CONFIG_HEAP_MEM_POOL_SIZE=65536&lt;/p&gt;
&lt;p&gt;What can be the maximum value of these configurations ? and how do they effect the memory ?&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Adeel.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffer JSON data and publish through MQTT nrf9160</title><link>https://devzone.nordicsemi.com/thread/294325?ContentTypeID=1</link><pubDate>Mon, 15 Feb 2021 09:03:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d2848559-d508-4018-abd6-50333d2cc6de</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;You need to free sensor6 each time my_work_handler_6 runs, otherwise you will leak the memory allocated by cJSON_CreateObject().&lt;/p&gt;
&lt;p&gt;If you move the call to cJSON_Delete() out of the for loop&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffer JSON data and publish through MQTT nrf9160</title><link>https://devzone.nordicsemi.com/thread/294175?ContentTypeID=1</link><pubDate>Fri, 12 Feb 2021 13:49:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:59b95bf7-e4a7-4a10-addd-ae2bb4f2df45</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Didrik,&lt;/p&gt;
[quote userid="81181" url="~/f/nordic-q-a/71505/buffer-json-data-and-publish-through-mqtt-nrf9160/293830#293830"]The easiest way to save the data is probably to create a static array, either with pointers to heap-allocated strings (the allocation should happen in your work_handler, and the strings must be freed when you send them), or memcpy&amp;#39;ed strings directly into the array.[/quote]
&lt;p&gt;Thanks for the advice on this and I moved along to create a static array to buffer the data. I was able to achieve my usage but I noticed that the code reboots when I try to publish my data for the 5th time. Its a bit strange and I tried to change the MQTT broker but the code reboots at exactly the same point again.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I am attaching the log file as well as the sample code I am using so you could reproduce it at your end.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/Rebooting-log.txt"&gt;devzone.nordicsemi.com/.../Rebooting-log.txt&lt;/a&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/mqtt_5F00_buffer_5F00_test.rar"&gt;devzone.nordicsemi.com/.../mqtt_5F00_buffer_5F00_test.rar&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;To test it quickly, I have each timers working at 30 seconds for now, I try to get the data every 30 seconds and then send out the data when I have buffered 6 messages.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It always happens that the code works perfectly as I need it for the first 4 times while publishing every 180 seconds for now, but at the 5th time, the code reboots without any error update.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I tried to understand it based on the memory issue, and increased my memory space to :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;# Main thread&lt;br /&gt;CONFIG_MAIN_STACK_SIZE=32768&lt;/p&gt;
&lt;p&gt;CONFIG_HEAP_MEM_POOL_SIZE=65536&lt;/p&gt;
&lt;p&gt;and now the reboot happens after the 15th time.&lt;/p&gt;
&lt;p&gt;I guess I am not freeing up my memory correctly that I need to, and I am not sure how to do that properly.&lt;/p&gt;
&lt;p&gt;Could you try to reproduce this code at your end and see what the issue could be with this. It would be of great help&amp;nbsp;&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Adeel.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffer JSON data and publish through MQTT nrf9160</title><link>https://devzone.nordicsemi.com/thread/293830?ContentTypeID=1</link><pubDate>Wed, 10 Feb 2021 15:38:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4a472362-e097-45d5-8e20-5d1448b0ef94</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Depending on your use case, it might be easier to store the raw data, than the encoded string.&lt;/p&gt;
&lt;p&gt;Where do you allocate memory for out1?&lt;/p&gt;
&lt;p&gt;The easiest way to save the data is probably to create a static array, either with pointers to heap-allocated strings (the allocation should happen in your work_handler, and the strings must be freed when you send them), or memcpy&amp;#39;ed strings directly into the array.&lt;/p&gt;
&lt;p&gt;You would also need a counter somewhere to keep track of which index you should write the next data point to.&lt;/p&gt;
&lt;p&gt;Alternatively, if you want a more fancy data structure, you can take a look at Zephyr&amp;#39;s &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.4.0/zephyr/reference/misc/data_structures.html#ring-buffer"&gt;ring buffer&lt;/a&gt; or &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.4.0/zephyr/reference/kernel/data_passing/fifos.html"&gt;FIFO queue&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Didrik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>