Kimzer
(Kimzer)
January 20, 2019, 3:06pm
1
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.
Thanks!
H102
(CM6.5 H102)
January 20, 2019, 3:33pm
2
Take a look at the EXEC binding.
psyciknz
(Psyciknz)
January 20, 2019, 8:24pm
4
vzorglub:
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.
Kimzer
(Kimzer)
January 23, 2019, 3:41pm
5
Would love it if you could post the items
Kimzer
(Kimzer)
January 23, 2019, 7:08pm
6
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
Kimzer
(Kimzer)
January 26, 2019, 6:52pm
7
Managed to sort it out myself. MQTT wasnt all that complicated as long as you run the script on the correct machine.
vzorglub
(Vincent Regaud)
January 26, 2019, 7:18pm
8
Could you post your code and setup, please?
It may help someone someday.
Kimzer
(Kimzer)
January 27, 2019, 9:50am
9
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()