GPIO - Event listener not working

Here is my item list, i still have to connect all the lights to my 5 double switches somehow. But first i need to get the gpio to work.

Group All

Group GF_Kitchen 
Group GF_Living 
Group FF_Bath 	
Group FF_Bed 		
Group FF_MasterBedroom			
Group GF_Toilet 	
Group GF_Corridor 	


/* active groups */

Group:Switch:OR(ON, OFF) 		Heating 	"No. of Active Heatings [(%d)]" 	<heating> 		(All)


/* Lights */

Switch Light_GF_Living_Table 		"Module 1" 		(GF_Living) { gpio="pin:12" }  
Switch Light_GF_Center 				"Module 2" 		(GF_Living) { gpio="pin:19" }   
Switch Light_GF_Tv 					"Module 3" 		(GF_Living) { gpio="pin:16" }
Switch Light_GF_Bar 				"Module 4" 		(GF_Living) { gpio="pin:26" }
Switch Light_GF_Elefant 			"Elefant" 		(GF_Living) { gpio="pin:6" }
Switch Light_GF_Glass 				"Glass" 		(GF_Living) { gpio="pin:13" }
Switch Light_GF_WallWasher 			"Wall washer" 	(GF_Living) { gpio="pin:10" }
Switch Light_FF_Bath_Ceiling 		"Ceiling" 		(FF_Bath) { gpio="pin:9" }
Switch Light_SF_Corridor_Ceiling 	"Ceiling"  		(GF_Corridor){ gpio="pin:20" }

Switch Light_SF_Corridor_Door 	"Door Opener" <heating> (GF_Corridor){ gpio="pin:22" }


/* Heating */

Switch Heating_GF_Corridor 	"Hallway Heating" 	<heating>	(GF_Corridor, Heating) { gpio="pin:17" }
Switch Heating_GF_Living 	"Livingroom" 		<heating>	(GF_Living, Heating){ gpio="pin:27" }
Switch Heating_GF_Kitchen 	"Kitchen" 			<heating>	(GF_Kitchen, Heating){ gpio="pin:11" }
Switch Heating_FF_Bath 		"Bath" 				<heating>	(FF_Bath, Heating){ gpio="pin:5" }
Switch Heating_FF_Office 	"Master bedroom" 	<heating>	(FF_MasterBedroom	, Heating){ gpio="pin:21" }
Switch Heating_FF_Bed 		"Bedroom" 			<heating>	(FF_Bed, Heating){ gpio="pin:4" }

Hi,

don’t you have to use “Contact” for input?

Did you mind to correctly pull up/down our input at the GPIO pin?

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

It works when I start openhab in debugmode with start_debug.sh, strange…

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.

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