[SOLVED] OpenHab2 - External RPi - Relay - Python code

Hey guys!

Im running to an issue here, i am running openhab2 on a laptop.
I also have an RPi controlling a relay, the relay code is in python.
So two different machines here.

My goal is to be able to make openhab run the python script on the RPi.

They are both on the same lan and i have ssh access to the pi.

How can i possibly get the openhab server to execute the python script?

Any help would be appreciated. :slight_smile:
Thanks!

Take a look at the EXEC binding.

Use MQTT

/\ This.

I do this myself, with this script https://github.com/psyciknz/OpenHAB-Scripts/blob/master/mqtt.fancontrol.py

And here’s one where I’m migrating to homie, but it’s nit working at the moment https://github.com/psyciknz/OpenHAB-Scripts/blob/master/mqtt.homie.fancontrol.py

I’ll have items that match these MQTT topics that I can post if needed.

Would love it if you could post the items :smiley:

Managed to this with gpio zero and remote gpio. Works good enough for me.

But how can i incorporate this into a button on openhab?
Using the below script for testing purposes. But would like one script for on and one for off.
https://johnwargo.com/internet-of-things-iot/driving-a-relay-board-from-python-using-gpio-zero.html

Managed to sort it out myself. MQTT wasnt all that complicated as long as you run the script on the correct machine. :rofl:

Could you post your code and setup, please?
It may help someone someday.

My big mistake was actually running the script on the wrong pc.
But running the below script on the pi controlling the relay worked out.

#!/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 = [16]

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/relay_channel/#")
def on_message(client, userdata, msg):
if msg.topic == “/home/relay_channel/1” :
if msg.payload == “ON” :
GPIO.output(16, GPIO.LOW)
time.sleep(SleepTimeL);
#print “Outlet 1 ON”
if msg.payload == “OFF” :
GPIO.output(16, GPIO.HIGH)
time.sleep(SleepTimeL);
#print “Outlet 1 OFF”

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect(“192.168.10.169”, 1883, 60)

client.loop_forever()

if name == ‘main’:
try:
main()
except KeyboardInterrupt:
GPIO.cleanup()