### Brick MQTT Proxy Example

TinkerForge Brick MQTT Proxy Example
The objective is to read or set TinkerForge Bricklet Values.
For this sample application, the Bricklets Temperature, Barometer, Humidity and LCD20x4 are used.
Values are read from the Bricklets Temperature, Barometer, Humidity and displayed on the LCD20x4 with the latest timestamp.
Used is the TinkerForge Brick MQTT Proxy which acts as the broker and the B4J jMQTT Client to initiate and display the values.
Strongly recommend to read the documentation:
TinkerForge Brick MQTT Proxy:
http://www.tinkerforge.com/en/doc/Software/Brick_MQTT_Proxy.html#brick-mqtt-proxy-topics-And-payload-structure
jMQTT Official MQTT client:
http://www.b4x.com/android/forum/threads/iot-mqtt-protocol.59471/
Application Principle
The B4J client connects to the MQTT Broker using TCP/IP address, ClientID (random) and Username & Password (both optional).
After connected successfully, the B4J client subscribes for TinkerForge topics and listens to messages published.
When a message arrives, an action is required to publish.
As an example, below is described to publish "Hello World" on the TinkerForge LCD20x4 display.
Subscribe to Topics
Each topic is composed out of a prefix and a suffix:
Examples:
Temperature Bricklet with UID nfu, from which the temperature is obtained when a message arrives
mqtt.Subscribe("tinkerforge/bricklet/temperature/nfu/temperature", 0)
LCD20x4 Bricklet with UID rTS set the backlight on or off
mqtt.Subscribe("tinkerforge/bricklet/lcd_20x4/rTS/backlight_on/set", 0)
mqtt.Subscribe("tinkerforge/bricklet/lcd_20x4/rTS/backlight_off/set", 0)
LCD20x4 Bricklet with UID rTS write a line
mqtt.Subscribe("tinkerforge/bricklet/lcd_20x4/rTS/write_line/set", 0)
Publish Topics
On the TinkerForge LCD20x4 Bricket with UID rTS write the Text "Hello World" at line 0 and position 7.
The terminal command would be - this is always a good way to test prior coding
mosquitto_pub -t tinkerforge/bricklet/lcd_20x4/rTS/write_line/set -m '{"line":0, "position":7, "text":"hello world"}'
Define the JSON payload
dim payload as string = "{""line"":0, ""position"":7, ""text"":""Hello World""}"
Publish using the prefix write_line/set and convert the payload string in bytes array
mqtt.Publish("tinkerforge/bricklet/lcd_20x4/rTS/write_line/set", payload.GetBytes("UTF8"))
That is it - the display should now show "Hello World" - HOW SIMPLE THIS HAS BECOME.
Summary
The use of MQTT has significantly simplified the solutions for reading Bricklet data or sending data.

Changed: , Category: TinkerForge MQTT, File: T6C266M1975.htm, ID: 1975