Create a Control Variable

Sign in into your Ubidots account, if you don't have on creat one clicking here.

1.- Go to device section cliking on the "Device" tab.

2.- Create a new device, just press "Add Device".

3.- Once the device is  created, you should see something like this:

4.- Enter to the device just created to add a  new variable. Select "Add variable".

5.- Assign a name for the variable, in this case we decided call it "switch". Also, on the icon below you'll able to know the Variable ID.

Add Control Widgets to your Dashboard

1.- Go to dashboard section cliking on the "Dashboard" tab.

2.- On the right part of the page, 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 case the ones we created before, 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. 

MQTT Example

We're going to show you a simple MQTT example using "MQTTLens".

Firstly, you have to know that our MQTT broker is available at the following URL:

things.ubidots.com

 port: 1883

To interact with it, you will need a TOKEN. The easiest way to get yours is clicking on “API Credentials” under your profile tab:

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

MQTT Credentials       Ubidots
       username         –> TOKEN
       password         –> TOKEN


1.- Open the MQTTLens  and select the icon below to "Add a connection"

2.- You have to complete the below fields with all required information below:

  • Connection name: mqtt example (also, you can put the name that you desire) 
  • Hostname: things.ubidots.com
  • Port: 1883
  • Username: Ubidots TOKEN
  • Password: Ubidots TOKEN

Once you finished complete the fields, select "CREATE CONNECTION".

Once the MQTT client is successfully authenticated you should be able to subscribe to a variable so you can get notified when the variables receives data.

3.- You have to assing the TOPIC on the Subscribe field:

/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 "my-new-device" and LABEL_VARIABLE should be "switch", as you can see on the image below. 

After assign the TOPIC to the subscribe field press the "SUBSCRIBE" button, then you'll be able to receive the messages from Ubidots.

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.

HTTP Example

To get the values of a variable using its label, make a GET request to:

https://things.ubidots.com/api/v1.6/devices/{LABEL_DEVICE}/{VARIABLE_LABEL}/values

Following the guide, our LABEL_DEVICE should be "my-new-device" and LABEL_VARIABLE should be "switch".

HTTP Request to get values using the variable’s label

GET /api/v1.6/devices/{LABEL_DEVICE}/{VARIABLE_LABEL}/values?token={TOKEN} HTTP/1.1
Host: things.ubidots.com

Example request to get values using the variable’s label

# Request: Retrieve all the values of a variable with label "switch" and datasource with label "my-new-device":

curl -X GET https://things.ubidots.com/api/v1.6/devices/my-new-device/switch/values?token=9DriKmYujOJZ2x6zxds37376xbFtaSLvq0

# Sample Response

[
  {
    "url":
    "https://things.ubidots.com/api/v1.6/values/58e261267625426043512ef5",
    "value": 1.0,
    "timestamp": 1491231014797,
    "context": {},
    "created_at": "2017-04-03T14:50:14.797"},
  {
    "url":
    "https://things.ubidots.com/api/v1.6/values/58e261257625426043512eed",
    "value": 0.0,
    "timestamp": 1491231013994,
    "context": {},
    "created_at": "2017-04-03T14:50:13.994"},
  {
    "url":
    "https://things.ubidots.com/api/v1.6/values/58e260dc7625426043512b2c",
    "value": 1.0,
    "timestamp": 1491230940787,
    "context": {},
    "created_at": "2017-04-03T14:49:00.787"
  }
]

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

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

Did this answer your question?