Control Raspberry I/O pin with EXEC

(Kaloschke) #1

Hi,
I want to control an I/O pin on my raspberry with the exec 2.x binding. I have a python script on-off.py , which works:

import RPi.GPIO as GPIO
import sys
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
if len(sys.argv) > 1:
    MOTOR_GPIO = 4
    GPIO.setup(MOTOR_GPIO, GPIO.OUT)
    if sys.argv[1] == "ON":
        GPIO.output(MOTOR_GPIO, GPIO.HIGH)
    elif sys.argv[1] == "OFF":
        GPIO.output(MOTOR_GPIO, GPIO.LOW)

My home.sidemap is:

sitemap home label="Motoren"
{
	Frame label="Motor" {
		Switch  item=MOTOR_1_SWITCH
	}
}

My home.item is :

String MOTOR_1_SWITCH "Motor 1"  ["Switchable"] { channel="exec:command:motor_1_control:input", channel="exec:command:motor_1_status:output", autoupdate="false" }

My home.things is:

Thing exec:command:motor_1_control [ command="/etc/openhab2/scripts/on-off.py %2$s", interval=0, autorun=true ]
Thing exec:command:motor_1_status [ command="/etc/openhab2/scripts/on-off.py status", interval=60, timeout=5 ]

And my home.rules are:

rule "Motor Power"
when
	Item Motor1 changed
then
	if(Motor1.state==ON){
		Motor1Input.sendCommand("OFF")
	}else{
		Motor1Input.sendCommand("ON")
	}

	while(Motor1.state != OFF){
		Thread::sleep(500)
	}
	
	Motor1.sendCommand(ON)
	Zustand.postUpdate(Motor1Output.state.toString)
end

When openhab starts, I get this event.log:

2018-03-12 16:04:25.193 [.ItemChannelLinkAddedEvent] - Link ‘MOTOR_1_SWITCH-exec:command:motor_1_status:output’ has been added.
2018-03-12 16:04:25.198 [.ItemChannelLinkAddedEvent] - Link ‘MOTOR_1_SWITCH-exec:command:motor_1_control:input’ has been added.
2018-03-12 16:04:25.351 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_control’ changed from UNINITIALIZED to INITIALIZING
2018-03-12 16:04:25.367 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_control’ changed from INITIALIZING to ONLINE
2018-03-12 16:04:25.370 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_status’ changed from UNINITIALIZED to INITIALIZING
2018-03-12 16:04:25.379 [hingStatusInfoChangedEvent] - ‘exec:command:motor_1_status’ changed from INITIALIZING to ONLINE
2018-03-12 16:04:25.509 [vent.ItemStateChangedEvent] - MOTOR_1_SWITCH changed from NULL to /etc/openhab2/scripts/on-off.py: 1: /etc/openhab2/scripts/on-off.py: import: not found
/etc/openhab2/scripts/on-off.py: 2: /etc/openhab2/scripts/on-off.py: import: not found
/etc/openhab2/scripts/on-off.py: 3: /etc/openhab2/scripts/on-off.py: Syntax error: word unexpected (expecting “)”)
/etc/openhab2/scripts/on-off.py: 1: /etc/openhab2/scripts/on-off.py: import: not found
/etc/openhab2/scripts/on-off.py: 2: /etc/openhab2/scripts/on-off.py: import: not found
/etc/openhab2/scripts/on-off.py: 3: /etc/openhab2/scripts/on-off.py: Syntax error: word unexpected (expecting “)”)

Does this mean, the python script isn’t ok? As I wrote already, it works from the commandline.

Greetings
Kaloschke

0 Likes

(Rich Koshak) #2

See How to solve Exec binding problems

The problem likely has to do with the fact that openHAB runs as the openHAB user and you probably installed the python libraries you are using only as your user rather than the system and they are therefore not available to the openhab user. Another explanation could be that the script is expecting to be executed from a specific folder but openHAB will be running the script from its own folder.

0 Likes

(Kaloschke) #3

Thank you very much for your reply.
The problem is, I’m a linux noob. I copy what I read. So what can I do to check the first part, the python libraries.
I use the openhabian image and install python as user openhabian.
The second I don’t understand. I thought, when I use the full path to the script, nothing can be wrong.
Greetings
Kaloschke

0 Likes

(Josar) #4

You need to ad the shebang to your Skript.
Place the library files to the right folder of your python installation. Some claims also the Skripts had to be at the same place, but I never had this issue.

But to only control rpi pins you could use the gpio binding.

https://docs.openhab.org/addons/bindings/gpio1/readme.html

0 Likes

(Kaloschke) #5

Fantastic. Shebang it is!. Thank you very very much.
I tried the gpio binding before for testing, but I need a script for a more complex situation.
Again, thank you very much.

0 Likes