I currently use the below callback to check the PAYLOAD of incoming MQTT messages, but does anyone know how I could continue to do this but also find messages coming under a specific TOPIC?
void callback(char * topic, byte * payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
if (!strcmp(p, "home")) {
Particle.publish(DEVICE_NAME, HOME_MSSG, 60, PRIVATE);
} else if (!strcmp(p, "chome")) {
Particle.publish(DEVICE_NAME, CHOME_MSSG, 60, PRIVATE);
}
}
The topic can be handled in pretty much the same way as the payload; e.g.
if (!strcmp(topic, "thisIsATopic")) {
// do something
}
Note that the payload is copied for two reasons:
\0
is added to the end if using functions like strcmp
(to avoid overruns).It looks like the library you are using copies the topic so you should be fine using that as-is (unlike with some other libraries).