GPIO - Event listener not working

Hi,

As promised an quite sketch how I experienced it. Below my defined items for this test.

Switch   Home_Buzzer   "Buzzer GPIO020"            (Home, Lights)  {gpio="pin:20"}
Contact Home_Doorbell  "Bell GPIO021"  (Home, Windows)           {gpio="pin:21 debounce:1500 activelow:no"}

Test results:

  1. 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)
  1. Stop openhab, clear the GPIOs (echo > /sys/class/gpio/unexport) and start openhab again
  • No errors found / all working as expected
  1. 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.

These results are in line as Kevin described above (I have not seen Kevin’s reboot problems). An issue is open for this (see https://github.com/openhab/openhab/issues/3124)

Jeroen

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?

Your help has been great.

Hi,

  1. openhab.cfg - for GPIO test I did not touch the config
  2. 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)
#!/bin/bash
echo "Unexporting the GPIO ports ...."
echo 20 > /sys/class/gpio/unexport
echo 21 > /sys/class/gpio/unexport
  1. For basic gpio tests I just start / close / status “openhab” with /etc/init.d/openhab <start|stop|status>

  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.

    import org.openhab.core.library.type.*
    import org.openhab.core.persistence.*
    import org.openhab.model.script.actions.*
    import org.openab.core.types.command
    import org.openhab.core.items.genericitem
    import java.util.Date
    import java.text.SimpleDateFormat

    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

Hope it helps
Jeroen

Thanks, its been working for a three days now… I basically did not change anything except of changing this

cd /sys/class/gpio

echo Initializing

chgrp -R dialout /sys/class/gpio
chmod -R g+rw /sys/class/gpio

sh -c "echo 2 > unexport"

to

#!/bin/bash
echo "Unexporting the GPIO ports ...."
echo 20 > /sys/class/gpio/unexport
echo 21 > /sys/class/gpio/unexport

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).

Have not connected the buzzer to the doorbell logic. You can turn the buzzer on via:

sendCommand(Home_Buzzer, ON)

Regards,
Jeroen

i think i finally found the bug in my system:

	echo unexporting all

sudo sh -c 'echo "2" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "3" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "4" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "17" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "27" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "22" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "10" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "9" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "11" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "5" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "6" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "13" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "19" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "26" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "14" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "15" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "18" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "23" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "24" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "25" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "8" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "7" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "12" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "16" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "20" > /sys/class/gpio/unexport'	
sudo sh -c 'echo "21" > /sys/class/gpio/unexport'	

echo Done unexporting

this how the unexporting script needs to look like, note the differnces between " and ’

I don’t understand why but it works now, at least for time being…

And thanks joroen, i think I also will add the buzzer to my setup… My only problem now is that I have run out of GPIO’s, so I might have to add arduino or something else:)

Thanks for all the help

Now it stopped working again…

I took a closer look at the log file:

  2015-09-18 18:49:31.355 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Initialize light states': Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number) on instance: null
2015-09-18 18:49:32.193 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Initialize heating states': Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number) on instance: null
2015-09-18 18:49:32.966 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Initialize contact states': Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number) on instance: null

this is what I see as user rights in gpio before i start openhab

pi@raspberrypi ~ $ ls -l /sys/class/gpio/
total 0
-rwxrwx--- 1 root gpio 4096 Oct  9 09:22 export
lrwxrwxrwx 1 root gpio    0 Jan  1  1970 gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
-rwxrwx--- 1 root gpio 4096 Oct  9 09:23 unexport

and this is after:

pi@raspberrypi ~ $ ls -l /sys/class/gpio/
total 0
-rwxrwx--- 1 root gpio 4096 Oct  9 09:42 export
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio10 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio10
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio11 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio11
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio12 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio12
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio16 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio16
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio17 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio17
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio19 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio19
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio20 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio20
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio21 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio21
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio22 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio22
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio26 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio26
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio27 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio27
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio4 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio4
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio5 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio5
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio6 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio6
lrwxrwxrwx 1 root gpio    0 Oct  9 09:42 gpio9 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio9
lrwxrwxrwx 1 root gpio    0 Jan  1  1970 gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
-rwxrwx--- 1 root gpio 4096 Oct  9 09:42 unexport

and this is my output from the terminal

2015-10-09 09:41:15.949 [INFO ] [runtime.busevents             ] - Heating_FF_Bath state updated to Undefined
2015-10-09 09:41:16.015 [INFO ] [runtime.busevents             ] - Light_Module2 state updated to Undefined
2015-10-09 09:41:16.037 [INFO ] [runtime.busevents             ] - Light_Module1 state updated to Undefined
2015-10-09 09:41:16.063 [INFO ] [runtime.busevents             ] - Light_Balcony state updated to OFF
2015-10-09 09:41:16.170 [INFO ] [runtime.busevents             ] - Light_Module1 state updated to OFF
2015-10-09 09:41:16.674 [INFO ] [runtime.busevents             ] - Light_Module2 state updated to OFF

