Hallo zusammen,
ich möchte einen LED-Strip per MQTT an einem Raspi per Openhab steuern.
ich habe folgende Items angelegt:
Außerdem habe ich folgende rule
und dieses Python Script
Der RGB_Wert wird korrekt an den Raspi übertragen , (123 123 123)
Wie muss ich jetzt das Script anpassen, damit die Strips auf den RGB_Wert reagieren und nicht mehr auf die Argumente in der Kommandozeile?
Kenne mich mit Python überhaupt nicht aus...
Gruß
Yankee
ich möchte einen LED-Strip per MQTT an einem Raspi per Openhab steuern.
ich habe folgende Items angelegt:
Code:
Color WS28xx_Strip "LED Steifen" Number RGB_rot "Rot [%s]" { mqtt=">[mosquitto:LED1/rot:command:*:default]" } Number RGB_gruen "Gruen [%s]" { mqtt=">[mosquitto:LED1/gruen:command:*:default]" } Number RGB_blau { mqtt=">[mosquitto:LED1/blau:command:*:default]" } String RGB_Wert { mqtt=">[mosquitto:LED1/gesamt:command:*:default]" }
Code:
var String RGBvalues rule "WSxx Change" when Item WS28xx_Strip received command then if (receivedCommand instanceof HSBType) { val red = ((WS28xx_Strip.state as HSBType).red * 2.55) as Number val green = ((WS28xx_Strip.state as HSBType).green * 2.55) as Number val blue = ((WS28xx_Strip.state as HSBType).blue * 2.55) as Number val red2 = red.intValue val green2 = green.intValue val blue2 = blue.intValue // RGBvalues = red.toString + " " + green.toString + " " + blue.toString + " " RGBvalues = red2 + " " + green2 + " " + blue2 sendCommand(RGB_Wert, RGBvalues) logInfo("WS Status", red + "," + green + "," + blue) RGB_rot.sendCommand(red) RGB_gruen.sendCommand(green) RGB_blau.sendCommand(blue) // executeCommandLine("/usr/bin/python3 /etc/openhab2/scripts/ws28xx.py " + red.toString + " " + green.toString + " " + blue.toString ) } else if (receivedCommand == ON){ //logInfo("WS Status", "ON") } else if (receivedCommand == OFF){ } end
und dieses Python Script
Code:
#!/usr/bin/env python3 # MQTT import paho.mqtt.client as mqtt import time, sys, inspect, ast import RPi.GPIO as GPIO # Import the WS2801 module. import Adafruit_WS2801 import Adafruit_GPIO.SPI as SPI MQTT_SERVER = "192.168.178.78" MQTT_PATH = "LED1/gesamt" # Configure the count of pixels: PIXEL_COUNT = 41 # other settings SPI_PORT = 0 SPI_DEVICE = 0 pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO) # 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(MQTT_PATH) # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global RGB_Wert RGB_Wert = msg.payload #HERE! # print(msg.topic+" "+str(msg.payload)) # print("Ergebnis") print(RGB_Wert) print("Wert angepasst") # more callbacks, etc def setRGBColor(pixels, msg): # cast from string to int # r, g, b = int(float(r)), int(float(g)), int(float(b)) pixels.set_pixels( Adafruit_WS2801.RGB_to_color( msg.payload ) ) pixels.show() print("LED leuchten") print(RGB_Wert) #if __name__ == "__main__": # if len(sys.argv) < 4: # sys.exit(1) cmdargs = sys.argv[1:] setRGBColor(pixels, cmdargs[0], cmdargs[1], cmdargs[2]) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect(MQTT_SERVER, 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()
Wie muss ich jetzt das Script anpassen, damit die Strips auf den RGB_Wert reagieren und nicht mehr auf die Argumente in der Kommandozeile?
Kenne mich mit Python überhaupt nicht aus...
Gruß
Yankee
Kommentar