So i have a project and im able to subscribe to a value and use it in my code. But i need to subscribe to a second, and maybe more things. right now i need to subscribe to widgets on the ubidots dashboard. this is easy alone, but when there is more than one thing to subscribe on i get confused. I have seen some example codes on here, but they use multiple clients. I have seen example code for this here: Create a digital Piano with Ubidots and a NodeMcu | Ubidots Help Center. But as said, is there a way to use a single client and subscribe to multiple values/widgets?
Hello @andylord ,
Thank you for reaching out.
Yes, you can subscribe to multiple values in Ubidots with only one client. To achieve it, please follow these steps:
- You need to subscribe to the corresponding variable in the setup function and in the loop function. For instance:
void loop()
{
// put your main code here, to run repeatedly:
if (!ubidots.connected())
{
ubidots.reconnect();
ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LABEL_1);
ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LABEL_2);
}
ubidots.loop();
}
- You need to compare the topic in the callback function. That way, you’ll be sure from which variable you’re receiving the data, this comparison can be done following this syntaxis:
if( String(topic) == "/v1.6/devices/{DEVICE_LABEL}/{VARIABLE_LABEL}/lv"){
//write your code
}
Where {DEVICE_LABEL}
is the label of the device and {VARIABLE_LABEL}
is the label of the variable. To obtain more information, please click here .