Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] subscribing to multiple topics

Hello,

 

I am looking at using MQTT  in our products and have been able to successfully port the MQTT C++ synchronous library to our development environment (ARM Cortex-M3 processor using µcLinux).

I could execute the hello world application which I have been transforming to assess the kind of performance and functionalities I can get on my board.

Though it is primarily working wonderfully for my target application I am running into a small issue I have not been able to tackle.

 

My basic test application is basically linked to inputs and output connected to my board.

When an input changes states, I publish a message with the topic identifying the board and the input involved.

My outputs are somehow linked to topics I subscribe to (for the moment I target to have one specific subscription per output).

When a message is received, a call back function switches the output state. (I have different callable functions for my four outputs).

 

Though it works pretty well when I have a single topic I subscribe to for my output, I have tried to subscribe to multiple topics but I only get actions triggered for the first topic I subscribed to.

In my application, I have used the code for the hello.cpp example but I am probably missing something here.

 

Here is a sample of code for the section where I subscribe to the different topics:

 

    for(i=0;i<4;i++){

        if (strcmp(config->relayTopic[i],"None")!=0){

            switch(i){

                case 0:

                    rc = client.subscribe(config->relayTopic[i], config->relayQoS[i], switchRelay1);

                    break;

                case 1:

                    rc = client.subscribe(config->relayTopic[i], config->relayQoS[i], switchRelay2);

                    break;

                case 2:

                    rc = client.subscribe(config->relayTopic[i], config->relayQoS[i], switchRelay3);

                    break;

                case 3:

                    rc = client.subscribe(config->relayTopic[i], config->relayQoS[i], switchRelay4);

                    break;

                default:

                    break;

            }

            if (verbose==1){  

                if (rc != 0)

                    printf("rc from MQTT subscribe (Relay %d, topic %s) is %d\n",

                        i+1,config->relayTopic[i],rc);

                else

                    printf("MQTT subscription for relay activation %d - topic %s\n",

                        i+1,config->relayTopic[i]);

            }

        }

    }

 

 

Thank you in advance for your help.

 

Regards,

 

François Macé

 

 


Back to the top