Use MQTT's single-level wildcard to Subscribe to multiple Topics
MQTT's single-level wildcards allow you to Subscribe to multiple Topics simultaneously while using a specific Topic structure (they will follow any possible Subscription that would render the same Topic Structure) .
Single-level wildcards are represented with a plus sign (+). For example, if using the following Subscription Topic with a single-level wildcard:
Topic
/v1.6/devices/+/<variable_label>
Then the response will be updated with the Last Dot of a device which contains a variable with the same {VARIABLE_LABEL} as specified in the Topic and that has received a value.
Field | Value | Required |
---|---|---|
Host | industrial.api.ubidots.com | Yes |
Port | 1883/8883 (Non-TLS/TLS) | Yes |
Username | Your Ubidots Token | Yes |
Password | Any character or leave blank | No |
Quality of Service | 0 or 1 | No |
Examples
NoteThe bash example from this section makes use of the Eclipse Mosquitto library, if you wish to test it please install it using the official instructions.
If there are 3 Devices which contain the following Variables:
Device | Variables |
---|---|
device-a | temperature, pressure, humidity |
device-b | temperature |
device-c | pressure, humidity |
Using single-level wildcard Subscription to obtain a value/dot sent to a specified Variable to any Device
When using the following single-level wildcard Subscription Topic:
/v1.6/devices/+/pressure/lv
/v1.6/devices/+/pressure
Then the results would be as if multiple Subscriptions were made as following:
/v1.6/devices/device-a/pressure/lv
/v1.6/devices/device-c/pressure/lv
/v1.6/devices/device-a/pressure
/v1.6/devices/device-c/pressure
Hence, the response would be updated if either device-a or device-c receive a new value/dot for their pressure variable.
$ mosquitto_sub \
-h "industrial.api.ubidots.com" \
-t "/v1.6/devices/+/pressure/lv" \
-u "BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB" \
-p 1883 \
-q 1
-v
$ mosquitto_sub \
-h "industrial.api.ubidots.com" \
-t "/v1.6/devices/+/pressure" \
-u "BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB" \
-p 1883 \
-q 1
-v
Using single-level wildcard to obtain the last value/dot of any variable from a device when it is updated
Use the following Subscription Topic:
/v1.6/devices/device-a/+/lv
/v1.6/devices/device-a/+
Then the results would be as if multiple Subscriptions were made as following:
/v1.6/devices/device-a/temperature/lv
/v1.6/devices/device-a/pressure/lv
/v1.6/devices/device-a/humidity/lv
/v1.6/devices/device-a/temperature
/v1.6/devices/device-a/pressure
/v1.6/devices/device-a/humidity
Hence, the response would be updated if device-a receives a new value for either its temperature, pressure, or humidity variable.
$ mosquitto_sub \
-h "industrial.api.ubidots.com" \
-t "/v1.6/devices/device-a/+/lv" \
-u "BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB" \
-p 1883 \
-q 1
-v
$ mosquitto_sub \
-h "industrial.api.ubidots.com" \
-t "/v1.6/devices/device-a/+" \
-u "BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB" \
-p 1883 \
-q 1
-v