Skip to main content
All CollectionsUser Guides
UbiFunctions: Publish data to your UbiFunction using MQTT
UbiFunctions: Publish data to your UbiFunction using MQTT

An MQTT+UbiFunction gateway acting as a middleware between MQTT devices and Ubidots

Sergio M avatar
Written by Sergio M
Updated over 4 months ago

This article is based on UbiFunction's official documentation, which can be found in our Dev Center.

Requirements

  • Ubidots account: trial or licensed

  • At least one UbiFunction already created (NOTE: if you don’t have one yet, follow this article first)

1. Introduction

MQTT + UbiFunctions allows you to publish data through an UbiFunction created in your account and make possible to integrate devices that transmit over this protocol but send JSON payload whose format is not compatible with our native MQTT broker or HTTP Data Ingestion API, or simply if additional data processing is required prior to being sent to Ubidots.

As UbiFunctions acts as a middleware here, only the MQTT Publish method is accepted. Accordingly, all subscriptions messages will be rejected.

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

MQTT Settinigs

Value

Host

functions.ubidots.com

Also found in some MQTT clients as:

tcp://functions.ubidots.com

Port

1883 for plain MQTT.
8883 for MQTT over TLS.

Topic

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

Username

Your account username

Password

Valid Token from your Ubidots account.

ClientID

Any random string. Preferably longer than 15 characters

Protocol

tcp

Note: Some MQTT clients offer the possibility to connect using websockets (shown as "ws://" or "wss://"), this is not supported.

2. Topic structure

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

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

  • <username> : Your Ubidots account username.

  • <function-label> : Name of the UbiFunction 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 support additional levels, separated by forward slash ("/"), beyond the ones used to identify the UbiFunction within the account. The structure is:
/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 limitations:

  • Length: 1500 characters

  • Levels: 10

3. UbiFunction arguments

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 the keys correspond as follows:

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.

4. TLS 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.

5. Examples

  • Port 1883:

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
  • Port 8883:

mosquitto_pub \
-p 8883 \
-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 \
--cafile /Users/user/Documents/Ubidots/roots.pem
Did this answer your question?