Publish data to a Variable

This topic lets you publish one or more dots to a variable in a device.

To send data to one variable, publish to the following topic with these parameters:

Topic

/v1.6/devices/<device_label>/<variable_label>
FieldValueRequired
Hostindustrial.api.ubidots.comYes
Port1883/8883 (Non-TLS/TLS)Yes
UsernameYour Ubidots TokenYes
PasswordAny character or leave blankNo
Quality of Service0 or 1No

Payload

The accepted message payloads for different cases are as follows:

{"value": 27}
{
  "value": 10,
  "timestamp": 1634311791000,
  "context": {
    "status": "cold"
  }
}
[
  {
    "value": 27,
    "timestamp": 1634311791000
  },
  {
    "value": 28,
    "timestamp": 1634311435000
  },
  {
    "value": 34,
    "timestamp": 1634876591000
  }
]
{
  "value": 1, 
  "context": {
    "lat": "6.5423", 
    "lng": "-70.5783"
  }
}
📘

timestamp and context

The timestamp and context parameters are optional.

🚧

Timestamp Format

All timestamps must be in milliseconds. You can convert dates to timestamp values here.

Sending location data

You may use the context key to send location data. Ubidots will automatically recognize location data when using these variable labels:

  • position
  • location
  • gps

There are three ways to send location data:

{
  "position": {
    "value": 1, 
    "context": {
      "lat": "6.5423", 
      "lng": "-70.5783"
    }
  }
}
// Because the "value" of a dot is mandatory, this option sets the variable value to "1".

{
  "position": {
      "lat": "6.5423", 
      "lng": "-70.5783"
    }
}
// Because the "value" of a dot is mandatory, this option sets the variable value to "1".

{
  "position": {
      "latitude": "6.5423", 
      "longitude": "-70.5783"
    }
}
👍

Location Data

You have several ways to include latitude and longitude when sending your dots to Ubidots. Please refer to this documentation.

Examples

📘

Note

All Bash examples in this section use the Eclipse Mosquitto library. To test them, install it using the official instructions.

Send a value to a variable
Update a variable and let Ubidots apply a timestamp equal to the reception time:

$ mosquitto_pub \
-h 'industrial.api.ubidots.com' \
-t '/v1.6/devices/weather-station/temperature' \
-u 'BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB' \
-p 1883 \
-q 1 \
-m '{"value": 27}'

Send multiple values to a variable
Update a variable with multiple values, each containing a specified timestamp:

$ mosquitto_pub \
-h 'industrial.api.ubidots.com' \
-t '/v1.6/devices/weather-station/temperature' \
-u 'BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB' \
-p 1883 \
-q 1 \
-m '[
  {
    "value": 27,
    "timestamp": 1634311791000
  },
  {
    "value": 28,
    "timestamp": 1634311435000
  },
  {
    "value": 34,
    "timestamp": 1634876591000
  }
]'

Send a dot to a single variable
Update a variable with a complete dot by specifying context and/or timestamp:

$ mosquitto_pub \
-h 'industrial.api.ubidots.com' \
-t '/v1.6/devices/weather-station/temperature' \
-u 'BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB' \
-p 1883 \
-q 1 \
-m '{
  "value": 10,
  "timestamp": 1634311791000,
  "context": {
    "status": "cold"
  }
}'

Send location data
Update a position variable so that the device displays its location:

$ mosquitto_pub \
-h 'industrial.api.ubidots.com' \
-t '/v1.6/devices/weather-station/position' \
-u 'BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB' \
-p 1883 \
-q 1 \
-m '{
  "value": 1, 
  "context": {
    "lat": "6.5423", 
    "lng": "-70.5783"
  }
}'
$ mosquitto_pub \
-h 'industrial.api.ubidots.com' \
-t '/v1.6/devices/weather-station/position' \
-u 'BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB' \
-p 1883 \
-q 1 \
-m '{
  "lat": "6.5423", 
  "lng": "-70.5783"
}'
$ mosquitto_pub \
-h 'industrial.api.ubidots.com' \
-t '/v1.6/devices/weather-station/position' \
-u 'BBFF-Rfcgaxns6HlVb155WA0RhSY85xNDmB' \
-p 1883 \
-q 1 \
-m '{
  "latitude": "6.5423", 
  "longitude": "-70.5783"
}'
📘

Same timestamp for multiple values

If you need the same timestamp for multiple values, publish data with a global timestamp to save payload space. For more instructions, see Publish data with global timestamp.