### Sample AmbientLight MQTT

To publish a MQTT message containing the illuminance value of the TinkerForge (TF) AmbientLight Bricklet (connected to a TF Master Brick which is stacked on a TF RED Brick).
The message content received in JSON format is converted to a type
Example Messages received of an Ambient Light (v1):
Topic: tinkerforge/bricklet/ambient_light/mdh/illuminance, PayLoad: {"illuminance":1354,"_timestamp":1456750088.109141}
Topic: tinkerforge/bricklet/ambient_light/mdh/illuminance, PayLoad: {"illuminance":1353,"_timestamp":1456750091.116549}
converted into
29-Feb-2016 14:14: 719
29-Feb-2016 14:14: 718
B4J MQTT Reference: https://www.b4x.com/android/forum/threads/iot-mqtt-protocol.59471/
Installing mosquitto on the RED Brick: sudo apt-get install mosquitto mosquitto-clients python-mosquitto
Check Mosquitto running:
ps -A | grep mosq
Libraries used:
jMQTT, ByteConverter (for the payLoad string), JSON to convert the payload string
'Non-UI application (console application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
'TinkerForge Library
#AdditionalJar: Tinkerforge
#End Region
`Sub Process_Globals
'Define MQTT Client
Public mqttclt As MqttClient
'TF RED Brick IP address
Private mqttserveruri As String = "tcp://192.168.0.30"
'Define the MQTT published message type which is build from the Payload JSON string received
Type PayLoadType (illuminance As Int, timestamp As String)
End Sub`
`Sub AppStart (Args() As String)
Dim clientId As String = Rnd(0, 999999999) & DateTime.Now
Try
mqttclt.Initialize("mqtt", mqttserveruri, clientId)
Dim mo As MqttConnectOptions
mo.Initialize("", "")
mqttclt.Connect2(mo)
Catch
Log($"Error initializing the MQTT client ${LastException}"$)
End Try
'Do not forget to start the message loop
StartMessageLoop
End Sub`
`Sub mqtt_Connected (Success As Boolean)
If Success = True Then
'Subscribe to topics = TF topic as published by the tinkerforge mqtt proxy
mqttclt.Subscribe("tinkerforge/bricklet/ambient_light/mdh/illuminance", 2)
Else
Log($"Error MQTT connection ${LastException}"$)
End If
End Sub`
`Private Sub mqtt_Disconnected
Log("mqtt.disconnected")
End Sub`
'Handle the arrival of messages. The content of the messages is in the payload byte array.
`Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
Dim PayLoadStr As String
PayLoadStr = BytesToString(Payload, 0, Payload.Length, "utf8")
Dim pt As PayLoadType = ParseMQTTMessage(PayLoadStr)
Log($"${pt.timestamp}: ${pt.illuminance}"$)
End Sub`
'Parse the MQTT Message (=the payload string) received in JSON Format.
'Example Payload: {"illuminance":1354,"_timestamp":1456750088.109141}
`Sub ParseMQTTMessage(msg As String) As PayLoadType
Dim parser As JSONParser
Dim pt As PayLoadType
pt.Initialize
parser.Initialize(msg)
Dim root As Map = parser.NextObject
pt.illuminance = root.Get("illuminance")
pt.timestamp = SetTimeStamp(root.Get("_timestamp"))
Return pt
End Sub`
'Convert the UNIX timestamp into format dd-MMM-YYYY HH:mm
`Sub SetTimeStamp(Value As String) As String
Dim timestamp As Long = Value * 1000
'Set the dateformat
DateTime.DateFormat = "dd-MMM-YYYY"
DateTime.TimeFormat = "HH:mm"
'Convert Date & Time to string
Return DateTime.Date(timestamp) & " " & DateTime.Time(timestamp)
End Sub`

Changed: , Category: TinkerForge MQTT, File: T6C266M1978.htm, ID: 1978