I have been mostly using openhab via MQTT commands to Arduino’s and ESP8266’s but ofcourse have been eying the mainly unused pins on my Raspberry Pi. In all honesty however, when I glanced over the various articles dealing with the use of GPIO pins, I got dizzy
But there is no way but forward, so i decided to just give it a try. It turned out to be much easier than I thought.
First of all, install the GPIO binding in the PaperUI. After one has done that, there isnt really much else to do on that binding. There is no services/gpio.cfg although some of the documentation mentions that file.
Before I go further it is good to note that I used the openhabian distro and I presume a lot of things are already OK in that, but if you did a manual install, it might be good to check if the user ‘openhab’ is member of ‘gpio’ and if not, set that with
sudo adduser openhab gpio
That’s basically all one needs to do.
Let’s setup an ‘input’ which in openhab is a ‘Contact’ and an ‘output’, which in openhab is a Switch. In fact, those are the only states the binding allows.
There is no pressing need to define anything in your sitemap file if e.g. you add the items to a group in your itemsfile, but if you prefer a sitemap then add this to your sitemap file:
Switch item=GPIO_LAMP
Text item=GPIO_BUTTON
Let’s pick some pins to use. It is easiest to use pins on the Raspberry header that are close to a ground pin.
Looking at the pin out of the raspi, we see that for instance GPIO pins 21,13,5, 7, 10, 27,22,24,25,12,16 and 26 are next to a ground pin. there are a few more, but those might have other functions so i stayed away from those.
I picked GPIO23 and GPIO24 as that one is also close to a 3V3 pin and when using that as input one can always decide later to make that have a HIGH or a LOW input.
The configuration string for the binding is defined as follows:
gpio="pin:PIN_NUMBER [debounce:DEBOUNCE_INTERVAL] [activelow:yes|no] [force:yes|no] [initialValue:high|low]"
for those who consider this as double dutch, it will become clear in the items definition:
Add this to your items file:
Switch GPIO_LAMP "Lamp" (LivingRoom) { gpio="pin:23 force:yes" }
Contact GPIO_BUTTON "Button [%s]" (LivingRoom) { gpio="pin:24 activelow:yes" }
The group ‘LivingRoom’ is not mandatory if you have defined your sitemap and of course you may use another group that suits you more.
The Switch that I set up is quite simple. In fact it already would be enough if it would have been:
Switch GPIO_LAMP "Lamp" {gpio="pin:23"}.
But with ‘force:yes’ we force the use of the pin even if it would have been in use by another process. Obviously that might not be the most elegant way to do it, but as i am pretty sure I am not using it for another process, I just wanted to make sure Openhab would have the pin available.
The current of a GPIO pin is far to small to drive any significant load as it can deliver some 60mA I think.
But the output can drive a small LED to test it… but use a 330 ohm series resistor, no matter what other websites tell you: you dont want to fry your GPIO pin. Eventually it would be more useful to drive a small relay module as long as it has a separate transistor to drive the relay (I know, I am cautious)
The configuration allows for some other parameters.
i already mentioned 'force:yes|no
activelow yes|no this means that when the switch is off, there is no voltage on the pin
initialValue:high:low determines the state the pin will take during initialisation.
debounce:interval is used to set the debounce value for the switch in milisecs
If we look at the output, I have not added an activelow because I thought that was the default value (but correct me if I am wrong)
If we look at our input, I set that to activelow, because the way the button works is it applies voltage to trigger the pin and I added it because I was not sure about the default
I did not set a debounce for the contact, but you could state:
Contact GPIO_BUTTON "Button [%s]" (LivingRoom) { gpio="pin:24 debounce:10 activelow:yes" }
This all worked for me, but as said, I used the openhabian distro and that went fine