MQTT2 setup / Help

  • 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

Your setup of openHAB for mqtt is totally mixed up!
I didn’t watch the linked video however I suspect the description in there is relation to the version1 mqtt binding since you use the item syntax only valid for this version.
Using openHAB 2.5.1 you have most probably installed the actual version of mqtt. You mentioned to have installed the mosquitto mqtt broker (stick with this one) however it looks like you have also installed the openHAB embedded broker (remove that one).
Please read in the actual openHAB documentation on how to setup mqtt.

I personally can’t say much about python script and gpio.

I also couldn’t get it to work so uninstalled all openHab v2 MQTT bindings and broker.

Then enabled v1.x bindings and installed the v1 MQTT binding and Mosquitto broker (update mqtt config file with mosquito settings) and everything worked.

Items are configured manually but it works for me.

As stated many times on this forum, if you are using mqtt for the first time it doesn’t make sense to start off with the deprecated version 1 mqtt binding. This one is not maintained anymore and will not work on the next major update of openHAB (OH3).

If you find any videos for documentation they are not official. The official documentation is written by our developers and experienced end users who are openHAB experts in that area.

1 Like

Thanks Opus. I did read through the official openHAB documentation and was a bit confused on how things were setup in the openHAB and MQTT. I had previously used older versions of MQTT and openHAB. I will review the changes of the two and go from there.

I would like to use the freshest build possible and I would like to do things the right way.

Thanks!

1 Like

I plan to upgrade to v2 but for now it works with v1 and my previous config from OH2.2.

I understand that you can have v1 and v2 binding running at the same time for different items so I will slowly move these over to v2 one at a time.

Yes this is correct as I’m on the latest OH version and have one device that still uses v1. I keep it around so when I upgrade and it breaks then I know where the cut off is. :laughing: All other devices use v2 and after the first one or two devices you convert its a breeze. :wink:

1 Like

@mountbaldy here’s a github link by @rlkoshak that you may find helpful:

1 Like

It looks like everyone has you well supported here but I do see something worth commenting. RPi Zero is not a supported OH configuration. The RPi 0 has only a single CPU and too little RAM to run openHAB well. You will not be happy with the performance of OH on this machine. I’m not sure whether openHABian can support an RPi 0. I strongly recommend using a more powerful machine (RPi 2 or better).

5 Likes

Thanks for pointing that out Rich. I’ll be checking out the tagged you in above as well. I appreciate the support!

My end goal is to produce a low cost PID controller that is web enabled. That said, it will only be controlling 4 relays and reading inputs for a couple of sensors. I appreciate the advice on using a better suited machine.

Cheers!

A Pi 3 B+ with a few Esp8622 devices flashed with either tasmota or EspEasy would be a good low cost option. I have them (esp’s) scattered all over my house for door, light, and motion detection. If your handy with a soldering iron you can get the Esp chips and dev boards for super cheap (less than $2-3 US a board) but you will need to solder them yourself. If not into soldering Amazon has them for around $10 if buying a dev board ready to go. The dev boards have a usb connection for flashing and power so you only need a small wall wart (roku power supply works good) for power.

I can share some examples if you like, just let me know and I will be glad to help.

2 Likes

That sounds interesting. I like the low cost option with soldering. I haven’t played with many of those yet (Esp8622) but they look great! I may go that route for another project. :slight_smile: Sounds like a very versatile setup. Thanks for sharing!