HOST = "localhost"
PORT = 4223
UID = "zHN" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_rgb_led import BrickletRGBLED

import colorsys
import time

SECONDS_PER_ROUND = 1.5
COLORS_PER_ROUND = 256

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    rl = BrickletRGBLED(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    print("Press ctrl+c to exit")

    while True:
        for i in range(COLORS_PER_ROUND):
            r, g, b = colorsys.hsv_to_rgb(1.0*i/COLORS_PER_ROUND, 1, 0.1) # Calculate color
            rl.set_rgb_value(int(r*255), int(g*255), int(b*255)) # Set color

            time.sleep(SECONDS_PER_ROUND/COLORS_PER_ROUND) # Wait for next color change

    ipcon.disconnect()
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ"  # Change XYZ to the UID of your RGB LED Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_rgb_led import BrickletRGBLED

if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    rl = BrickletRGBLED(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Set light blue color
    rl.set_rgb_value(0, 170, 234)

    raw_input("Press key to exit\n")  # Use input() in Python 3
    ipcon.disconnect()
    def onHeartbeat(self):
        self.HeartbeatCounter = self.HeartbeatCounter + 1
        Domoticz.Debug("onHeartbeat called. Counter=" + str(self.HeartbeatCounter * self.HeartbeatInterval) + " (Heartbeat=" + Parameters["Mode5"] + ")")
        # Flag to check if connected to the master brick
        self.ipConnected = 0

        # check the heartbeatcounter against the heartbeatinterval
        if (self.HeartbeatCounter * self.HeartbeatInterval) % int(Parameters["Mode5"]) == 0:
            # Get the moisture value
            try:
                # Create IP connection
                ipcon = IPConnection()
            
                # Create device objects using the UID as defined in the parameter Mode1
                # The string contains multiple UIDs separated by comma (,). This enables to define more bricklets.
                brickletUIDParam = Parameters["Mode1"]
                Domoticz.Debug("UIDs:" + brickletUIDParam )
                # Split the parameter string into a list of UIDs
                brickletUIDList = brickletUIDParam.split(',')
                # Check the list length (3 because 3 bricklet UIDs) and create the device objects
                if len(brickletUIDList) == 3:
                    mb = BrickletMoisture(brickletUIDList[0], ipcon)
                    sb = BrickletSegmentDisplay4x7(brickletUIDList[1], ipcon)
                    lb = BrickletRGBLED(brickletUIDList[2], ipcon)

                # Connect to brickd using Host and Port
                try:
                    ipcon.connect(Parameters["Address"], int(Parameters["Port"]))
                    self.ipConnected = 1
                    Domoticz.Debug("IP Connection - OK")
                except:
                    Domoticz.Debug("[ERROR] IP Connection failed")

                # Don't use device before ipcon is connected
                if self.ipConnected == 0:
                    Devices[2].Update( nValue=0, sValue="[ERROR] Can not connect to Master Brick. Check settings, correct and restart Domoticz." )
                    Domoticz.Log(Devices[2].sValue)
                    return

                # Get current moisture value
                moisturetf = mb.get_moisture_value()
                Domoticz.Debug("Tinkerforge value:" + str(moisturetf) )
                moisturedom = converttfvalue(moisturetf)
                Domoticz.Debug("Domoticz value:" + str(moisturedom) )

                # Moisture Device and Text Device
                # Update the value - only nValue is used, but mandatory to add an sValue
                Devices[1].Update( nValue=moisturedom, sValue="0")

                # Tinkerforge Bricklet Updates

                # Segment Display set the value between 0(dry) - 100(wet)
                # Inverse the Domoticz moisure value
                moistureled = DOMOTICZMOISTUREDRY - moisturedom
                l = list(str(moistureled))
                # dry
                if  len(l) == 1:
                    segments = (DIGITS[0], DIGITS[0], DIGITS[0], DIGITS[int(l[0])])

                # irrigation advice
                if  len(l) == 2:
                    segments = (DIGITS[0], DIGITS[0], DIGITS[int(l[0])], DIGITS[int(l[1])])

                # adequate
                if  len(l) == 3:
                    segments = (DIGITS[0], DIGITS[int(l[0])], DIGITS[int(l[1])], DIGITS[int(l[2])])

                # not used
                if  len(l) == 4:
                    segments = (DIGITS[int(l[0])], DIGITS[int(l[1])], DIGITS[int(l[2])], DIGITS[int(l[3])])

                # Write the moisture value to the display with full brightness without colon
                sb.set_segments(segments, 7, False)        
                Domoticz.Debug("Segment Display updated")

                # Set the color of the RGB LED indicator
                # The indicator uses own scheme to keep simple: dry < 20; irrigation advice 20-40; wet > 40
                Domoticz.Debug("RGB LED updated. Brightness=" + Parameters["Mode4"])
                lbbrightness = int(Parameters["Mode4"])
                if lbbrightness < RGBBRIGHTNESSMIN:
                    lbbrightness = RGBBRIGHTNESSMIN
                if lbbrightness > RGBBRIGHTNESSMAX:
                    lbbrightness = RGBBRIGHTNESSMIN

                # Turn the LED on with color RGB depending LED value - 0(dry) - 100(wet)
                # dry = RED
                if moistureled < 20:
                    lb.set_rgb_value(lbbrightness, 0, 0)

                # irrigation advice = YELLOW
                if moistureled >= 20 and moistureled <= 40:
                    lb.set_rgb_value(lbbrightness, lbbrightness, 0)
                
                # wet = GREEN
                if moistureled > 40:
                    lb.set_rgb_value(0, lbbrightness, 0)
               
                # Disconnect
                ipcon.disconnect()

                # Log Message
                Devices[2].Update( nValue=0, sValue="Polling OK: TF=" + str(moisturetf)+", Dom="+str(moisturedom)+", LED="+str(moistureled))
                Domoticz.Log(Devices[2].sValue)

            except:
                # Error
                # Important to close the connection - if not, the plugin can not be disabled
                if self.ipConnected == 1:
                    ipcon.disconnect()
            
                Devices[2].Update( nValue=0, sValue="[ERROR] Check settings, correct and restart Domoticz." )
                Domoticz.Log(Devices[2].sValue)