MQTT Subscribe issues

Hi,
I am working with Boron, Before with the community help I am able to successfully publish the payload to "TopicA"our company dashboard. In the dashboard, I am measuring the tank level with hardcore height parameters. Now, I am making the dashboard which is independent of the hardcore height parameters and ask from customers. My idea is that publish these values to the “Topic B” from dashboard. and subscribe to the “Topic B” from the webIDE and then assign these values to the variable "X"and pass this variable as a payload to the “Topic A”.
My Question is that

  1. Is assigned variable “X” able to get the new coming values from “Topic B”. I am trying to using like height=client.subscribe(“Topic B”) but it return the 1 all the time doesn’t matter i updated or not.
#include "MQTT.h"

void callback(char* topic, byte* payload, unsigned int length);

MQTT client("broker.hivemq.com", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = '\0';

    RGB.control(true);
    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else {
        Serial.printlnf("Unknown message: %s", p);
        RGB.control(false);                         // give up RGB control
    }
}

bool mqttConnect() {                                // one simple function to easily (re)connect at multiple places in code
    client.connect("sparkclient");
    if (client.isConnected()) {
        client.subscribe("outTopic/message");
        return true;
    }
    
    return false;
}

void setup() {
    Particle.function("mqttPub", mqttPub);          // test function (send RED, GREEN, BLUE via console)
    mqttConnect();                                  // initial connection
}

void loop() {
    if (client.isConnected()) 
        client.loop();
    else {                                          // when connection is lost, tell us so and reconnect
        Serial.println("connection lost - reconnect");
        mqttConnect();
    }
}

Thanks

It is possible, but the data received has to be handled in the callback() function according to the topic parameter received.
Catching the return value of the client.subscribe() call will only tell you whether the subscription was successfully hooked up.

Hi,
I am trying to read the "Client. subscribe("Topic B") and want to publish the content in Topic B to Topic A. I found that client.subscribe is bool function . How we can access the message in it.Can anyone guide me in this regards?
I am trying with

`int value= client.subscribe("Topic B");

client.publish("Topic A", value);`

The publish output is 1 no matter what the content is in the Topic A
Thanks

See my post above.

Thank You for reply. Can you please advise me how we can access that call back function. I have observed in serial monitor that I am not seeing any data coming in payload

void callback(char* topic, byte* payload, unsigned int length) {
    String response;
    for (int i=0;i<length;i++)
    {
        response+=(char)payload[i];
    }
    Serial.println(response);
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = '\0';
    RGB.control(true);
    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else {
        Serial.printlnf("Unknown message: %s", p);
        RGB.control(false);                         // give up RGB control
    }
}

I Tried with the following code in loop function as well. It comes 868661 in the serial screen

   int brightnes=(int) callback;
   Serial.println(brightnes);

This will only give you the address of the function in memory.

Then you most likely have not subscribed to the correct topic.

In your previous code you have

hence you will only see the callback triggered when that topic is sent. You haven't subscribed to Topic B (having blanks in a topic is no good idea anyhow) but publish Topic A it should be no surprise not to get this in the callback.

Yes, you are right, But I successfully subscribed to the TopicA where I am sending the data. In command Prompt I am able to see the coming data in the TopicA. I am also able to see that the device is successfully subscribe with the TopicA and print 1 (successful) in the serial monitor. I don’t know why it is not showing the payload.

bool mqttConnect() {                                // one simple function to easily (re)connect at multiple places in code
    client.connect("sparkclient");
    if (client.isConnected()) {
        client.subscribe("TopicA");
        return true;
    }
    
    return false;
}
inline void softDelay(uint32_t msDelay)
{
  for (uint32_t ms = millis(); millis() - ms < msDelay; Particle.process());
}

void setup() {
    Particle.function("mqttPub", mqttPub);          // test function (send RED, GREEN, BLUE via console)
    mqttConnect(); 
    	Particle.function("Postperday",ledToggle);
	Particle.function("UTCoffset",UTC);

	// initial connection
}

void loop() {
     mqttConnect();
   
    String myID = System.deviceID();
    int i=mqttConnect();
    Serial.println(i);
}

The below picture showing the subscribe TopicA

The below picture of Boron successfully connected to the TopicA

I updated the Callback()

void callback(char* topic, byte* payload, unsigned int length) {
    String response;
    for (int i=0;i<length;i++)
    {
        response=response+(char)payload[i];
    }
    Serial.printlnf("%s",response);
    Serial.print(topic);
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = '\0';
    RGB.control(true);
    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else {
        Serial.printlnf("Unknown message: %s", p);
        RGB.control(false);                         // give up RGB control
    }
}

It doesn’t print anything about Topic as well. I looks like the call back function is not working

You keep changing too many things at once and that doesn't really help you understand what the changes do nor us to understand where you are going.

You should stick with

In your latest iteration you are permanently reconnecting even when there is no need to and even calling it multiple times in one iteration of loop()

With mosquitto_sub you are also subscribing to a different (i.e. wildcarded) topic while you are only subscribing to TopicA exactly.
Why not try the same subscription in both cases???

Nope, it's still the same issue: The callback is not called since you are subscribing to the wrong topic.

1 Like

Sorry to make a mess for you. Thank you for your help and I am able to access the message in call back. There was a problem with my loop()function.
Thanks