All Collections
Technical Resources
Controlling Devices using MQTT
Controlling Devices using MQTT

Learn to control devices directly from an Ubidots dashboard using MQTT

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

Using the Control widget in an Ubidots dashboard lets developers easily control system or environment. The 2 available control widgets in Ubidots application builder are "Switch" and "Slider":

Learn more about Automation with Ubidots by creating activation triggers in the Ubidots Events Engine. 

To control any device from your Ubidots Dashboard, follow the below steps carefully. 

Step-by-Step

  1. Create a Control Variable

  2. Add a Control Variable to the Dashboard

  3. MQTT Control Sample Code

Step 1: Create a Control Variable

Sign in into your Ubidots account, or start your free 30 day trial here.

1. Go to "Devices" section by clicking on the "Devices" tab. And, create a new device simply with the "+" icon located in the top-right of the interface.

2. To control any device, we need to first create a variable in the device. Accordingly, let's create a "Blank" device 

3. Then, enter to the device just created to add a new variable. Select "Add variable" and create a new "Raw Variable."

4. Assign a name for the variable, in this case we call it "switch".

IMPORTANT NOTE: The Variable API Label is the identifier that the sample codes below use to control both the end-device and the control widget on the dashboard.

2. Add a Control Variable to the Dashboard

1. Go to dashboard section clicking on the "Dashboard" section of the "Device Management" tab in the main Admin Navbar.

2. On the top-right part of the interface, you'll find the "+" icon below for create a widget.

3. On the section "How would you like to see your data?" select "Control". We have two type of widget to control your devices, "Switch" and "Slider". For this example we're going to use "Switch".

4. Select the device and variable that you want control. In this tutorial we will select the previously created "New Device" and "switch" and press "Finish".

5. Now you'll be able to visualize the widget created. Once, you finish this guide you are going to control your device through this switch. 

3. MQTT Control Sample Code

The below guide is a simple MQTT example using the "MQTTLens" simulator.

Firstly, the Ubidots MQTT broker is available at the following URL:

industrial.api.ubidots.com

 Port: 1883

NOTE: For more information about the Ubidots MQTT API Reference, please click here.  

To interact with Ubidots MQTT Broker, you will need a TOKEN. The easiest way to locate your unique TOKEN, select “API Credentials” from your user dropdown or click here for additional instructions:

IMPORTANT NOTE: To connect to our MQTT broker, use your Ubidots TOKEN as the MQTT username, and password.

MQTT Credentials       Ubidots
       username         –> TOKEN
       password         –> TOKEN

1. Download and open the MQTTLens and select the "+" icon pictured below to "Add a connection."

2. Complete the provided fields with the required information below:

  • Connection name: Ubidots Broker (also, you can put the name that you desire) 

  • Hostname: industrial.api.ubidots.com 

  • Port: 1883 

  • Username: Ubidots TOKEN 

  • Password: Ubidots TOKEN 

After completing each field, the connection details should look as follows:

Once you have completed all fields, select "CREATE CONNECTION"

Once the MQTT client is successfully authenticated and connected you should be able to see the green icon at the right side of the client name assigned, in this case "Ubidots Broker"

Now you are able to subscribe to a variable when the variables receives data.

3. Assign the TOPIC desired to establish the subscription on the Subscribe field:

  • TOPIC structure format:

/v1.6/devices/{LABEL_DEVICE}/{LABEL_VARIABLE}/lv

This will return the last value of the variable as a single float value.

Following the guide, our LABEL_DEVICE should be "new-device" and LABEL_VARIABLE should be "switch", as you can see on the image below:

After assigning the TOPIC to the subscribe field press the "SUBSCRIBE" button:

Now you are able to receive messages from Ubidots once the control widget has been clicked on or off:

If you're a visual learner, also you can take a look of this video tutorial.

NOTE: To learn more about our MQTT requests, please read the Documentation.

Did this answer your question?