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

How do you subscribe multiple topics on nRF9160?

I want to subscribe two topics. How do you implement it?

static int subscribe(void)
{
	struct mqtt_topic subscribe_topic_1 = {
		.topic = {
			.utf8 = CONFIG_MQTT_SUB_TOPIC_1,
			.size = strlen(CONFIG_MQTT_SUB_TOPIC_1)
		},
		.qos = MQTT_QOS_1_AT_LEAST_ONCE
	};

	struct mqtt_topic subscribe_topic_2 = {
		.topic = {
			.utf8 = CONFIG_MQTT_SUB_TOPIC_2,
			.size = strlen(CONFIG_MQTT_SUB_TOPIC_2)
		},
		.qos = MQTT_QOS_1_AT_LEAST_ONCE
	};

	const struct mqtt_subscription_list subscription_list = {
		.list = &subscribe_topic, // TODO HOW DO YOU SET TWO TPICS?
		.list_count = 2,
		.message_id = 1234
	};

	printk("Subscribing to: %s len %u\n", CONFIG_MQTT_SUB_TOPIC,
		(unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC));

	return mqtt_subscribe(&client, &subscription_list);
}

Parents
  • Hi,

    To subscribe to multiple topics, you can do the following:

    static int subscribe(void)
    {
    	struct mqtt_topic subscribe_topics[] = {
    		{
    		    .topic = {
    			    .utf8 = CONFIG_MQTT_SUB_TOPIC_1,
    			    .size = strlen(CONFIG_MQTT_SUB_TOPIC_1)
    		    },
    		    .qos = MQTT_QOS_1_AT_LEAST_ONCE
    		},
            	{
    		    .topic = {
    			    .utf8 = CONFIG_MQTT_SUB_TOPIC_2,
    			    .size = strlen(CONFIG_MQTT_SUB_TOPIC_2)
    		    },
    		    .qos = MQTT_QOS_1_AT_LEAST_ONCE
    		}
    	};
    
    	const struct mqtt_subscription_list subscription_list = {
    		.list = subscribe_topics,
    		.list_count = ARRAY_SIZE(subscribe_topics),
    		.message_id = 1234
    	};
    
    	printk("Subscribing to: %s len %u\n", CONFIG_MQTT_SUB_TOPIC,
    		(unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC));
    
    	return mqtt_subscribe(&client, &subscription_list);
    }

    Best regards,

    Jan Tore

Reply
  • Hi,

    To subscribe to multiple topics, you can do the following:

    static int subscribe(void)
    {
    	struct mqtt_topic subscribe_topics[] = {
    		{
    		    .topic = {
    			    .utf8 = CONFIG_MQTT_SUB_TOPIC_1,
    			    .size = strlen(CONFIG_MQTT_SUB_TOPIC_1)
    		    },
    		    .qos = MQTT_QOS_1_AT_LEAST_ONCE
    		},
            	{
    		    .topic = {
    			    .utf8 = CONFIG_MQTT_SUB_TOPIC_2,
    			    .size = strlen(CONFIG_MQTT_SUB_TOPIC_2)
    		    },
    		    .qos = MQTT_QOS_1_AT_LEAST_ONCE
    		}
    	};
    
    	const struct mqtt_subscription_list subscription_list = {
    		.list = subscribe_topics,
    		.list_count = ARRAY_SIZE(subscribe_topics),
    		.message_id = 1234
    	};
    
    	printk("Subscribing to: %s len %u\n", CONFIG_MQTT_SUB_TOPIC,
    		(unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC));
    
    	return mqtt_subscribe(&client, &subscription_list);
    }

    Best regards,

    Jan Tore

Children
Related