MQTT is a machine-to-machine (M2M) and Internet of Things (IoT) publish/subscribe message transport protocol. The Brick MQTT Proxy provides access to Bricks and Bricklets over MQTT. It is written in Python and translates messages between the Tinkerforge TCP/IP Protocol (TFP) and MQTT.
Note
The Brick MQTT Proxy is discontinued and no further development will be done. The MQTT API bindings are the recommended replacement.
The proxy is provided as Python script and can be downloaded from its Brick MQTT Proxy GitHub Repository:
wget --backups=1 https://raw.githubusercontent.com/Tinkerforge/brick-mqtt-proxy/master/brick-mqtt-proxy.py
As dependencies the Brick Daemon, the Python API Bindings and the Eclipse Paho MQTT Python client library has to be installed. See the Brick Daemon and Python API Bindings documentation about how to install them. On Linux the Paho MQTT Python client library can be installed using the following command:
sudo pip install paho-mqtt
Afterwards the proxy can be startet with:
python brick-mqtt-proxy.py
Enter the following to get a list of command line arguments for host and port configuration for Brick Daemon and the MQTT broker, data update interval and debug output:
python brick-mqtt-proxy.py --help
In the following examples we use mosquitto as broker and client. Other broker and clients can be used similarly. To install the broker and client software under Linux do the following:
sudo apt-get install mosquitto mosquitto-clients
Afterwards start the Brick MQTT Proxy and execute the following examples. The structure is described below. Keep in mind to change the UID in the path to that of your Bricklet.
# enumerate all available devices
mosquitto_sub -v -t tinkerforge/enumerate/available/#
# enumerate all available Laser Range Finder Bricklets
mosquitto_sub -v -t tinkerforge/enumerate/available/bricklet/laser_range_finder
# enable laser
mosquitto_pub -t tinkerforge/bricklet/laser_range_finder/vbM/enable_laser/set -m ''
# get distance
mosquitto_sub -v -t tinkerforge/bricklet/laser_range_finder/vbM/distance
# enumerate all available Analog Out 2.0 Bricklets
mosquitto_sub -v -t tinkerforge/enumerate/available/bricklet/analog_out_v2
# get input voltage
mosquitto_sub -v -t tinkerforge/bricklet/analog_out_v2/7xwQ9g/input_voltage
# get output voltage
mosquitto_sub -v -t tinkerforge/bricklet/analog_out_v2/7xwQ9g/output_voltage
# set output voltage to 2.5V
mosquitto_pub -t tinkerforge/bricklet/analog_out_v2/7xwQ9g/input_voltage -m '{"voltage":2500}'
The topics are split into device (currently only bricklet/
) and
enumerate/
topics.
The proxy publishes retained messages about value and configuration changes of supported devices on topics with the following pattern:
tinkerforge/<prefix>/<uid>/<suffix>
For example, for a Temperature Bricklet with UID XYZ
the temperature value
is published on:
tinkerforge/bricklet/temperature/XYZ/temperature
The value and configuration information is represented in JSON with the following format:
{
"_timestamp": <timestamp>,
"<key>": <value>
}
All messages published by the proxy include a UNIX timestamp to indicate the
age of the provided information. The naming and meaning of the key-value pairs
matches the payload definition of our TCP/IP protocol.
All key-value pairs added by the proxy start with an underscore to avoid name
collisions. For example, for a Temperature Bricklet with UID XYZ
the
temperature value is published as:
{
"_timestamp": 1440083842.785104,
"temperature": 2343
}
The proxy subscribes to topics ending in /set
allows you to change the
configuration of a device. For example, to change the configuration of an
Ambient Light Bricklet 2.0 with UID ABC
the following JSON payload:
{
"illuminance_range": 1,
"integration_time": 2
}
Can be published to this topic:
tinkerforge/bricklet/ambient_light_v2/ABC/configuration/set
The proxy parse the payload and call the configuration setter accordingly.
Again, the naming and meaning of the key-value pairs matches the payload definition of our TCP/IP protocol. In this case the illuminance range is set to 32000lux and the integration time is set to 150ms.
There are three major enumerate/
subtopics the proxy will publish enumerate
events on:
tinkerforge/enumerate/available/<device-topic-prefix>
: If the list of
available devices with a matching topic prefix changes then a retained message
with the updated list of all now available devices is published.tinkerforge/enumerate/connected/<device-topic-prefix>
: If a new device
with a matching topic prefix gets connected then a message with information
about the connected device is published.tinkerforge/enumerate/disconnected/<device-topic-prefix>
: If a known
device with a matching topic prefix gets disconnected then a message with
information about the disconnected device is published.For example, if an LCD 20x4 Bricklet gets connected then this is published on:
tinkerforge/enumerate/connected/bricklet/lcd_20x4
Also, the retained message on tinkerforge/enumerate/available/bricklet/lcd_20x4
is updated. The device information is represented in JSON for all subtopics with
the following format:
{
"_timestamp": <timestamp>,
"uid": "<uid>",
"connected_uid": "<connected_uid>",
"position": "<position>",
"hardware_version": [<major>, <minor>, <release>],
"firmware_version": [<major>, <minor>, <release>],
"device_identifier": <device_identifier>
}
For a Temperature Bricklet it looks like this:
{
"_timestamp": 1440143404.176469,
"uid": "se3",
"connected_uid": "5VihSm",
"position": "c",
"hardware_version": [1, 1, 0],
"firmware_version": [2, 0, 3],
"device_identifier": 216
}
The table below shows all supported devices with their names, suffixes and links to the corresponding TCP/IP protocol documentation for details about the payloads.
The Brick MQTT Proxy is designed to be easily extendable for other Bricks and
Bricklets. Take a look in the
source of the script.
To add other products you will have to implement your own proxy class derived
from DeviceProxy
class. Comments in the code describe the necessary
structure.