Decided last week to change the OS on my Raspberry Pi 2 model B from Wheezy to Jessie (Debian 8) including the OH 1.7.1. Have done a fresh installation of the OS and OH.
Openhab is starting up correctly and the existing functionality is working except an issue with GPIO:
output works as expected
I can control buzzer, sirenes, flashlights, lights etc
input not ok
Somehow OH does not receive the input.
When an input changes (e.g. door open/close) I can see the change on system level.
root@raspberrypi:/sys/class/gpio/gpio14# cat direction
in
root@raspberrypi:/sys/class/gpio/gpio14# cat value
0
root@raspberrypi:/sys/class/gpio/gpio14# cat value
1
These GPIO input changes somehow do not trigger OH. Have experienced this behavior only in the Jessie distribution and not in Wheezy. Does anybody have suggestions or similar experience with Jessie?
Changes to /etc/default/openhab :JAVA_ARGS=-Djna.boot.library.path=/usr/lib/jni
Set root privileges for gpio :sudo adduser openhab gpio
Changes to /etc/openhab/configurations/openhab.cfg :gpio:sys fs=/sys
gpio:debounce=10"
I have successfully implemented the Switch function to turn on and off an LED but can’t seem to get the Contact function working. Have used the following in home.items.
There are no errors in openhab.log but when PIN 15 is toggled up or down (1K resistor to either ground or 3V3) nothing happens and there are no events in event.log. Have also tried another PIN and confirmed that both work from a system command within PuTTY or when set as outputs.The Raspberry Pi is rebooted after a change so the unexport issue shouldn’t be the cause.
The problems you describe souns like the problems I have. However I did not solve the problem and (for the time being) rolled back to Wheezy. I was hoping to stay on the latest OS and find a solution or support here (maybe I continue to work on Jessie next week)
Btw - is your libjna path correct? When I remember correct on Jessie the path was a bit deeper
/usr/lib/arm-linux-gnueabihf (or something like this)
I had the same problem. Neither adding user openhab to the gpio group nor as root user.
The solution was to rebuild …io.gpio with a newer jna library (4.1) and then it worked as root user.
To solve the second problem (i don’t like the idea of running openhab as root) was a bit more difficult. The problem lies in the udev scripts. When the gpio binding reserves a gpio pin a subdirectory for that pin is created. For GPIO17 for example: /sys/class/gpio/gpio17:
The problem is that there is a race condition between the udev script applying the chown command and the gpio binding writing to the pin.
My solution was to add a few lines of code in the GPIOLinx class which, after reserving a pin, enters a loop checking whether openhab has read access to that pin thus waiting for the udev script to finish.
public GPIOPin reservePin(Integer pinNumber) throws IOException {
final String SYSFS_CLASS_GPIO = sysFS + "/class/gpio/";
GPIOPinLinux pin = null;
/* Variable 'sysFS' may be null if mandatory pseudo file system 'sysfs' isn't mounted or mount point can't be determined. */
if (sysFS == null) {
throw new IOException("Mount point for '" + SYSFS_VFSTYPE + "' isn't configured and can't be determined");
}
/* Sanity check, negative pin number is illegal. */
if (pinNumber < 0) {
throw new IllegalArgumentException("Unsupported argument for 'pinNumber' parameter (" + pinNumber + ")");
}
/* Acquiring write lock guarantees atomic check/set operation */
try {
if (gpioLock.writeLock().tryLock(GPIOLOCK_TIMEOUT, GPIOLOCK_TIMEOUT_UNITS)) {
try {
if (gpioRegistry.containsValue(pinNumber)) {
throw new IllegalArgumentException("The pin with number '" + pinNumber + "' is already registered");
}
/* Exports the pin to user space. */
Files.write(Paths.get(SYSFS_CLASS_GPIO + "export"), pinNumber.toString().getBytes());
/* Create backend object */
pin = new GPIOPinLinux(pinNumber, SYSFS_CLASS_GPIO + "gpio" + pinNumber, defaultDebounceInterval);
/* Register the pin */
gpioRegistry.put(pin, pinNumber);
} finally {
gpioLock.writeLock().unlock();
}
} else {
/* Something wrong happened, throw an exception and move on or we are risking to block the whole system */
throw new IOException("Write GPIO lock can't be aquired for " + GPIOLOCK_TIMEOUT + " " + GPIOLOCK_TIMEOUT_UNITS.toString());
}
} catch (InterruptedException e) {
throw new IOException("The thread was interrupted while waiting for write GPIO lock");
}
/* loop until pin is writable
* should be extended with a timeout.
*/
while(!pin.isWriteable()) {
try {
Thread.sleep(100);
} catch(InterruptedException ex) {}
}
return pin;
}
and another few lines of code in GPIOPinLinux.java
I’ve experienced the same problem as described here. I can report that the solution by AlejandroGuirao works for me. Thx Alejandro! I’m not running openhab as root I just added openhab to the gpio group.
It is indeed an incompatibility between OpenHAB (built with libjna 3.2.7) and Raspbian (comes with libjna 4.1.0).
After trying to convince OpenHAB not to use the system jna-lib (-Djna.nosys=true) - which did not work (no idea, why not), I removed libjna-java and libjna-jri.
I did try OpenHAB 1.8.0 (#1113) with the above solution (replacing …io.gpio…) but that did not work. There was no error, but the GPIOs were never initialised.
I haven’t tried, but this should work with the OpenHAB version 1.7.1 from the repository as well.
Steamrunners solution didn’t work for me either, i guess the JAVA_ARGS-thing is the problem?
how do i remove libjna-java and libjna-jri and how to install the linjna-java_3.2.7-4?
i mean what would be the commands i would have to execute in the commandline?
thx for uploading the jar file. i replaced my old gpio binding with yours but it didn’t work.
i don’t really understand the last sentence in your post (JAVA-ARGS), so i guess the problem could lie there?
@poertner
I did try setting the jna.boot.library.path. But didn’t work either. No error, but no initialisation of GPIO ports. I assume the loop is waiting indefinitely for the ports to become available - but that’s just an assumption. I don’t really know java - well I had to learn it a bit and learned to hate it a lot
Not sure if it’s clear from my initial post but you need to replace the org.openhab.io.gpio…jar and keep the existing org.openhab.binding.gpio…jar
Depending on how you installed openhab the documentation tells you to add -Djna.boot.library.path=/usr/lib/jni to either /etc/default/openhab or /opt/openhab/start.sh. I use /usr/lib/arm-linux-gnueabihf/jni in stead of /usr/lib/jni. Not entirely sure if that is needed though. (See here: https://github.com/openhab/openhab/wiki/GPIO-Binding#installation)
Please also note that there are a few other things to know about the gpio binding. (These are not raspbian jessie specific and are discussed elsewhere.)
the binding doesn’t unexport previously used pins, meaning restarting openhab will cause previously used pins to become unusable until reboot. (search forum for other solutions)
the binding does not use the Pi’s internal pull-up or pull-down resistors so all pins will be floating. Either HW resistors need to be used or scripts external to openhab.