<?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>Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/70951/publishing-mqtt-data-to-multiple-topics-in-thingy91</link><description>Hi Dev Team, 
 I am running a program that is publishing data over MQTT using a particular topic and broker on the Thingy91. 
 I wanted to know a way in which I could send different data to different topics and how it could be configured in the mqtt example</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 10 Feb 2021 09:59:40 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/70951/publishing-mqtt-data-to-multiple-topics-in-thingy91" /><item><title>RE: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/293716?ContentTypeID=1</link><pubDate>Wed, 10 Feb 2021 09:59:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5bb73b09-20bb-4a41-98d2-1db5d0377aa1</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Hakon,&lt;/p&gt;
&lt;p&gt;Yes, makes sense. I understood what you mean here. This helps &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;Thanks for the help :) .&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: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/293580?ContentTypeID=1</link><pubDate>Tue, 09 Feb 2021 14:06:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:28f6a2df-1459-4503-8962-0adb100ce39b</guid><dc:creator>Hakon</dc:creator><description>[quote user="Adeel"]Is it possible to have just one data_publish function wherein I can publish to various topics ?[/quote]
&lt;p&gt;&amp;nbsp;Yes, it should be possible to use the same function if you just change the content of param.message.topic.topic.utf8 between each time you call mqtt_publish(). No need to have two separate functions, just change the publish topic inside the data_publish() function. Does that make sense?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/292673?ContentTypeID=1</link><pubDate>Wed, 03 Feb 2021 12:54:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1b559198-8577-4867-bc34-dc303f70ce4a</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Hakon,&lt;/p&gt;
[quote userid="73166" url="~/f/nordic-q-a/70951/publishing-mqtt-data-to-multiple-topics-in-thingy91/292670#292670"]&amp;nbsp;You can&amp;#39;t use the data_publish() as it is, because the publish topic is hard coded.[/quote]
&lt;p&gt;Yeah I understand this now.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I added another function : data_publish1() like this :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
/**@brief Function to publish data on the configured topic
 */
static int data_publish(struct mqtt_client *c, enum mqtt_qos qos,
	u8_t *data, size_t len)
{
	struct mqtt_publish_param param;

	param.message.topic.qos = qos;
	param.message.topic.topic.utf8 = CONFIG_MQTT_PUB_TOPIC;
	param.message.topic.topic.size = strlen(CONFIG_MQTT_PUB_TOPIC);
	param.message.payload.data = data;
	param.message.payload.len = len;
	param.message_id = sys_rand32_get();
	param.dup_flag = 0;
	param.retain_flag = 0;

	data_print(&amp;quot;Publishing: &amp;quot;, data, len);
	printk(&amp;quot;to topic: %s len: %u\n&amp;quot;,
		CONFIG_MQTT_PUB_TOPIC,
		(unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC));

	return mqtt_publish(c, &amp;amp;param);
}


/**@brief Function to publish data on the configured topic
 */
