This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Publishing MQTT data to multiple topics in Thingy91

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.

For instance, I need to send different sensor values to different topics : (home/living/temp for temperature), (home/living/humid for humidity value).

I don't see an option of configuring more than 1 topic in the mqtt example. 

For now, I have defined it as : CONFIG_MQTT_PUB_TOPIC "home/living" in the autoconf.h file.

Could you let me know how to configure multiple topics in the program ?

Regards,

Adeel.

Parents Reply Children
  • Hi Hakon,

    No, I am just building up my application on the mqtt_simple example. So basically the sample I am using is mqtt_simple.

    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.

    Regards,

    Adeel.

  • Hi Hakon,

    I found a way of doing this, though I am not sure its the most optimized way.

    I duplicated the data_publish() function in the mqtt_simple , with a name : data_publish1() and configured it with a different topic : CONFIG_MQTT_PUB_TOPIC_1. I already updated this topic in the autoconf.h file.

    I added this new function I created to the MQTT_EVT_PUBLISH event after the data_publish() function.

    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. 

    Could you let me know if there can be another method for doing this ?

    Regards,

    Adeel.

  • Adeel said:
    Could you let me know if there can be another method for doing this ?

     You can'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.

  • Hi Hakon,

     You can't use the data_publish() as it is, because the publish topic is hard coded.

    Yeah I understand this now. 

    I added another function : data_publish1() like this : 

    /**@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("Publishing: ", data, len);
    	printk("to topic: %s len: %u\n",
    		CONFIG_MQTT_PUB_TOPIC,
    		(unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC));
    
    	return mqtt_publish(c, &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("Publishing: ", data, len);
    	printk("to topic: %s len: %u\n",
    		CONFIG_MQTT_PUB_TOPIC_1,
    		(unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC_1));
    
    	return mqtt_publish(c, &param);
    }
    
    

    In the autoconf.h file, I added : 

    #define CONFIG_MQTT_PUB_TOPIC "sensorstest1/nrf352656101119801/pressure"
    #define CONFIG_MQTT_PUB_TOPIC_1 "sensorstest1/nrf352656101119801/humidity"

    In the MQTT_EVENT_PUBLISH: , i added : 

    case MQTT_EVT_PUBLISH: {
    		const struct mqtt_publish_param *p = &evt->param.publish;
    
    		printk("[%s:%d] MQTT PUBLISH result=%d len=%d\n", __func__,
    		       __LINE__, evt->result, p->message.payload.len);
    		err = publish_get_payload(c, p->message.payload.len);
    		if (err >= 0) {
    			data_print("Received: ", payload_buf,
    				p->message.payload.len);
    			/* Echo back received data */
    			data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				payload_buf, p->message.payload.len);
                            data_publish1(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				payload_buf, p->message.payload.len);

    This made it work as I am able to publish on different topic.

    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 :) ).

    Regards,

    Adeel.

  • Adeel said:
    Is it possible to have just one data_publish function wherein I can publish to various topics ?

     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?

Related