引言
我身边大部分朋友是使用JAVA的,他们都对树莓派有不少兴趣,所以我干脆也探索一下如何使用JAVA来操作树莓派的GPIO接口,并写此文给想尝试树莓派的JAVA程序员参考。
Pi4j简要说明
要使用JAVA操作树莓派的GPIO,要借助一个第三方库:PI4J。以下是PI4J的官方说明:
This project is intended to provide a friendly object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform. This project abstracts the low-level native integration and interrupt monitoring to enable Java programmers to focus on implementing their application business logic.
Pi4j是依赖wiringpi封装而成的,不仅能操作GPIO,还能使用I2C协议和SPI协议与硬件设备进行交互。
安装
安装PI4J之前,你需要先在树莓派里面安装两样东西:
最新版的Raspbian系统貌似已经默认安装这两样东西了的。
安装PI4J有两种方式:
方式一(推荐),执行以下命令即可安装完成:
curl -s get.pi4j.com | sudo bash
sudo dpkg -i pi4j-1.1.deb
安装之后,PI4J的库放在以下目录:
/opt/pi4j/lib
如果你想更新PI4J,可以执行以下命令:
sudo apt-get install pi4j
或者
pi4j --update
如果想要卸载PI4J,可以执行以下命令
sudo apt-get remove pi4j 或 pi4j --uninstall
完