MQTT Publish

You can invoke an UbiFunction by publishing data through MQTT. This makes it possible to integrate devices and gateways that may have a non-editable JSON payload that is not compatible with our native MQTT broker or our HTTP Data Ingestion API.

Because our UbiFunctions MQTT Broker is shared across all functions, it is not possible to support subscribe requests and only the MQTT Publish method is accepted. Accordingly, all subscriptions requests will be rejected.

If you'd like to have a private MQTT Broker mapped to an UbiFunction, please reach out to our sales team. In this case, subscribe actions can be supported.

Here are the MQTT settings needed to invoke an UbiFunction over the MQTT protocol:

MQTT Settinigs

Value

URL

functions.ubidots.com

Port

1883 for plain MQTT. 8883 for MQTT over TLS.

Topic

/prv/<your-username>/<function-label>

Username

Your account username

Password

Valid Token from your Ubidots account.

ClientID

Any random string. Preferably longer than 15 characters

Topic structure

To trigger an UbiFunction through MQTT, the publication topic must follow the structure:

/prv/<username>/<function-label>

Where:

  • <username> is your Ubidots account username.

  • <function-label> is the Name of the function but all lowercase and spaces replaced by dashes.

The base topic corresponds to the UbiFunction HTTPS Endpoint URL's path, as seen in the image below:

Advanced Topics

Additional topic levels are supported. To add more levels, simply add a forward slash ("/") after the base topic:

/prv/<username>/<function-label>/<level-1>/···/<level-N>

Where:

  • <level-N> is any additional UTF-8 encoded string that you want to add to the topic

Topics have the below limitions:

Topic Limits

Limit

Length

1500 characters

Levels

10

UbiFunction args

When triggering an UbiFunction through the MQTT protocol, you'll receive a JSON object in the args variable of the main function. The JSON will have this structure:

{
  "topic": "<topic>",
  "payload": "<payload>",
  "from_client_id": "<client_id>",
}

Where:

Key

Type

Value description

topic

String

The multilevel publication topic

payload

String

The payload sent by the device in the publication message.

from_client_id

String

The client ID used in the MQTT connection.

Certificates

Ubidots supports SSL V1.1, TLS V1.2 and V1.3. You can download our root certificates in different formats:

  • PEM file: Certificate chain with two root certificates from our certificate authorities (CAs).

  • DER file: Same as the PEM file, with an alternative encoding.

  • CRT file: Same as the PEM file, with a different extension. Often referred to as .crt, .cert or .cer.

Examples

mosquitto_pub \
-p 1883 \ 
-h functions.ubidots.com \
-t "/prv/<username>/<function-name>" \
-m '{"token": "TOKEN", "device": "device-label", "variable-label": value}' \
-u "<username>" \
-P "TOKEN" \
-q 1 -d

Last updated