Raspberry 4 Java Pi4J GPIO Input not changing state

373 Views Asked by At

I am trying to use my Raspberry Pi 4's GPIOs to turn on various thing in my smart home.

I am getting 2.6V on the GPIO Input, thus resulting in a HIGH signal. I have confirmed this with a basic python script, but since I am better in Java and I want to use some APIs, I wanted to use Java and thus Pi4J.

Whatever I am doing however, the Inputs are never changing their state. Or rather, the Listener for the input is never triggered. But the console does print statements generally, the console works.

Here my code:

    public class App {

    private final static int inputOne = 27; // PIN 13, address 27
    private final static Context pi4j = com.pi4j.Pi4J.newAutoContext();

    public static void main(String[] args) throws InterruptedException, IOException {
        final Console console = new Console();
        console.promptForExit();
        // I/O Config Build
        var isInput = DigitalInput.newConfigBuilder(pi4j).id("0").name("inputOne").address(inputOne)
                .pull(PullResistance.PULL_DOWN).debounce(3000L).provider("pigpio-digital-input").build();

        // I/O Build
        var inputOne = pi4j.din().create(isInput);


        inputOne.addListener(e -> {
            if (e.state() == DigitalState.HIGH) {
                console.print("input high");
            } else {

            }
        });
        console.waitForExit();
        pi4j.shutdown();
    }
}

I have also tried using the Board address of the pin instead of the board layout, did result in nothing as well.

My pom.xml, I am using Pi4J v2:

   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Pi_SmartHomeLogic</groupId>
    <artifactId>Pi_SmartHomeLogic</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <release>17</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.4.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>app.App</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <!--<repository> -->
    <!-- <id>caarmen-repo</id>-->
    <!--<url>https://dl.bintray.com/caarmen/maven/</url> -->
    <!--</repository> -->
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.2</version>
            <type>maven-plugin</type>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.3.0</version>
            <type>maven-plugin</type>
        </dependency>
        <dependency>
            <groupId>ca.rmen</groupId>
            <artifactId>lib-sunrise-sunset</artifactId>
            <version>1.1.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.github.zeroone3010</groupId>
            <artifactId>yetanotherhueapi</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-gpio-extension</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-plugin-raspberrypi</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-plugin-pigpio</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.36</version>
        </dependency>
    </dependencies>
</project>

Since Pi4J does no longer use wiringPi, it uses the BCM address instead, so I dont get why it doesnt work?

2

There are 2 best solutions below

1
Frank On

Just to be sure, can you extend your listener a bit like this

inputOne.addListener(e -> {
        console.print(e.state());
        if (e.state() == DigitalState.HIGH) {
            console.print("input high");
        } 
    });

This example describes all the steps for such an example application: https://pi4j.com/getting-started/minimal-example-application/

0
tangens On

I had the same problem. For me it helped to change the line:

var inputOne = pi4j.din().create(isInput);

to (like in the example):

DigitalInputProvider provider = pi4j.provider("pigpio-digital-input");
var inputOne = provider.create(isInput);

and to run the java program as user root (see The big sudo challenge: how can we fix the need to run PiGpio provider applications as sudo?). Then it will recognize the changed state of the input pin.

When running the program as user pi I get the error:

com.pi4j.library.pigpio.PiGpioException: PIGPIO ERROR: PI_INIT_FAILED; pigpio initialisation failed
    at com.pi4j.library.pigpio.impl.PiGpioBase.validateResult(PiGpioBase.java:265) ~[pi4j-library-pigpio-2.2.1.jar:?]
    at com.pi4j.library.pigpio.impl.PiGpioBase.validateResult(PiGpioBase.java:251) ~[pi4j-library-pigpio-2.2.1.jar:?]
    at com.pi4j.library.pigpio.impl.PiGpioNativeImpl.gpioInitialise(PiGpioNativeImpl.java:95) ~[pi4j-library-pigpio-2.2.1.jar:?]
    at com.pi4j.library.pigpio.PiGpio.initialize(PiGpio.java:159) ~[pi4j-library-pigpio-2.2.1.jar:?]
    at com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalInputProviderImpl.create(PiGpioDigitalInputProviderImpl.java:60) ~[pi4j-plugin-pigpio-2.2.1.jar:?]
    at com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalInputProviderImpl.create(PiGpioDigitalInputProviderImpl.java:41) ~[pi4j-plugin-pigpio-2.2.1.jar:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
    at com.pi4j.provider.impl.ProviderProxyHandler.invoke(ProviderProxyHandler.java:100) ~[pi4j-core-2.2.1.jar:?]
    ... 9 more