Multiple MQTT Topics with Arduino PubSubClient

Adding a few more subscriptions is pretty easy.

multiple mqtt topics

In my Arduino MQTT Examples, I kept things simple by only subscribing to a single topic. One of the strengths of MQTT is that a device can subscribe (or publish) to multiple topics. The broker will sort things out. Even though my first example only showed one, it is straight forward to get the Arduino PubSubClient library to subscribe to Multiple MQTT topics.

The quick answer is that you need to look at the MQTT response to find out which topic sent the payload.

tl;dr version

If you’re looking for a quick answer, here’s the magic code we’ll add to the callback() function.

void callback(char* topic, byte* payload, unsigned int length) {
if (strcmp(topic,"pir1Status")==0)
  // whatever you want for this topic
}

Keep reading for a more detailed explanation of how to Subscribe to Multiple MQTT topics with Arduino’s PubSubClient. Obviously, this code will work on Arduino boards with a TCP/IP interface and, of course, the ESP8266 based boards.

Subscribing to Multiple MQTT Topics

Subscribing to multiple MQTT topics is just a matter of repeating the subscribe code. For example, here I subscribe to topics called “red,” “green,” and “blue.” (Guess what they represent.)

boolean reconnect() {
  if (client.connect("arduinoClient")) {
    client.subscribe("pir1Status");
    client.subscribe("red");
    client.subscribe("green");
    client.subscribe("blue");
    return client.connected();
  }
  Serial.println("I think connect failed.");
  return 0;
}

Once subscribed, turn your attention to PubSubClient’s callback function. Whenever a message is received, the callback function handles it.

MQTT reply

The callback() function provides a character array called “topic.” You might be tempted to convert this into an Arduino String object. This overused object probably seems easier to use, since you could use a “==” operator to match strings.

You don’t need the overhead of the String object. Instead, leave “topic” as a character array. There is a function in libc (and avr-libc) that helps, strcmp(). My guess is that “strcmp” stands for “string compare.”

strcmp() takes two arguments: string1 and string2. The value it returns tells you something about how the two strings compare to each other. It’s a simple function to use but has a few things you need to know.

Issues using strcmp()

The first thing to know is that you can’t use a switch statement with strcmp(). I wish you could; it would make the code much easier to read. At least, in my eyes.

Next, you should understand that strcmp() doesn’t return what most people expect. Instead ‘0’, or false, comes back when there is a match.

Note on Substrings. Since strcmp() is part of libc, there is plenty of documentation to explain how it works in detail. You should understand why it might return a negative number or something more than zero.  I’m not going to cover handling substrings here. I carefully pick my topics to avoid substring matches.

Here’s an example of callback() that could support receiving a message while subscribed to multiple topics.

void callback(char* topic, byte* payload, unsigned int length) {
  if (strcmp(topic,"pir1Status")==0){
    // whatever you want for this topic
  }

  if (strcmp(topic,"red")==0) {
    // obvioulsy state of my red LED
  }

  if (strcmp(topic,"blue")==0) {
    // this one is blue...
  }  

  if (strcmp(topic,"green")==0) {
    // i forgot, is this orange?
  }  
}

It’s a simple matter of using strcmp() to match the topic received in the PubSubClient packet.

Conclusion

No doubt, there are probably more optimized ways to handle both the subscribing and receive processing. But I like the code being straight forward and easy to read. The key to handling multiple MQTT topics is processing the packet that PubSubClient provides.

Question: What other MQTT questions can I help answer? You can leave a comment by clicking here.

Long comments, URLs, and code tend to get flagged for spam moderation. No need to resubmit.

Leave a comment

