I am using openHAB for 2nd day and so far I am very enthusiastic about the platform.
Today I started to configure GPIO bindings (switches and contacts):
openHAB working on Raspberry pi 2 model B / Ubuntu 15.04 / Java 1.8.0_60
Breadboard connected with switch button and LED
GPIO binding installed
LED output worked without any issue
The input change (switch button) is not detected by openHAB
My items defined
Group All
Group OfficeJeroen (All)
/* Test setup GPIO */
Switch Light_Office "LED GPIO17" (OfficeJeroen, Lights) {gpio="pin:17"}
Contact Door_Office "Door GPIO18" (OfficeJeroen, Windows) {gpio="pin:18"}
My rules defined
/* Rules */
rule "Switch GPIO18 test"
when
Item Door_Office received update
then
logInfo("test", "GPIO18 (switch) changed to " + Door_Office.state)
end
rule "LED GPIO17 test"
when
Item Light_Office received update
then
logInfo("test", "GPIO17 (LED) changed to " + Light_Office.state)
end
My question:
The contact itself is working (checked /sys/class/gpio18/value -> the value changes after pressing the button). However this change is not received in openHAB and I am puzzled how to fix this. It seems that event listener is not set correctly during startup - see below
2015-09-01 10:44:04.015 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'demo.rules'
Exception in thread "openHAB GPIO event listener (pin 18)" java.lang.NoClassDefFoundError: Could not initialize class com.sun.jna.Native at org.openhab.io.gpio.linux.LibC.<clinit>(LibC.java:54) at org.openhab.io.gpio.linux.GPIOPinLinux$EventListener.run(GPIOPinLinux.java:652)
It would be good to know if somebody has the GPIO binding up & running on a Raspberry Pi 2?
When you have both the input and output GPIO pins working I would love to learn how you set it up.
I use GPIO extensively for both input and output. I’ve noticed a few things to beware of:
1)Editing default.items means GPIO pins move to unititialized state, so everything stops working until you either manually reset things or reboot
2)From reboot Contact items are uninitialized until they change state or are forced into a known condition using postUpdate
3)On reboot output items are all set to on by the system which I got round by using a relay to only apply power to all my other relays when it was set to off. I then use a startup script to set everything to its normal state before I set the power providing relay to off.
Thanks for your input Kevin.
I probably need to consider that as I also want to connect some relay cards (but first need to get a bit more familiar with openhab). Btw - is somebody working on the issues you mentioned?
I’m not aware of anyone working on the setting all GPIO output pins to on issues being worked upon. I just assumed it was something it did and worked around it. It would be useful if it didn’t do it however so I’ll put out an issue on GIT and see if anyone responds.
hi, I have similiar problems, my gpio changes to an an udefined state all the time…
2015-09-25 12:56:31.416 [INFO ] [runtime.busevents ] - Light_GF_Elefant state updated to Undefined
2015-09-25 12:56:31.422 [INFO ] [runtime.busevents ] - Heating_FF_Bed state updated to Undefined
2015-09-25 12:56:31.457 [INFO ] [runtime.busevents ] - Light_GF_Glass state updated to Undefined
2015-09-25 12:56:31.746 [INFO ] [runtime.busevents ] - Light_GF_Living_Table state updated to OFF
2015-09-25 12:56:32.251 [INFO ] [runtime.busevents ] - Light_GF_Center state updated to OFF
So I added this to the start.sh file, but it still gives me the same behaviour. What do I miss here?
cd /sys/class/gpio
echo Initializing
chgrp -R dialout /sys/class/gpio
chmod -R g+rw /sys/class/gpio
sh -c "echo 2 > unexport"
sh -c "echo 2 > export"
sh -c "echo out > gpio2/direction"
echo Setting all gpio high
sh -c "echo 1 > gpio2/value"
sleep 20
echo unexporting all
sh -c "echo 2 > unexport"
I did this for all GPIO 'on the pi, is that the problem?
forgot to mentioned that my configuration looks like this
################################### GPIO Binding ######################################
# Optional directory path where "sysfs" pseudo file system is mounted, when isn't
# specified it will be determined automatically if "procfs" is mounted
gpio:sysfs=/sys
# Optional time interval in miliseconds when pin interrupts are ignored to
# prevent bounce effect, mainly on buttons. Global option for all pins, can be
# overwritten per pin in item configuration. Default value if omitted: 0
gpio:debounce=0
I have not connected anyone to input yet, that will be the next step, as soon as i can figure out why the state goes to undefined all the time.
This is my autostart script, but I guess the problem is not in that one, since i get the same problem if start it manually.
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sudo /home/pi/openhab/start.sh &
exit 0
Well now it stopped working again, and i am back to the darkness:( The problem has to be in openhab, because otherwise it would not have been able to turn it on for 10s(if the gpio was not unexported correctly, wrong file permissions). So what did I miss in the .item file or in the .cfg file?
Have defined quite some in and outputs and in principle it is working.
When setting it up I crosschecked the actual values of each GPIO (in /sys/class/gpio/).
Will you my config example tomorro - am on the move now.
Stop openhab, do not (!) ‘clear’ the gpio and start openhab again. Following errors on all defined GPIOs:
2015-09-28 08:04:43.457 [ERROR] [.o.b.gpio.internal.GPIOBinding] - Error occured while creating backend object for item Home_Doorbell, exception: Device or resource busy
In this case the GPIOs cannot be controlled via openHAB (although on your UI it does indicate that it switches and works properly)
Stop openhab, clear the GPIOs (echo > /sys/class/gpio/unexport) and start openhab again
No errors found / all working as expected
Openhab is running correctly and GPIOs (input and output) work fine. I switched ON the buzzer, after that I added a comment in the default.items file and saved it. Following happened:
2015-09-28 08:40:16.652 [INFO ] [org.openhab.model.script.test ] - Buzzer state change. State = ON
2015-09-28 08:40:51.555 [INFO ][c.internal.ModelRepositoryImpl] - Refreshing model ‘hag.items’
2015-09-28 08:40:51.615 [INFO ][org.openhab.model.script.test ] - Buzzer state change. State = Undefined
2015-09-28 08:40:53.940 [INFO ][org.openhab.model.script.test ] - Doorbell state change. State = Undefined
2015-09-28 08:40:54.665 [INFO ][org.openhab.model.script.test ] - Buzzer state change. State = OFF
2015-09-28 08:40:54.705 [INFO ][org.openhab.model.script.test ] - Doorbell state change. State = OPEN
NOTE: Changing the .rules does not result in incorrect states.
Thanks, four questions
1 :How does your .cfg file look like?
2:How does your autostart file look like, i guess there is where you have put your :
echo unexporting all
sh -c “echo 2 > unexport”
sh -c “echo 3 > unexport”
…
I unexported all GPIO, even though they might not have been used, is that an issue? The unexport is not run as sudo right? I have openhab installed in /home/pi/Openhab and i am logged in the user pi. Do i need to alter some file permission for the/sys/class/gpio in the reset script?
Do you use an autoreset script in your /etc/rs.local ? Because my pi goes down ones per day in average, so I need the autostart…(think i need to put a capasitor on the power line, because my pc power supply fails ones in a while for a few seconds.) I have my pi powered through the 5v gpio pin, do you power it with usb?
3: My final question is how do you kill openhab when its not run in osgi mode?
4: How does your rule file look like? I gues you have your contact item changing the state of the switch item? Did you use internal pullup on the contact item or did you put a 10kohm resistor between vcc and ground?
openhab.cfg - for GPIO test I did not touch the config
Unexporting the GPIOs - created simple script (at the moment I do manually run it) ./cleangpio.sh. Even when there is no need to unexport you can run these commands. You need to run it in sudo (otherwise gpio dir not accessible)
For basic gpio tests I just start / close / status “openhab” with /etc/init.d/openhab <start|stop|status>
.rules - for above tests I used the basic lines below. Yes, I used a pullup resistor for the doorbell (only 1k) - however also used an opto isolator (4N25) to separate the 2 circuits. For the buzzer (output) of course no resistor used. When you are interested I can send you the schematics.
rule “Bell detection”
when
Item Home_Doorbell received update
then
var String Timestamp = String::format( “%1$tH:%1$tM:%1$tS %1$tY-%1$tm-%1$td”, new Date() )
logInfo(“test”, "Doorbell state change. State = " + Home_Doorbell.state)
sendMail(“me@blabla.com”, “Doorbell state received: (” + Home_Doorbell.state + “)”, "Doorbell rang at " + Timestamp)
end
rule “Buzzer state”
when
Item Home_Buzzer received update
then
logInfo(“test”, "Buzzer state change. State = " + Home_Buzzer.state)
end
rule “HAG system start”
when
System started
then
var String Timestamp = String::format( “%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS”, new Date() )
sendMail(“me@blabla.com”, “HAG system started”, "The HAG system started on: " + Timestamp)
end
I will see if it still works next time it reboots…
I have a few question regarding
2 .rules - for above tests I used the basic lines below. Yes, I used a pullup resistor for the doorbell (only 1k) - however also used an opto isolator (4N25) to separate the 2 circuits. For the buzzer (output) of course no resistor used. When you are interested I can send you the schematics.
Can you post the schematic, why did you use 4n25? Is it needed? Did you connect the switch to ground or 3v?
And how did you tell your doorbell contact to set pin 20 high(home buzzer item) ?
I guess it should be something like this or:
rule "Bell detection"
when
Item Home_Doorbell received update
Home_Buzzer.state = + Home_Buzzer.state
then
end
Below doorbell schematic. Used opto coupler to isolate the doorbell circuit (12vdc) from my Raspberry. Could use transistor logic as well but I decided to physically separate them (the circuit basically detects the current when doorbell rings / inside the 4n25 a LED “activates” the transistor).