带有 Pi4j Java 的 Raspberry Pi4 [英] Raspberry Pi4 with Pi4j Java

查看:53
本文介绍了带有 Pi4j Java 的 Raspberry Pi4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pi4j java 库 v1.2 在我的 Raspberry Pi4 上运行一个简单的测试.不知何故,它什么也没做.我也没有在控制台上看到任何错误.它只是在 2 秒后完成.我希望 LED 灯在 2 秒后打开和关闭.

I am trying to run a simple test on my Raspberry Pi4 using the pi4j java library v1.2. Somehow it's not doing anything. I also don't see any errors on console. It just finishes after 2 seconds. I'm expecting the LED light to turn on and turn off after 2 seconds.

我还按照此https://stackoverflow.com/a/63433316/607637 但还是一样.此外,命令 gpio write 19 1 没有做任何事情.

I also updated my gpio binary to v2.52 as per this https://stackoverflow.com/a/63433316/607637 but it's still the same. Also, the command gpio write 19 1 is not doing anything.

GPIO

pi@gtpi:~ $ gpio -v
gpio version: 2.52
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 4B, Revision: 02, Memory: 4096MB, Maker: Sony 
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.
pi@gtpi:~ $ gpio write 19 1  ///---> this also doesn't do anything


Java 应用程序:使用 com.pi4j:pi4j-core:1.2

import com.pi4j.io.gpio.*;
public class Pi4 {
  public static void main(String[] a) throws Exception {
    GpioPinDigitalOutput op = GpioFactory.getInstance().provisionDigitalOutputPin(RaspiPin.GPIO_19);

    op.high();
    Thread.sleep(2000);
    op.low();
 }
}

但是,我编写的用于执行相同操作的 Python 脚本正在运行:

However, a python script that I wrote to do the same is working:

from gpiozero import LED
from time import sleep

red = LED(19)
red.on()
sleep(2)
red.off()

问题:我还需要做什么才能使 Java 应用程序正常工作(打开 LED)?是不是完全不兼容Pi4?

Question: what else do I need to do to make the Java app working(turning on the LED)? Is it not compatible with Pi4 at all?

推荐答案

看来它的答案在这里:

看起来 Pi4j 正在使用不同的引脚编号方案.在幕后",WiringPi 用于控制 GPIO.

It looks Pi4j is using a different pin numbering scheme. "Under the hood", WiringPi is used to control the GPIOs.

所以,我需要告诉它使用 Broadcom PIN 编号方案:

So, I needed to tell it to use Broadcom pin numbering scheme using:

GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

现在可以工作了:

import com.pi4j.io.gpio.*;
public class Pi4 {
  public static void main(String[] a) throws Exception {
    GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

    GpioPinDigitalOutput op = GpioFactory.getInstance().provisionDigitalOutputPin(RaspiPin.GPIO_19);

    op.high();
    Thread.sleep(2000);
    op.low();
 }
}

这篇关于带有 Pi4j Java 的 Raspberry Pi4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