27 thoughts on “Multiple MQTT Topics with Arduino PubSubClient

  1. I’m hoping you’d help me out by looking at my code and tell me where I went wrong. I’m trying to connect some Wemos Mini D1s to the mqtt broker on my raspberry pi zero w. I was able to turn on/off an led attached the wemos via an ssh connection from my laptop. Now what I would like to do is add a toggle switch to one which would publish either an 0 or 1 to tuun on/off leds attached to the Wemos that have subscribed to that topic.
    here’s the code:
    “`

    /*
    
    BABY STEPS...
    
     Starting with the "basic ESP8266 MQTT example", making modifications for my ongoing project as I go.
    
     This sketch demonstrates the capabilities of the pubsub library in combination
     with the ESP8266 board/library.
    
     It will reconnect to the server if the connection is lost using a blocking
     reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
     achieve the same result without blocking the main loop.
    
    My goal is to get input from a toggle switch connected to D6, Publish to topic "Charts" as output status == HIGH or LOW, then the other ESP6266s would Subsribe to "Charts", turn on/off LED accoirdingly.
    
    */
    
    #include 
    #include 
    
    int togglePin = D6;//  Connect Adafruit capacitive touch toggle switch to D6
    int chartsledPin = D1;//  LED indicator for Charts-up
    boolean on = false;
    int buttonState = 0;
    
    
    // Update these with values suitable for your network.
    const char* ssid = "MY SSID";
    const char* password = "MY PASSWORD";
    const char* mqtt_server = "Mosquitto_broker";
    
    WiFiClient espClient;
    PubSubClient client(espClient);
    long lastMsg = 0;
    char msg[50];
    int value = 0;
    
    void setup() {
      pinMode(D6, INPUT);     // Adafruit Capacitive Touch Toggle Switch to D1
      pinMode(D1, OUTPUT);
    
      Serial.begin(115200);
      setup_wifi();
      client.setServer(mqtt_server, 1883);
      client.setCallback(callback);
    }
    
    void setup_wifi() {
    
      delay(10);
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
    
    void callback(char* topic, byte* payload, unsigned int length) {
      Serial.print("Message arrived [");
      Serial.print(topic);
      Serial.print("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }
      Serial.println();
    
    
    }
    
    
    void reconnect() {
      // Loop until we&#039;re reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect("ESP8266Client")) {
          Serial.println("connected");
          // Once connected, publish an announcement...
          //   client.publish("outTopic", "hello world");  *** I just want the LED (D1) to blink slowly until connected***
          // ... and resubscribe
          client.subscribe("Charts/PS");
          client.subscribe("Chapperones/PSA");//   WILL ADD THESE IN AFTER FIGURING OUT
          client.subscribe("Chapperones/PSB");//  HOW TO INCLUDE BUTTON INPUT
          client.subscribe("Chapperones/PSC");//
          client.subscribe("Chapperones/PSD");//
        } else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 5 seconds");
          // Wait 5 seconds before retrying
          delay(5000);
        }
      }
    }
    
    void loop() {
    
      if (!client.connected()) {
        reconnect();
      }
    
      int value = 0;
      client.loop();
    
      {
        buttonState = digitalRead(D6);
        if (buttonState == HIGH) {
          client.publish("Charts/PS", "1");
          digitalWrite(D1, HIGH);
        }
    
        else {
          client.publish("Charts/PS", "0");
          digitalWrite(D1, LOW);
        }
      }
    }
    
      • James, I am so sorry, I’m trying to add in a button press that will turn on/off leds on remote Wemos. Im toggling on/off via SSH from my laptop. It’s not 100%. I think maybe resending the publish too fast for the broker to receive, distribute the message. One Led will come on, then maybe the other, sometimes both and sometimes none at all.

        • I’m still not following ‘what you expect’ and ‘what is happening’. Now you’re saying that you have a hardware toggle button that works over ssh? I don’t understand how that’s possible. (Please read this on asking questions: http://www.catb.org/esr/faqs/smart-questions.html#beprecise)

          In your code, you are going to be blasting the broker with messages. With every iteration of loop() you are going to send either a 0 or 1. That’s a lot of messages. You need to learn how to use state-change detection for a button so you only send an event when the button’s state changes from on to off. The code in this example does that. You don’t need the counter stuff. https://www.arduino.cc/en/Tutorial/StateChangeDetection

          • Thank you so much for your time. I see your point with the state change. Working on that. I have another issue. I can get 2 wemos to connect to the mqtt server, but when I try more than 2, the rest will not connect.
            Any suggestions? here’s the LOOP currently:

            void loop()
            {
            if (!client.connected()) {
            reconnect();
            }
            client.loop();
            }

      • I just discovered that it goes awry when I plug in the second Wemos with the same sketch. Then it says “Client ESP8266 Client already connected, closing old connection”. Then tries reconnecting, goes into a loop of trying to reconnect.

        So in lines 32, 33 where it says espClient; I should be giving each Wemos a name?
        Line 87 is the only instance is says: “ESP8266” so I’m a bit confused. Going to change line 87 on the second Wemos to “PSA”, see what happens.

      • Thank you again for your time, patience and also directing me to the state change for my button press. I got that all sorted out. A couple of bad wires and BAD coding was tripping me up!

        Quick question: in this tutorial; I’ve read through this like 10 times. It looks like you are controlling the LEDs independently? or are they turned on/off based on results of the string comparison, meaning just one led at a time? Like a selector switch.

      • This compiles, but where, how do you digitalWrite(HIGH LOW) to turn them on/off respectively?

        void callback(char* topic, byte* payload, unsigned int length) {
        
          if (strcmp(topic, "Charts/PS") == 0) {
            // Charts up!
          }
        
          if (strcmp(topic, "Chaperone/PSA") == 0) {
            // Need a chaperone in Exam Room A
          }
        
          if (strcmp(topic, "Chaperone/PSB") == 0) {
            // Need a chaperone in Exam Room B
          }
        
          if (strcmp(topic, "Chaperone/PSC") == 0) {
            //  Need a chaperone in Exam Room C
          }
        
          if (strcmp(topic, "Chaperone/PSD") == 0) {
            //  Need a chaperone in Exam Room D
          }
        }
        
        
        • You could put your code where the comments are, like on line 3. If you’re using single character commands to turn on and off, then you would made the code something like:

            if (strcmp(topic, "Charts/PS") == 0) {
              // Charts up!
              for (int i=0;i<length;i++) {
                char receivedChar = (char)payload[i];
                if (receivedChar == '0')
                   digitalWrite(ledPin, HIGH);
                if (receivedChar == '1')
                   digitalWrite(ledPin, LOW);
                }
            }
          

          This code look as the “payload” received for a character and reacts. It’s from the original MQTT tutorial I posted. If you’re sending full strings like “ON” or “OFF” then you need another round of strcmp() on the payload variable.

          • Thank you again for your assistance.That worked(sort of). Instead of the led just turning on and staying on, it came on for a split second. At first I didn’t think it had worked. Maybe issues with jumper wires again. But I spotted it! The led flashed. SO I added a 5 second delay after the digitalWrite. Yes! it comes on. But why is it not staying on when digitalWrite(ledPin, HIGH)? Posting the whole sketch seems a bit much.
            What parts should I post?

          • You need brackets on your if-statements.

            if (receivedChar == '0')
                 Serial.println (F("PSA LED ON"));
            digitalWrite(officeaPin, HIGH);
            if (receivedChar == '1')
                Serial.println (F("PSA LED OFF"));
            digitalWrite(officeaPin, LOW);
            

            The digitalWrite()s are executing regardless of what the payload contains. So the light might be briefly flickering on, but will also be turned off.

            Should have been

            if (receivedChar == '0') {
                 Serial.println (F("PSA LED ON"));
                 digitalWrite(officeaPin, HIGH);
            }
            if (receivedChar == '1') {
                Serial.println (F("PSA LED OFF"));
                digitalWrite(officeaPin, LOW);
            }
            
          • Eureeka! They work as I intended. Thank you sooo much!
            So that just proves that an ESP8266 can Sub/Pub to several topics at the same time. Until you run out if pins. : D

            I struggled with it for some time, then I remembered you saying something about choosing the topic names carefully. PSA, PSB etc did not work but A, B, C, D did. Putting in comment tags helped keep it all straight.
            ”’
            if (strcmp(topic, “A”) == 0) {
            // Need a chaperone in Exam Room A
            for (int i = 0; i < length; i++) {
            char receivedChar = (char)payload[i];
            if (receivedChar == '0') {
            Serial.println (F("PSA LED ON"));
            digitalWrite(officeaPin, HIGH);
            }
            if (receivedChar == '1') {
            Serial.println (F("PSA LED OFF"));
            digitalWrite(officeaPin, LOW);
            }
            }
            }
            '''

    • That worked! Each Client must have a name, that is specified in quotation marks on line 87.
      The lights are turning on/off consistently. Now I can drop the SSH connection and move on to the button state change as you suggested.

  2. Hi James. Good tutorial, thanks !

    Can I have two PubSubclient clients – onte connected to a one broker, one to another ?

    Like:

    const char* mqtt_server1 = "iot.eclipse.org";
    const char* mqtt_server2 = "odilon.local";
    #define mqtt_port 17546
    
    WiFiClient espClient;   //this is a ESP8266 client; the connection is not aborded here
    
    PubSubClient MQTTclient1(espClient);
    PubSubClient MQTTclient2(espClient);
    
    .....
    .....
    
       MQTTclient1.setServer(mqtt_server1, mqtt_port);
       MQTTclient1.setCallback(callback1);
    
       MQTTclient2.setServer(mqtt_server2, mqtt_port);
       MQTTclient2.setCallback(callback2);
    

    Something like this.

    Thanks again !

    Odilon

    • I think I’ve connected to multiple brokers before (one inside my network and one public). But can’t find the code. (Maybe I haven’t.) I would imagine in addition to setCallback, you also need to add the second client to the reconnect function.

      • Hi James.

        Multiple brokers with a PubSubClient library are not supported. I took a look at the source, there is only one instance of the class in PubSubClient.h:

        private:
           Client* _client;
           uint8_t buffer[MQTT_MAX_PACKET_SIZE];
           uint16_t nextMsgId;
           unsigned long lastOutActivity;
           unsigned long lastInActivity;
           bool pingOutstanding;
           MQTT_CALLBACK_SIGNATURE;
           uint16_t readPacket(uint8_t*);
           boolean readByte(uint8_t * result);
           boolean readByte(uint8_t * result, uint16_t * index);
           boolean write(uint8_t header, uint8_t* buf, uint16_t length);
           uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
           IPAddress ip;
           const char* domain;
           uint16_t port;
           Stream* stream;
           int _state;
        

        I’ll take a look at this library to see if I can modify it.

        Best regards !

      • Hi james,

        I was wrong. Yes, it is possible to connect to two different brokers simultaneously.

        However, it is also necessary to double the Ethernet client:

        Const char * mqtt_server1 = "192.168.2.111";
        Const char * mqtt_server2 = "test.mosquitto.org";
        Const int mqtt_port1 = 1883;
        Const int mqtt_port2 = 1883;
        
        WiFiClient espClient1, espClient2;
        PubSubClient MQTTclient1 (espClient1);
        PubSubClient MQTTclient2 (espClient2);
        
        I made the connection up - to the broker on my local network and to the test broker at mosquitto.org. I sent and received messages from both.
        
        Regards !!
        
      • The reconnect function can be parameterized:

        void reconnect(PubSubClient MQTTclient, char* cliente) {
           // Loop until we're reconnected
           while (!MQTTclient.connected()) {
              Serial.print("Attempting MQTT connection...");
              // Attempt to connect
              if (MQTTclient.connect(cliente)) {
        
  3. Hi James,
    Tkx again for this tutorial, you make things simple ! In a project I’m trying to publish payload from my arduino or ESP8266 to a webpage, which updates the information. This goes about mqtt client in javascript. I’ve looked on the net but the info is scarse (or I don’t search well 🙂 Maybe you can give an example of how this could be done ?
    Thanks !