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.

ALL comments submitted with fake or throw-away services are deleted, regardless of content.

Don't be a dweeb.

Leave a comment

55 thoughts on “Multiple MQTT Topics with Arduino PubSubClient

  1. Thank you James,
    How would I check to see if F63F1E is in the payload?
    {“Time”:”2020-06-17T14:49:19″,”RfReceived”:{“Sync”:12570,”Low”:450,”High”:1230,”Data”:”F63F1E”,”RfKey”:”None”}}
    Thanks,
    Dennis

  2. “The callback() function provides a character array called “topic.” You might be tempted to convert this into an Arduino String object. ”

    I think you’re confusing topic with incoming payload for that topic. You already know what your incoming topic subscriptions are, so no need to do any strcmp on those. You would want to compare the payload (coming in a a char array) from your MQTT topic to see if it has an actionable value

    • Nope. I was specifically using the example where you are subscribed to multiple topics.

      You already know what your incoming topic subscriptions are

      Nope. You have to check which topic generated the callback. In this example, pir1Status, red, blue, green. Inside of the if-statement with the strcmp, you would then look at the payload to see what the actionable value is associated with the topic that generated the callback.

  3. Hello, I have a project which involves up to 50 devices
    I want to receive and store the data gathered on PC.
    Here is my setup: 1 PC and 50 esp32 devices
    I want to get the esp32 device data when the PC ask for the specific device.

    Ex: The PC publishes a topic “home/data” with message “esp32-01” which is meant for esp32-01
    The esp32-01 would response with message “XXX” to the subscribed topic “home/data” and checking the message and confirming it is for itself.
    Then when the PC receive the message sending from esp32-01 it would store it somewhere
    The repetition go for esp32-01 to esp32-50….

    How do I go about doing it ?