I'm using knolleary's excellent MQTT library, and after a long battle with random hanging after a period of time, and other failures (hardware/software on my part!) it's running beautifully stable now.
This is not really an MQTT question, nor really an Arduino question, but more of a programming question, although it relates a lot to MQTT library so hopefully someone here might help? Apologies in advance if I'm posting in the wrong place but it seems like a friendly community :)
My setup routine connects then subscribes to a topic
client.connect("arduinoClient");
delay(50);
client.publish("audio/home/bedroom/arduinovector", "trackplease");
delay(50);
client.subscribe("audio/home/bedroom/title");
delay(50);
The callback function looks like this:
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
msg = (char*)payload;
writetitle = msg;
titlelength = length;
Serial.println("Found a title update");
}
Again apologies for the question to follow, I've spent so many hours and now my hair is falling out!
All I want is to be able to listen to multiple topics. Here's what I've tried:
1) Change the subscribe from
to
so that I can listen to all topics in the bedroom
2) Change the callback to include two "selector" if statements like this:
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
String strTopic = String((char*)topic);
if (strTopic == "audio/home/bedroom/title") {
msg = (char*)payload;
writetitle = msg;
Serial.println("Found a title update");
}
if (strTopic == "audio/home/bedroom/vol") {
msg = (char*)payload;
writevol = msg;
Serial.println("Found a volume update");
}
}
Problem is, if I do a Serial.println(writetitle) in the loop, it shows payloads from ALL topics under audio/home/bedroom/#
Obviously the desired effect is that my writetitle variable *only* has track titles, and then my writevol variable only has volume updates!
Am I doing it all wrong?