Multiple MQTT topic subscribe - PubSubClient

Hi guys,

I need your help, is it possible to subscribe on multiple mqtt topic with pubSubClient library ?

I found a solution by using strcmp()

void callback(char* topic, byte* payload, unsigned int length) {

  if(strcmp(topic,inTopicPwmChannel) == 0) { 
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.println("] ");
 
    for (int i = 0; i < length; i++) {
      charPwm[i] = (char)payload[i];
    }
   
      sscanf(charPwm, "%d", &pwmValue);

      Serial.print("PWM: ");
      Serial.println(pwmValue);   
  }

  if(strcmp(topic,inTopicCommandChannel) == 0) {
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.println("]");

    for (int i = 0; i < length; i++) {
      charPwm[i] = (char)payload[i];
    }

      sscanf(charPwm, "%d", &pwmValue);

      Serial.print("PWM: ");
      Serial.println(pwmValue);
  }
}

So there is another method to have the same result ?
My goal is eliminate the if(strcmp(....)) inside void callback(), to have multiple void callback() for each subscription.

In this example: void pwmCallback()... and void commandCallback()...

Is it possible ?

Post your code.

I looked at an MQTT pubsub library and I see a setCallback function, which implies I think that there can be only one. What's wrong with your strcmp method?

wildbill:
What's wrong with your strcmp method?

Nothing, the code with multiple subscription and strcmp() works. I only asked my self if there is another way to have the same results.