so what am I doing wrong? I now start and stop openhab by using

sudo sh resetGpio.sh
sudo sh /home/pi/openhab/start.sh

I now moved the whole openhab file folder from /home/pi/ to /etc/ but its the same result…

maybe i can do like here and define my item

Switch Lamp_LV_Standing "Standing Lamp" {exec=">[OFF:sudo /opt/rc-switch/switchOff.sh 1] >[ON:sudo /opt/rc-switch/switchOn.sh 1]"}

which in my case will be something like

Switch Module2  "Module 2" {exec=">[OFF:sudo echo "0" > /sys/class/gpio/gpio2/value 1] >[ON:sudo echo "1" > /sys/class/gpio/gpio2/value 1]"}

From your mail I cannot see what the issue is (I am also new to OH) - how do your items and rules look like?

PS: I would not move the openhab folders from the default folders to your own home dir.

So which folder do you have openhab installed under? I read a tutorial saying that I should have it under /home/pi/openhab and others say etc/openhab.

Maybe I just try to reinstall debian, and openhab then, should i also migrate to openhab2?

There are a lot of tutorials out there with all sorts of instructions for how and where to put it. Most are out of date.

The preferred installation approach on Debian based Linux machines (Raspbian, Ubuntu, Mint, etc) is to use apt-get as described here. This will create the openhab user and group, set openhab to start as a service, let you upgrade b

oth openHAB and addons automatically, and put the various parts of openhab in standardized locations

  • Config: /etc/openhab
  • openHAB home: /usr/share/openhab
  • addons: /usr/share/openhab/addons
  • webapps (where Habmin goes, location of icons): /usr/share/openhab/webapps
  • logs: /var/log/openhab

Will reinstall it tonight and try it out again, I had it installed in /home/pi everything… Because that was what a probably outdated tutorial on instructables said…

Send me a link to that Instructables or post it here. I know @watou has contacted the author of at least one Instructable author about his outdated instructions but I don’t think this was the same one. At a minimum I can put a comment on it pointing to the more up to date instructions.

Thanks

here is one that refers to 1.5 version
and here

this one is good
and this is also superb

Couldn’t find the one with /home/pi right now, have it bookmarked on my personal laptop somwhere.

A final note, how do I change the state of a gpio in my rule, sendCommand(Module1, ON) does not do the the trick if its ON and i want to set it to off.

Thanks guys!

Reinstalling seems so far to work, thanks rlkoshak:) However how can I send the command to turn on my light from my switch, the log file shows that its detected state change but this sendCommand(Light_Kitchen_Item, ON) does not turn it on.

It should just be a matter of configuring the item correctly. Unfortunately I’ve not used GPIO from openHAB so I have no experience to draw upon to help.

Sounds like command is send but not executed on GPIO level. Can you see input from GPIO or switch anything on/off?

How is Light_Kitchen as Item defined?

Below is my item file:

Switch Light_Kitchen 		"Ceiling"  					(Group_Kitchen,Lights)				{ gpio="pin:6" } //Relay 4
/*Contact*/
Contact Contact_LivingRoom1  "Switch Living Room"  (Home, Switches)   {gpio="pin:18 debounce:1500 activelow:yes"}

here is my sitemap:

Frame label="Debug" {
	Group item=Lights icon="light"
	Group item=Switches label="Contacts" icon="kitchen"
	Group item=Heating label="Heating" icon="bath"
	Group item=rgb label="RGB Switch"
}

and this is my rule file:

import org.openhab.core.library.type.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openab.core.types.command
import org.openhab.core.items.genericitem
import java.util.Date
import java.text.SimpleDateFormat

rule "Switch detection"
when 
Item Contact_LivingRoom1 received update
then
	var String Timestamp = String::format( "%1$tH:%1$tM:%1$tS %1$tY-%1$tm-%1$td", new Date() )
	logInfo("test", "Contact_LivingRoom1 state change. State = " + Contact_LivingRoom1.state)
	sendCommand("Light_Kitchen", "ON")
	executeCommandLine("echo halla")
	sendMail("skatun@gmail.com", "Contact_LivingRoom1 state received: (" + Contact_LivingRoom1.state + ")", "Contact switched at " + Timestamp)
	
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("skatun@gmail.com", "HAG system started", "The HAG system started on: " + Timestamp)
end

Thanks for all the tips.

Try SendCommand without " so … sendCommand(Light_Kitchen, ON)

Also easy to use is raspi-gpio get to see actual status