MQTT message integration

Hi,
I have tried as many MQTT examples as I can find and always hit the same brick wall.

I can get connected and I can publish messages which at least proves fundamentally I have a working system.

My problem is in the Arduino code to unpack the message data from a subscribed topic.
I have not uploaded code because what I am hoping is a pointer to a couple of known good examples which I can work with.

My experience has been...

  1. if my topic receives a single numerical digit (0 through 9) I am fine but as soon as I try and unpack a 23 for example, the example I was following doesn't work. Although I can see that the code is meant to loop through the received numbers and stitch it back together to be a 23.

  2. if my topic receives a string of characters I have great difficulty looping through the received characters and putting it back together. So a simple on or off would be a struggle.

So my question really is, who has found a great robust example for reference or which of the Arduino functions do you recommend I try to use that will provide a reliable and robust solution?
Thanks in advance
Richard

are you using pubsubclient?

are you using a callback function with this signature?

void callback(const char[] topic, byte* payload, unsigned int length)

what is really in your payload? (a null terminated string, a binary representation of your number,...?)

Jackson,
Thanks for your reply and I should have added a little more context.
Yes I am using the pubsubclient addin as part of the examples I have found.

The void callback is also in the examples I have tried. I have seen a few versions which seem to use different code examples to loop through the received characters/digits and re-assemble them back into a variable.

I'm confident the data is landing in the specified subscribed topic but it seems to be the 2 or 3 lines trying to put the characters/digits back together which trip me up.
Unfortunately I am not a great programmer and I can't figure out the issue.
When sending digits maybe they are interpreted as characters, I really don't know but 0 through 9 works okay.

To be honest, I will make the message content match a working example. I can publish what I want it has just been that no matter what I publish I seem to be struggling matching it back in a loop to an action.

I'm also not sending a lot or often so it can be a little crude, it just needs to be robust in the outcome.
Thanks again.
Richard

if you do this in your callback

void callback(const char[] topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] with ");
  Serial.print(length);
  Serial.print(" bytes => ");

  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
    Serial.print("(0x");
    Serial.print(payload[i], HEX);
    Serial.print(") ");
  }
  Serial.println();
}

what do you see in the serial monitor?

Oh, thank you, I will have a look as soon as I get home.
Thanks.
RR

I am getting a compile error that the topic has not been declared.

However I do have this...

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }

I guess the question if you had time was which of the example mqtt programs would you suggest I start with and copy your example into?

There are so many which is fuelling my confusion. I seem to be chasing butterflies.
Cheers
Richard

please post the full code. can't help on snippets (Snippets R Us!)

if you want an example, use the one from the library like https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_basic/mqtt_basic.ino

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.