Skip to content

Instantly share code, notes, and snippets.

@joleuger
Created June 6, 2017 21:03
Show Gist options
  • Save joleuger/1b7bcae7990dfc19330b5a141d945e16 to your computer and use it in GitHub Desktop.
Save joleuger/1b7bcae7990dfc19330b5a141d945e16 to your computer and use it in GitHub Desktop.
Control SeduBoard with OpenHAB via MQTT (Script needs to be adapted)
#!/usr/bin/python3
# pip3 install paho-mqtt
# pip3 install pyserial
# Assume that OpenHAB writes HSV values to MQTT server at path /things/Licht_Wohnzimmer_Ambilight_Farbe
# This is possible with an item in conf/items/example.items which is connected to MQTT, e.g.,
# Color Licht_Wohnzimmer_Ambilight_Farbe "Wohnzimmer Ambilight" {mqtt=">[openhab-things:/things/Licht_Wohnzimmer_Ambilight_Farbe:state:*:default]"}
# For more details, see http://docs.openhab.org/addons/bindings/mqtt1/readme.html
# and http://docs.openhab.org/configuration/items.html
# Handbook with SEDU commands
# http://www.sedu-board.de/docs/SEDU-AmbilightV31.pdf
import paho.mqtt.client as mqtt
import colorsys
import serial
import binascii
ser = serial.Serial('COM4',250000, timeout=0)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/things/Licht_Wohnzimmer_Ambilight_Farbe")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
hsvValues=msg.payload.decode("utf-8").split(',')
assert(len(hsvValues)==3)
assert(hsvValues[0].isnumeric)
assert(hsvValues[1].isnumeric)
assert(hsvValues[2].isnumeric)
print(hsvValues)
rgbColors=colorsys.hsv_to_rgb(float(hsvValues[0])/360.0,float(hsvValues[1])/100.0,float(hsvValues[2])/100.0)
print(rgbColors)
rgbColorsNormalized=(int(rgbColors[0]*255),int(rgbColors[1]*255),int(rgbColors[2]*255))
print(rgbColorsNormalized)
# set colors
serialCmdSetColors=bytearray() # mutable bytearray
serialCmdSetColors[0:2]=[0xa5,0x5a,0xff] # input sequence
serialCmdSetColors[3:3]=[0xc0] # set, but do not save
serialCmdSetColors[4:4]=[0x01] # fixed color
serialCmdSetColors[5:5]=[rgbColorsNormalized[0]] #r
serialCmdSetColors[6:6]=[rgbColorsNormalized[1]] #g
serialCmdSetColors[7:7]=[rgbColorsNormalized[2]] #b
serialCmdSetColors[8:8]=[0xff] # brightness
serialCmdSetColors[9:9]=[0x03] # speed=1
serialCmdSetColors[10:10]=[0x00] #colors=rgb
serialCmdSetColors[11:11]=[1] # number of colors=1
ser.write(serialCmdSetColors)
serialCmdSetColorsResult=ser.read(50)
print(binascii.hexlify(serialCmdSetColorsResult))
# write colors
serialCmdSave=bytearray()
serialCmdSave[0:2]=[0xa5,0x5a,0xff] # input sequence
serialCmdSave[3:3]=[0xc2] # save
ser.write(serialCmdSave)
serialCmdSaveResult=ser.read(50)
print(binascii.hexlify(serialCmdSaveResult))
client = mqtt.Client(client_id="thing-ambilight",)
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("username", password="password")
client.connect("192.168.1.1", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment