Arduino MQTT - how to subscribe to multiple topics and save values

6,495 views
Skip to first unread message

Mat Smith

unread,
Sep 11, 2014, 5:45:28 PM9/11/14
to mq...@googlegroups.com
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
audio/home/bedroom/title
to
audio/home/bedroom/#

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?

Ian Craggs

unread,
Sep 17, 2014, 4:56:19 PM9/17/14
to mq...@googlegroups.com
So unfortunately I'm probably not going to be very helpful :-(    Have you printed out strTopic to see that it contains what you expect?  How about changing the order of the two check blocks - shouldn't change the behaviour, but if it does, something is happening you're not expecting.

Have you tried asking your question on Github or Nick's own website?  This group is generally for MQTT general questions.  

Not that this is likely to help much either, but the Paho C++ embedded client library now has an Arduino interface too. 


which I will support.

Ian

Nicholas O'Leary

unread,
Sep 17, 2014, 4:59:50 PM9/17/14
to mq...@googlegroups.com
Hi Ian,

I told Mat we'd take this off-list. Unfortunately, I've not had a chance to dig into it yet. I suspect the issue is it's not taking a copy of the payload in the callback. The code is pointing at the internal buffer that'll get overwritten with the next packet that arrives.

Nick



--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at http://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.

ffro...@gmail.com

unread,
Feb 16, 2019, 7:32:32 AM2/16/19
to MQTT
hi i have your same situation, could you give him some solution?
Reply all
Reply to author
Forward
0 new messages