-
Platform information:
- Hardware: RPI Zero
- OS:
Release = Raspbian GNU/Linux 10 (buster)
Kernel = Linux 4.19.75+
Platform = Raspberry Pi Zero W Rev 1.1
-
openHAB 2.5.1-2 (Release Build)
-
Issue of the topic:
I have spent the last day trying to get my GPIO relay to work via MQTT. I have MQTT running and can see it via MQTT Explorer but I am having one hell of a time getting it configured in OpenHab. I tried following this YouTube tutorial https://www.youtube.com/watch?v=Xi5kDoM7hF8&t=7s and failed.
I can get my 4-channel relay to fire correctly with one of the scripts he uses but I cannot seem to make his python script work with my MQTT setup. I cannot locate my MQTT config files either.
So what have I done up to this point?
Installed Openhabian.
I am using the following script to pass things between python and the GPIO and MQTT. I am completely new with working with MQTT here so please correct me if I’m making this more complicated than it is. The following script is NOT mine.
mqttrelay.py
/usr/bin/env/python
import os
import paho.mqtt.client as mqtt
from time import sleep
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
SleepTimeL = 0.2
pinList = [18,23,24,25]
for i in pinList:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
# The callback for when the client receives a CONNACK response from the server.
def main():
def on_connect(client, userdata, flags, rc):
client.subscribe("/home//#")
def on_message(client, userdata, msg):
if msg.topic == "/home/test/1" :
if msg.payload == "ON" :
GPIO.output(18, GPIO.LOW)
time.sleep(SleepTimeL);
#print "Outlet 1 ON"
if msg.payload == "OFF" :
GPIO.output(18, GPIO.HIGH)
time.sleep(SleepTimeL);
#print "Outlet 1 OFF"
if msg.topic == "/home/test/2" :
if msg.payload == "ON" :
GPIO.output(23, GPIO.LOW)
time.sleep(SleepTimeL);
#print "Outlet 2 On"
if msg.payload == "OFF" :
GPIO.output(23, GPIO.HIGH)
time.sleep(SleepTimeL);
#print "Outlet 2 Off"
if msg.topic == "/home/test/3" :
if msg.payload == "ON" :
GPIO.output(24, GPIO.LOW)
time.sleep(SleepTimeL);
#print "Outlet 3 ON"
if msg.payload == "OFF" :
GPIO.output(24, GPIO.HIGH)
time.sleep(SleepTimeL);
#print "Outlet 3 OFF"
if msg.topic == "/home/test/4" :
if msg.payload == "ON" :
GPIO.output(25, GPIO.LOW)
time.sleep(SleepTimeL);
#print "Outlet 4 ON"
if msg.payload == "OFF" :
GPIO.output(25, GPIO.HIGH)
time.sleep(SleepTimeL);
#print "Outlet 4 OFF"
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.252", 1883, 60)
client.loop_forever()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
GPIO.cleanup()`
Configured my home.items file to look like this:
`group gAll
Group gRelay (gAll)
Switch Outlet1 "Outlet 1" (gRelay) {mqtt=">[mymosquitto:/home/test/1:command:on:ON],>[mymosquitto:/home/test/1:command:off:OFF],<[mymosquitto:/home/test/1:state:default"}
Switch Outlet2 "Outlet 2" (gRelay) {mqtt=">[mymosquitto:/home/test/2:command:on:ON],>[mymosquitto:/home/test/2:command:off:OFF],<[mymosquitto:/home/test/2:state:default"}
Switch Outlet3 "Outlet 3" (gRelay) {mqtt=">[mymosquitto:/home/test/3:command:on:ON],>[mymosquitto:/home/test/3:command:off:OFF],<[mymosquitto:/home/test/3:state:default"}
Switch Outlet4 "Outlet 4" (gRelay) {mqtt=">[mymosquitto:/home/test/4:command:on:ON],>[mymosquitto:/home/test/4:command:off:OFF],<[mymosquitto:/home/test/4:state:default"}`
Configured my home.sitemap to look like this:
sitemap defualt label="Outlets" { Frame label="Outlet Control1" { Switch item=Outlet1 Switch item=Outlet2 Switch item=Outlet3 Switch item=Outlet4 } }
I tried configuring just the MQTT interfaces there… I could not connect to them via MQTT explorer when I did this. SO I completely installed the binding AND the MISC MQTT binding listed there as well and reinstalled Mosquitto from the apt repository. Next I re-installed the MQTT binding from the bindings list. Than I added the following “Thing”.
- If logs where generated please post these here using code fences.
Openhab.log file:
`2020-01-21 22:54:05.128 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item
'Outlet4' for widget org.eclipse.smarthome.model.sitemap.sitemap.Switch
2020-01-21 22:54:05.141 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for
widget org.eclipse.smarthome.model.sitemap.sitemap.Switch
2020-01-21 22:54:05.159 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item
'Outlet4' for widget org.eclipse.smarthome.model.sitemap.sitemap.Switch
2020-01-21 22:54:05.172 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item
'Outlet4' for widget org.eclipse.smarthome.model.sitemap.sitemap.Switch
2020-01-21 22:54:05.186 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item
'Outlet4' for widget org.eclipse.smarthome.model.sitemap.sitemap.Switch
2020-01-21 22:54:05.288 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename
`home.sitemap` does not match the name `defualt` of the sitemap - please fix this as you might see
unexpected behavior otherwise.`
Any advice is greatly appreciated! I tried to scour as much about OpenHab 2.5 and the new MQTT binding as I could but I struggled to get things going.
Cheers!
Joe