mit mqtt topic einen gpio mit gpiozero schalten
Verfasst: Sonntag 5. Mai 2024, 18:28
Hallo alle zusammen
Ich bin seit längeren dabei meine gpio´s neu am Raspberry pi anzusteuern. bis Bullseye ging das alles ohne Probleme über GPIO.rpi
Ich habe einen mqtt Broker auf meinen Fhem Server der mir die gewünschten topics sendet die ich zum ansteuern einzelner GPIO´s sendet.
Ich habe zur Zeit eine wie ich finde sehr Aufwändige Lösung mit einen Python script am laufen.
ist es möglich die ganzen if Abfragen auf eine für alle gpio´s zu reduzieren. Ich bekomme ja im topic und im payload eigentlich alles was ich brauche.
wie zum Beispiel
ich hatte mir gedacht das ich das topic zerlegen müsste und dann die einzelnen Variablen zum schalten der gpio´s verwendet werden. z.B. wie hier gedacht.
Meine Suchanfragen haben mir leider keine Lösung gebracht.
Vieleicht hat hier ja einer eine Idee wie das funktionieren könnte.
Ich habe leider auch noch keine grosse Erfahrung mit python.
Taste mich erst ganz langsam an das Thema heran.
Viele Grüsse
Stephan
Ich bin seit längeren dabei meine gpio´s neu am Raspberry pi anzusteuern. bis Bullseye ging das alles ohne Probleme über GPIO.rpi
Ich habe einen mqtt Broker auf meinen Fhem Server der mir die gewünschten topics sendet die ich zum ansteuern einzelner GPIO´s sendet.
Ich habe zur Zeit eine wie ich finde sehr Aufwändige Lösung mit einen Python script am laufen.
Code: Alles auswählen
#!/usr/bin/env python3
import logging
import time
import paho.mqtt.client as mqtt
import sys
from gpiozero import LED
from gpiozero import Button
GPIO22 = LED(22)
GPIO10 = LED(10)
GPIO9 = LED(9)
GPIO11 = LED(11)
GPIO0 = LED(0)
GPIO6 = LED(6)
GPIO13 = LED(13)
GPIO16 = LED(16)
GPIO25 = LED(25)
GPIO24 = LED(24)
GPIO23 = LED(23)
GPIO15 = LED(15)
GPIO14 = LED(14)
TOPIC = "gpio/#"
broker_address="localhost"
port = 1883
client = mqtt.Client("GPIO")
username = 'username'
password = 'password'
def on_message(client, userdata, message):
if "gpio/22/on" in (message.topic):
GPIO22.on()
if "gpio/22/off" in (message.topic):
GPIO22.off()
if "gpio/10/on" in (message.topic):
GPIO10.on()
if "gpio/10/off" in (message.topic):
GPIO10.off()
if "gpio/9/on" in (message.topic):
GPIO9.on()
if "gpio/9/off" in (message.topic):
GPIO9.off()
if "gpio/11/on" in (message.topic):
GPIO11.on()
if "gpio/11/off" in (message.topic):
GPIO11.off()
if "gpio/0/on" in (message.topic):
GPIO0.on()
if "gpio/0/off" in (message.topic):
GPIO0.off()
if "gpio/6/on" in (message.topic):
GPIO6.on()
if "gpio/6/off" in (message.topic):
GPIO6.off()
if "gpio/13/on" in (message.topic):
GPIO13.on()
if "gpio/13/off" in (message.topic):
GPIO13.off()
if "gpio/16/on" in (message.topic):
GPIO16.on()
if "gpio/16/off" in (message.topic):
GPIO16.off()
if "gpio/25/on" in (message.topic):
GPIO25.on()
if "gpio/25/off" in (message.topic):
GPIO25.off()
if "gpio/24/on" in (message.topic):
GPIO24.on()
if "gpio/24/off" in (message.topic):
GPIO24.off()
if "gpio/23/on" in (message.topic):
GPIO23.on()
if "gpio/23/off" in (message.topic):
GPIO23.off()
if "gpio/15/on" in (message.topic):
GPIO15.on()
if "gpio/15/off" in (message.topic):
GPIO15.off()
if "gpio/14/on" in (message.topic):
GPIO14.on()
if "gpio/14/off" in (message.topic):
GPIO14.off()
def on_connect(client, userdata, flags, rc):
print("Connected to MQTT Broker: " + broker_address)
client.subscribe(TOPIC)
if __name__ == "__main__":
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username, password)
client.connect(broker_address, port)
client.loop_forever()
wie zum Beispiel
Code: Alles auswählen
gpio/22/on
gpio/22/off
Code: Alles auswählen
gpio/22/on wird zu
gpio $1 $2 womit ich dann statt hard codiert
GPIO22.on() schreibe vieleicht schreiben könnte
GPIO$1.$2()
Vieleicht hat hier ja einer eine Idee wie das funktionieren könnte.
Ich habe leider auch noch keine grosse Erfahrung mit python.
Taste mich erst ganz langsam an das Thema heran.
Viele Grüsse
Stephan