[SOLVED] ESP32 Subscribing via MQTT

Hi,

I am trying to subscribe to a variable on my ubidots device which is controlled by a slider that ranges from 0 to 100. I am currently using the ESP32 development board and the following libraries: WiFi.h and PubSubClient.h .

So far I have only been able to publish four variables to my ubidots device but I have not had any luck subscribing. I have had a good look at the examples in the ubidots API and other examples as well but nobody really explains in detail how it all works. The closest thing I found was an example that allowed the user to control a relay but this has not helped me understand the subscribe procedure either.

Does anyone have any SIMPLE bare bones example that subscribes to a variable on ubidots and prints its value to the serial monitor?? If I have that, I would be able to take it from there.

Thank you for you help

Hi @Masso_RainCloud,

Please refer to section 4 of the below article. It addresses subscription to Ubidots from an ESP32, and it is proven to work.

Furthermore, if you want to simply subscribe to a variable on Ubidots and print its value to the serial monitor, use the following callback function instead of that in the article:

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

–David

Hi @dsr,

I followed the tutorial in your reply.

I am able to subscribe and receive data from Ubidots for single topic. However if I subscribe two topics to a single client, only last subscription is valid. No data is received from first subscription. Here is the code:

sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, "relay");
client.subscribe(topicSubscribe);

 sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, "servopos");
 client.subscribe(topicSubscribe);

What could be the possible cause? Thanks

Hello @Casey

You need to clean the topicSubscribe variables before the second variable to subscribe, or have a separate variable for the second topic. Either way, here are the 2 examples:

  1. Cleaning the topicSubscribe variable before the second subscription:
sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, "relay");
client.subscribe(topicSubscribe);

sprintf(topicSubscribe, "");

sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, "servopos");
client.subscribe(topicSubscribe);
  1. Having a second variable for the second topic to subscribe: note that in the second subscription I didn’t recycle the topicSubscribe but instead used topicSubscribe2. Remember to declare topicSubscribe2
sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, "relay");
client.subscribe(topicSubscribe);

sprintf(topicSubscribe2, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, "servopos");
client.subscribe(topicSubscribe2);

–David

2nd example solve my problem.

Thanks for the help.

hellon @Casey, I have the same problem, I already tried the second example but the data is still shown in the same variable, could you show me your code. I hope you can help me.

Hello @gerry ,

David’s response had a subtle mistake, and the second subscribe client was assigning its value to the same variable as the first, which explains what was happening to you.

He has corrected the mistake and now the second subscribe outputs its value to the second declared value. Makes sense?

Best regard,
Sebastián