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.
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.

