### About

TinkerForge Brick MQTT Proxy Documentation
http://www.tinkerforge.com/en/doc/Software/Brick_MQTT_Proxy.html#brick-mqtt-proxy-topics-and-payload-structure
TinkerForge Brick MQTT Proxy Download
https://github.com/Tinkerforge/brick-mqtt-proxy
For detailed description on how to setup, see the B4J HowTo Raspberry Pi > MQTT Setup.
It covers TinkerForge Brick MQTT Proxy, B4X jMQTT Client Library and Domoticz MQTT.
Install / Update
//Get the latest paho-mqtt
sudo pip install paho-mqtt
//IMPORTANT: This ensures the latest libraries are installed which are used by the proxy.
sudo pip install tinkerforge --upgrade
//Set the MQTT folder, e.g.
cd /home/pi/tinkerforge/mqtt
//Remove the existing script else the new scropt will be called like brick-mqtt-proxy.py.1
rm brick-mqtt-proxy.py
//Get the latest version from github
wget https://raw.githubusercontent.com/Tinkerforge/brick-mqtt-proxy/master/brick-mqtt-proxy.py
TinkerForge Brick MQTT Proxy Hints
Note:
Included are also hints for use with B4J.
These require the MQTT client from the jMQTT library, like Private mqtt As MqttClient
Start the Brick MQTT Proxy as Process
cd /home/pi/tinkerforge/mqtt
python brick-mqtt-proxy.py&
Ensure to add the & at the end of the script name to start as process.
Change the Brick MQTT Proxy Interval
The default interval is 3 seconds, change to 1 min by setting value to 60
python brick-mqtt-proxy.py --update-interval 60 &
Start the Brick MQTT Proxy at Reboot using a Script (Option 1)
//Create a folder /home/pi/scripts
mkdir /home/pi/scripts
cd /home/pi/scripts
//Create script with content. Ensure to add the & at the end of the script name to start as process.
sudo nano brick-mqtt-proxy.sh
cd /home/pi/tinkerforge/mqtt
python brick-mqtt-proxy.py&
//Make the script executable
sudo chmod 755 brick-mqtt-proxy.sh
//Add line to crontab
sudo crontab -e
@reboot sudo /home/pi/tinkerforge/mqtt/brick-mqtt-proxy.sh
Start the Brick MQTT Proxy during Boot (Option 2)
Add following to the file /etc/rc.local via $sudo nano /etc/rc.local
TinkerForge MQTT Broker
cd /home/pi/tinkerforge/mqtt
'python brick-mqtt-proxy.py&
Default interval is 3 seconds, change to 1 min by setting value to 60
python brick-mqtt-proxy.py --update-interval 60 &
printf "TinkerForge MQTT Broker started"
Connect to a Broker
Ensure a broker is installed, like Mosquitto.
Install:
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
Subscribe & Publish case setting
Ensure to use lowercase for all commands, e.g
tinkerforge/bricklet/rgb_led/zJW/rgb_value
Note:
The previous command returns topic=payload, e.g.
tinkerforge/bricklet/rgb_led/zJW/rgb_value={"r":0,"_timestamp":1474550970.679011,"b":0,"g":0}
Get all Bricklets connected to a Master Brick
Raspberry Pi Command:
mosquitto_sub -v -t tinkerforge/enumerate/available/#
Output Example for a Humidty Bricklet
tinkerforge/enumerate/available/bricklet/humidity
[{
"_timestamp":1445765987.530884,
"uid":"kcz",
"hardware_version":[1,1,0],
"device_identifier":27,
"connected_uid":"5VGrDJ",
"position":"a",
"firmware_version":[2,0,2]
}]
Output Example for several Bricklets connected to a Master brick
tinkerforge/enumerate/available/bricklet/humidity [{"_timestamp":1445765987.530884,"uid":"kcz","hardware_version":[1,1,0],"device_identifier":27,"connected_uid":"5VGrDJ","position":"a","firmware_version":[2,0,2]}]
tinkerforge/enumerate/available/bricklet/temperature [{"_timestamp":1445765987.541225,"uid":"nfu","hardware_version":[1,1,0],"device_identifier":216,"connected_uid":"5VGrDJ","position":"b","firmware_version":[2,0,3]}]
tinkerforge/enumerate/available/bricklet/lcd_20x4 [{"_timestamp":1445765987.555978,"uid":"rTS","hardware_version":[1,2,0],"device_identifier":212,"connected_uid":"5VGrDJ","position":"c","firmware_version":[2,0,6]}]
tinkerforge/enumerate/available/bricklet/barometer [{"_timestamp":1445765987.591564,"uid":"jXZ","hardware_version":[1,0,0],"device_identifier":221,"connected_uid":"5VGrDJ","position":"d","firmware_version":[2,0,2]}]
Python Example
This is a simple example to subscribe to a TinkerForge Topic and log the Payload result.
Uses TinkerForge Bricklet Temperature connected to a Raspberry Pi.
File tfsubscribetemp.py
#!/usr/bin/env python
#Python HowTo TinkerForge MQTT example for a Temperature bricklet
#2015 by rwbLinn
import paho.mqtt.client as mqtt
import json
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("tinkerforge/bricklet/temperature/nfu/temperature")
#Handle the mqtt messages
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload));
#parse the payload json string
j = json.loads(str(msg.payload));
#get the temp as a flow. note to divide by 100 as a float to get a float ;-)
t = j['temperature'] / float(100)
#convert float temp as string with 2 digits
ts = str("%.2f" % t)
print("T: " + ts + "C")
#define the mqtt client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
#connect to the broker - can also be another ip
#after connection handle the functions on_connect and on_message
client.connect("localhost", 1883, 60)
#loop - stop by pressing two times ctrl+c
client.loop_forever()
Run in a Terminal
python tfsubscribetemp.py
Output Example:
pi@raspberrypi ~/python/mqtt $ python tfsubscribetemp.py
Connected with result code 0
tinkerforge/bricklet/temperature/nfu/temperature {"_timestamp":1449852781.600672,"temperature":1750}
T: 17.50C

Changed: 2018-06-07 09:25, Category: TinkerForge MQTT, File: T6C266M1973.htm, ID: 1973