Hey all,
I’ve got some troubles getting some external raspberry pi gpio’s to work.
I’m an noob related to programming, so here’s what I’ve copy & pasted so far:
Client-Script:
#!/usr/bin/python
# -- coding: utf-8 --
# Import package
import paho.mqtt.client as mqtt
#add for output
import RPi.GPIO as GPIO
# Define Variables
MQTT_HOST = "192.168.X.X"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC1 = "pibox/oben/switch/1"
MQTT_TOPIC2 = "pibox/oben/switch/2"
MQTT_TOPIC3 = "pibox/oben/switch/3"
MQTT_TOPIC4 = "pibox/oben/switch/4"
MQTT_TOPIC5 = "pibox/oben/switch/5"
MQTT_TOPIC6 = "pibox/oben/switch/6"
MQTT_TOPIC7 = "pibox/oben/switch/7"
MQTT_TOPIC8 = "pibox/oben/switch/8"
#MQTT_TOPIC = [("pibox/oben/switch1",0),("pibox/oben/switch2",0),("pibox/oben/switch3",0),("pibox/oben/switch4",0),("pibox/oben/switch5",0),("pibox/oben/switch6",0),("pibox/oben/switch7",0),("pibox/oben/switch8",0)]
MQTT_TOPIC = client.subscribe("pibox/#",0)
# Define Ports
SWITCH1 = 17 # wPi: 0
SWITCH2 = 4 # wPi: 7
SWITCH3 = 27 # wPi: 2
SWITCH4 = 24 # wPi: 5
SWITCH5 = 5 # wPi: 21
SWITCH6 = 6 # wPi: 22
SWITCH7 = 13 # wPi: 23
SWITCH8 = 16 # wPi: 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(SWITCH1, GPIO.OUT)
GPIO.setup(SWITCH2, GPIO.OUT)
GPIO.setup(SWITCH3, GPIO.OUT)
GPIO.setup(SWITCH4, GPIO.OUT)
GPIO.setup(SWITCH5, GPIO.OUT)
GPIO.setup(SWITCH6, GPIO.OUT)
GPIO.setup(SWITCH7, GPIO.OUT)
GPIO.setup(SWITCH8, GPIO.OUT)
try:
# Define on connect event function
# We shall subscribe to our Topic in this function
def on_connect(self,mosq, obj, rc):
mqttc.subscribe(MQTT_TOPIC)
print("Connected on "+MQTT_HOST)
# Define on_message event function.
# This function will be invoked every time,
# a new message arrives for the subscribed topic
def on_message(mosq, obj, msg):
if(msg.topic==MQTT_TOPIC1):
print 'Switch 1 wird geschaltet!'
SWITCH = SWITCH1
if(msg.topic==MQTT_TOPIC2):
print 'Switch 2 wird geschaltet!'
SWITCH = SWITCH2
if(msg.topic==MQTT_TOPIC3):
print 'Switch 3 wird geschaltet!'
SWITCH = SWITCH1
if(msg.topic==MQTT_TOPIC4):
print 'Switch 4 wird geschaltet!'
SWITCH = SWITCH4
if(msg.topic==MQTT_TOPIC5):
print 'Switch 5 wird geschaltet!'
SWITCH = SWITCH1
if(msg.topic==MQTT_TOPIC6):
print 'Switch 6 wird geschaltet!'
SWITCH = SWITCH6
if(msg.topic==MQTT_TOPIC1):
print 'Switch 7 wird geschaltet!'
SWITCH = SWITCH7
if(msg.topic==MQTT_TOPIC8):
print 'Switch 8 wird geschaltet!'
SWITCH = SWITCH2
if (msg.payload=='ON'):
GPIO.output(SWITCH,True)
print 'Switch '+SWITCH+' switches to ON'
print "Topic: " + str(msg.topic)
print "QoS: " + str(msg.qos)
if (msg.payload=='OFF'):
GPIO.output(SWITCH,False)
print 'Switch '+SWITCH+' switches to OFF'
print "Topic: " + str(msg.topic)
print "QoS: " + str(msg.qos)
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed to Topic: " +
MQTT_TOPIC + " with QoS: " + str(granted_qos))
# Initiate MQTT Client
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
# Continue monitoring the incoming messages for subscribed topic
mqttc.loop_forever()
except KeyboardInterrupt:
# here you put any code you want to run before the program
# exits when you press CTRL+C
GPIO.cleanup()
#finally:
#GPIO.cleanup() # this ensures a clean exit
configuration.yaml
# External MQTT Switches
- platform: mqtt
name: “Switch 1”
state_topic: “pibox/oben/switch/1/state”
command_topic: “pibox/oben/switch/1/” #set"
availability_topic: “pibox/oben/switch/1/available”
payload_on: “ON”
payload_off: “OFF”
state_on: “ON”
state_off: “OFF”
optimistic: false
qos: 0
retain: true
- platform: mqtt
name: "Switch 2"
state_topic: "pibox/oben/switch/2/state"
command_topic: "pibox/oben/switch/2/set"
availability_topic: "pibox/oben/switch/2/available"
payload_on: "ON"
payload_off: "OFF"
state_on: "ON"
state_off: "OFF"
optimistic: false
qos: 0
retain: true
etc..
And this is what it looks like:
Home Assistant Dashboard:
- new user, so just one picture
Entities tab:
I think this issue get’s caused by my lack of knowledge related to mqtt and his topics
Using this code just works like a charm:
-
new user, so just one picture.
/dev-mqtt
topic: pibox/oben/switch/8
payload: ON
Any clue how to solve this issue?
Many thanks in advance!