简体   繁体   English

Raspberry 4 Java Pi4J GPIO 输入不变 state

[英]Raspberry 4 Java Pi4J GPIO Input not changing state

I am trying to use my Raspberry Pi 4's GPIOs to turn on various thing in my smart home.我正在尝试使用我的 Raspberry Pi 4 的 GPIO 打开我智能家居中的各种东西。

I am getting 2.6V on the GPIO Input, thus resulting in a HIGH signal.我在 GPIO 输入上获得 2.6V,从而产生高信号。 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.我已经用基本的 python 脚本确认了这一点,但由于我在 Java 中表现更好,并且我想使用一些 API,所以我想使用 Java,因此我想使用 Pi4J。

Whatever I am doing however, the Inputs are never changing their state. Or rather, the Listener for the input is never triggered.然而,无论我在做什么,输入永远不会改变它们的 state。或者更确切地说,输入的监听器永远不会被触发。 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:我的 pom.xml,我正在使用 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?由于 Pi4J 不再使用 wiringPi,而是使用 BCM 地址,所以我不明白为什么它不起作用?

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? ).root用户身份运行 java 程序(参见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.然后它会识别输入引脚的变化state。

When running the program as user pi I get the error:以用户pi身份运行程序时出现错误:

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

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/此示例描述了此类示例应用程序的所有步骤: https://pi4j.com/getting-started/minimal-example-application/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM