ich bin absoluter Neuling im Bereich Python.
Ich habe einen Raspi, welcher per Python-Script einen LED-Streifen ansteuert.
Hier das Script für die LEDs
Funktioniert auch, wenn ich das script per Python an der Shell starte
python W2801.py 123 123 123 (dieZiffern sind die Farbe in RGB)
Die LEDs leuchten weiss
python W2801.py 0 0 0
Die LEDs sind wieder aus
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time, sys, inspect, ast
import RPi.GPIO as GPIO
# Import the WS2801 module.
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
# Configure the count of pixels:
PIXEL_COUNT = 32
# other settings
SPI_PORT = 0
SPI_DEVICE = 0
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO)
################################################################################
# DEFINE YOUR FUNCTIONS HERE
################################################################################
def setRGBColor(pixels, r=255, g=255, b=255):
# 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( r, g, b ) )
pixels.show()
if __name__ == "__main__":
if len(sys.argv) < 4:
sys.exit(1)
cmdargs = sys.argv[1:]
setRGBColor(pixels, cmdargs[0], cmdargs[1], cmdargs[2])
Nun möchte ich die Farbe per MQTT steuern, mit diesem Script wird auch ein String in folgender Form empfangen:
python mqtt_subscribe.py
Code: Alles auswählen
import paho.mqtt.client as mqtt
MQTT_SERVER = "localhost"
MQTT_PATH = "test_channel"
# 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):
print(msg.topic+" "+str(msg.payload))
# more callbacks, etc
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()
LED1/gesamt 48.00000000000001285714285714285800000000000 48.0000000000000128571428571428580 105.000000000000015
Wie kann ich jetzt den Empfang der Zahlen (des Strings) so in das LED-Script einbauen, das die Farbwerte per MQTT ausgewertet werden?
Ist es auch möglich, das Script dann auszuführen, wenn die Werte sich ändern?
Oder muss es dann als Dauerschleife laufen?
Sorry für die Fragen, aber ich bin jetzt echt ratlos.....
Gruß
Yankee