Raspian Jessie / GPIO input

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?

Thanks,
Jeroen

I’m having a similar problem.

System: Raspberry Pi 2, Latest Raspbian software as of yesterday. I have installed latest openhab and added the GPIO binding as follows:

Install GPIO binding (also installs libjna) :sudo apt-get install openhab-addon-binding-gpio

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.

Contact Contact_FF_Bath    "Door"    (FF_Bath, Doors) { gpio="pin:15" }

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.

Any suggestions please?

Michael

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)

Sorry, have only been using Raspbery Pi, Raspian and openhub for 2 days so whilst I have that directory, I wouldn’t know if it was the libjna path.

Michael

Same problem on my jessie.

I never solved it / planning to have a more in-depth look during xmas break
(or somebody else should have solved it :sunglasses: )

same problem here.

output (LED on pin7) works fine.
input (contact on pin 8) is not recognised by OH.

input is recognised by the system though… --> $ gpio -readall shows the input on pin 8 correctly.

somebody suggested the “old” JDK version (1.8.0_b132) could be the reason, so i updated it to 1.8.0_65 but OH still ignores the gpio inputs…

I agree with many of the posts here.

I did full, clean install of Jessie on Raspberry Pi 2 using NOOBs 1.5.0.

I cannot detect GPIO inputs. I can see the state change using gpio readall command.

nothing in openhab.log or events.log

Is there some config / debug option that can be activated to learn more where the failure is?

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:

-rwxrwx--- 1 root gpio 4096 Dez 5 13:04 active_low lrwxrwxrwx 1 root gpio 0 Dez 5 13:04 device -> ../../../20200000.gpio -rwxrwx--- 1 root gpio 4096 Dez 5 13:04 direction -rwxrwx--- 1 root gpio 4096 Dez 5 13:04 edge lrwxrwxrwx 1 root gpio 0 Dez 5 13:04 subsystem -> ../../../../../../class/gpio -rwxrwx--- 1 root gpio 4096 Dez 5 13:04 uevent -rwxrwx--- 1 root gpio 4096 Dez 5 13:04 value

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

public boolean isWriteable() { return Files.isWritable(activelowPath) & Files.isWritable(directionPath) & Files.isWritable(edgePath) & Files.isWritable(valuePath); }

I hope this helps.

Thanks for the solution. For us less than highly techie, could you provide more detailed steps:

  • how to update/rebuild with jna 4.1

  • which files (and where are they installed) to modify these lines of code

  • How do we get these changes into the pipeline so they become part of the source code of the system?

thank you for digging for a solution.

i’d be interested in a detailed (for dummies) how-to for your solution too. :wink:

thx,
stefan

Thanks Alejandro - will try this in the upcoming xmas break!

any news on this? Will this be fixed in OH 1.8.0 or do we have to use the fix mentioned above?

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.

For those that find it a little difficult to compile a binding on their own I uploaded the jar file I’m now using to: https://www.dropbox.com/sh/uwoa2pv3o25rcig/AABRvk1wZamI62h2l_ClAJmla?dl=0

Just add this file ot your /usr/share/openhab/addons folder and remove the existing gpio binding.

Please also note that I’m using: JAVA_ARGS=-Djna.boot.library.path=/usr/lib/arm-linux-gnueabihf/jni in /etc/default/openhab

SOLVED

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.

Installing libjna-java_3.2.7-4_armhf.deb (from http://ftp.debian.org/debian/pool/main/libj/libjna-java/) and restarting the GPIO input was initialised properly and changes are reported to OpenHAB.

I am running OpenHAB 1.7.1.

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.

1 Like

@TSchenkel

Steamrunners solution didn’t work for me either, i guess the JAVA_ARGS-thing is the problem? :wink:

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,
stefan

@Steamrunner

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?

cheers,
stefan

To remove the installed version:

sudo apt-get autoremove libjna-java

If it wasn’t installed, then ignore the error.

To download and install the (old) version:

wget http://ftp.debian.org/debian/pool/main/libj/libjna-java/libjna-java_3.2.7-4_armhf.deb

sudo dpkg -i libjna-java_3.2.7-4_armhf.deb

That should work.

@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 :slight_smile:

@poertner

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.