### Sample RGB LED

An example of switching a RGB LED On and Off by publishing a MQTT message for the RGB LED.
Set the state of the RGB LED
' Sets the light of an RGB LED to on or off
`Sub SetSetTopLight(stateon As Boolean)
' Get the UID from a settings map. The uid is like zJW
Dim uid As String = Main.settings.Get("lighthousetoplightuid")
' Define the topic to publish
Dim topic As String = $"tinkerforge/bricklet/rgb_led/${uid}/rgb_value/set"$
' Define the payload
If stateon Then
Dim payload As String = $"{ "r":255,"g":255,"b":255 }"$
Else
Dim payload As String = $"{ "r":0,"g":0,"b":0 }"$
End If
' Publish the topic with payload as bytes
Main.mqtt.Publish(topic, payload.GetBytes("UTF8"))
' Log for testing
Log("Top Light switched to " & stateon & ":" & payload)
' Examples log entry:
' Top Light switched to true:{ "r":255,"g":255,"b":255 }
' Top Light switched to false:{ "r":0,"g":0,"b":0 }
End Sub`
Assiging the sub to a ToggleButton which background color changes depending state:
`Sub Class_Globals
Private tbToplight As ToggleButton
...
`Sub tbToplight_SelectedChange(Selected As Boolean)
SetSetTopLight(Selected)
If Selected Then
CSSUtils.SetBackgroundColor(tbToplight, fx.Colors.Red)
Else
CSSUtils.SetBackgroundColor(tbToplight, fx.Colors.Green)
End If
End Sub`
Get the state of the RGB LED
To get the state of the RGB LED, subscribe to
tinkerforge/bricklet/rgb_led/UID/rgb_value
where the UID like zJW
tinkerforge/bricklet/rgb_led/zJW/rgb_value
This results in a message topic=payload, like
tinkerforge/bricklet/rgb_led/zJW/rgb_value={"r":0,"_timestamp":1474553572.646973,"b":0,"g":0}
The colors of the RGB LED are r,,g,b. In case the RGB LED is off.
The _timestamp is unix format. This can be converted using:
'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`
Subs for the RGB LED Bricklet
' Subscribe to the value of a TF RGBLED
' mqtt: mqtt client which must initialized prior calling
' uid: unique uid of the rgb led bricklet
' Example: SubcribeRGBLED_RGBValue(mqtt, "zNx")
' The value is returned as JSON string:
' {"r":112,"_timestamp":1474963659.833381,"b":34,"g":112}
`Public Sub SubcribeRGBLED_Value(mqtt As MqttClient, uid As String)
Dim t as String = $"tinkerforge/bricklet/rgb_led/${uid}/rgb_value"$)
Try
mqtt.Subscribe(t, 0)
Catch
Log($"Error subscribung TinkerForge RGB LED Color: ${LastException.message}"$)
End Try
End Sub`
' Set the color of a RGB LED Blicklet
' mqtt: mqtt client which must initialized prior calling
' uid: unique uid of the rgb led bricklet
' r color: 0 -255
' g color: 0 - 255
' dimmer: pct 0 - 100
' Example: setTFRGBLEDColor(mqtt, "uid", 200, 201, 202, 50)
' TODO: additional checks for rgb values in range
`Public Sub SetRGBLED_Color(mqtt As MqttClient, uid As String, r As Int, g As Int, b As Int, dimmerpct As Int)
Dim pct As Double = dimmerpct / 100
Dim topic As String = $"tinkerforge/bricklet/rgb_led/${uid}/rgb_value/set"$
Dim payload As String = $"{ "r":${Round(r * pct)}, "g":${Round(g * pct)}, "b":${Round(b * pct)} }"$
Try
mqtt.Publish(topic, payload.GetBytes("UTF8"))
Catch
Log($"Error publishing TinkerForge RGB LED Color: ${LastException.message}"$)
End Try
End Sub`
' Parse the RGBLED JSON payload into a map with key timestamp,r,g,b. Requires the JSON library.
' Example payload:
' {"r":112,"_timestamp":1474963659.833381,"b":34,"g":112}
' Case $"tinkerforge/bricklet/rgb_led/zJW/rgb_value"$
' Dim m As Map = TFMQTT.getRGBLED_Color(PayLoadStr)
' Log(m.Get("timestamp") & "-" & m.Get("r") & "-" & m.Get("g") & "-" & m.Get("b"))
`Public Sub GetRGBLED_Color(payload As String) As Map
Log(payload)
Dim parser As JSONParser
parser.Initialize(payload)
Dim root As Map = parser.NextObject
Dim r As Int = root.Get("r")
Dim b As Int = root.Get("b")
Dim g As Int = root.Get("g")
Dim ts As String = convertUNIXDateTimeStamp(root.Get("_timestamp"))
Dim m As Map = CreateMap("timestamp":ts, "r":r, "g":g, "b":b)
Return m
End Sub`
' Convert a UNIX datetimestamp, like 1474553572.646973 into format dd-MMM-YYYY HH:mm
`Public Sub ConvertUNIXDateTimeStamp(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: T6C266M1982.htm, ID: 1982