static int data_publish1(struct mqtt_client *c, enum mqtt_qos qos,
	u8_t *data, size_t len)
{
	struct mqtt_publish_param param;

	param.message.topic.qos = qos;
	param.message.topic.topic.utf8 = CONFIG_MQTT_PUB_TOPIC_1;
	param.message.topic.topic.size = strlen(CONFIG_MQTT_PUB_TOPIC_1);
	param.message.payload.data = data;
	param.message.payload.len = len;
	param.message_id = sys_rand32_get();
	param.dup_flag = 0;
	param.retain_flag = 0;

	data_print(&amp;quot;Publishing: &amp;quot;, data, len);
	printk(&amp;quot;to topic: %s len: %u\n&amp;quot;,
		CONFIG_MQTT_PUB_TOPIC_1,
		(unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC_1));

	return mqtt_publish(c, &amp;amp;param);
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In the autoconf.h file, I added :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define CONFIG_MQTT_PUB_TOPIC &amp;quot;sensorstest1/nrf352656101119801/pressure&amp;quot;
#define CONFIG_MQTT_PUB_TOPIC_1 &amp;quot;sensorstest1/nrf352656101119801/humidity&amp;quot;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In the MQTT_EVENT_PUBLISH: , i added :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;case MQTT_EVT_PUBLISH: {
		const struct mqtt_publish_param *p = &amp;amp;evt-&amp;gt;param.publish;

		printk(&amp;quot;[%s:%d] MQTT PUBLISH result=%d len=%d\n&amp;quot;, __func__,
		       __LINE__, evt-&amp;gt;result, p-&amp;gt;message.payload.len);
		err = publish_get_payload(c, p-&amp;gt;message.payload.len);
		if (err &amp;gt;= 0) {
			data_print(&amp;quot;Received: &amp;quot;, payload_buf,
				p-&amp;gt;message.payload.len);
			/* Echo back received data */
			data_publish(&amp;amp;client, MQTT_QOS_1_AT_LEAST_ONCE,
				payload_buf, p-&amp;gt;message.payload.len);
                        data_publish1(&amp;amp;client, MQTT_QOS_1_AT_LEAST_ONCE,
				payload_buf, p-&amp;gt;message.payload.len);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This made it work as I am able to publish on different topic.&lt;/p&gt;
&lt;p&gt;Is it possible to have just one data_publish function wherein I can publish to various topics ? (only if possible, otherwise the above mentioned method is working :) ).&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: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/292670?ContentTypeID=1</link><pubDate>Wed, 03 Feb 2021 12:46:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:15a81e59-3369-4fea-a2af-a0c3fc0e8f80</guid><dc:creator>Hakon</dc:creator><description>[quote user="Adeel"]Could you let me know if there can be another method for doing this ?[/quote]
&lt;p&gt;&amp;nbsp;You can&amp;#39;t use the data_publish() as it is, because the publish topic is hard coded. But you can just change the value of param.message.topic.topic.utf8 to something else.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/292177?ContentTypeID=1</link><pubDate>Mon, 01 Feb 2021 10:28:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2e66724e-42a6-413e-9a16-b64514886c19</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Hakon,&lt;/p&gt;
&lt;p&gt;I found a way of doing this, though I am not sure its the most optimized way.&lt;/p&gt;
&lt;p&gt;I duplicated the data_publish() function in the mqtt_simple , with a name : data_publish1() and configured it with a different topic :&amp;nbsp;CONFIG_MQTT_PUB_TOPIC_1. I already updated this topic in the autoconf.h file.&lt;/p&gt;
&lt;p&gt;I added this new function I created to the MQTT_EVT_PUBLISH event after the data_publish() function.&lt;/p&gt;
&lt;p&gt;By doing this, I am able to publish different data to different topics now, although I think there can be an optimized method of doing this instead of creating new functions for different MQTT_topics.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you let me know if there can be another method for doing this ?&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: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/291735?ContentTypeID=1</link><pubDate>Thu, 28 Jan 2021 10:07:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:33b0426e-4e18-4f25-9e2b-f2f7920a1045</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Hakon,&lt;/p&gt;
&lt;p&gt;No, I am just building up my application on the mqtt_simple example. So basically the sample I am using is mqtt_simple.&lt;/p&gt;
&lt;p&gt;Right now, I have 1 topic assigned and I am able to publish the data to that topic. I need to publish another data to a different topic as well. I wanted to know as to how I could do that.&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: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/291734?ContentTypeID=1</link><pubDate>Thu, 28 Jan 2021 10:04:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5e1a131c-d59a-4c99-bce3-fba37295eddc</guid><dc:creator>Hakon</dc:creator><description>&lt;p&gt;What sample are you using? Are you using the AWS IoT library?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/291728?ContentTypeID=1</link><pubDate>Thu, 28 Jan 2021 09:56:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a3191aec-2b3e-4e08-adab-f1a7db827c92</guid><dc:creator>Adeel</dc:creator><description>&lt;p&gt;Hi Hakon,&lt;/p&gt;
&lt;p&gt;I meant publishing to multiple topics .&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: Publishing MQTT data to multiple topics in Thingy91</title><link>https://devzone.nordicsemi.com/thread/291727?ContentTypeID=1</link><pubDate>Thu, 28 Jan 2021 09:54:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fd806354-1aaf-4a93-b9c1-9350486a1b3c</guid><dc:creator>Hakon</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]Could you let me know how to configure multiple topics in the program ?[/quote]
&lt;p&gt;&amp;nbsp;What do you mean by configure multiple topics? Do you mean publishing to multiple topics or subscribe to multiple topics?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>