您的位置:首页 > 编程语言 > Java开发

第一个树莓派JAVA测试程序-LED灯控制

2017-11-18 00:08 387 查看
1. 安装JDK(用SecuritFX)
上传jdk-8u151-linux-arm32-vfp-hflt.tar.gz到树莓派/home/pi

tar -zxvf jdk-8u151-linux-arm32-vfp-hflt.tar.gz
sudo nano /etc/profile
在最后加入:
JAVA_HOME=/home/pi/jdk1.8.0_151
CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool$
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
Ctrl+O,回车,保存
Ctrl+X,退出
sudo reboot
重启后生效

2. 安装wiringPi
上传wiringPi-96344ff.tar.gz,路径同上。
tar xfz wiringPi-96344ff.tar.gz
cd wiringPi-96344ff
./build

3. 安装pi4j
上传pi4j-1.2-SNAPSHOT.deb,路径同上,注意树莓派3一定要安装这个版本,低版本不能正确运行。
cd ..
sudo dpkg -i pi4j-1.2-SNAPSHOT.deb

4.新建一个JAVA程序Led.java
/*
* #%L
* **********************************************************************
* ORGANIZATION  :  Pi4J
* PROJECT       :  Pi4J :: Java Examples
* FILENAME      :  ControlGpioExample.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here:  http://www.pi4j.com/
* **********************************************************************
*/
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
/**
* This example code demonstrates how to perform simple state
* control of a GPIO pin on the Raspberry Pi.
*
* @author Robert Savage
*/
public class Led {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("<--Pi4J--> GPIO Control Example ... started.");
        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();
        // provision gpio pin #01 as an output pin and turn on
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);
        // set shutdown state for this pin
        pin.setShutdownOptions(true, PinState.LOW);
        System.out.println("--> GPIO state should be: ON");
        Thread.sleep(5000);
        // turn off gpio pin #01
        pin.low();
        System.out.println("--> GPIO state should be: OFF");
        Thread.sleep(5000);
        // toggle the current state of gpio pin #01 (should turn on)
        pin.toggle();
        System.out.println("--> GPIO state should be: ON");
        Thread.sleep(5000);
        // toggle the current state of gpio pin #01  (should turn off)
        pin.toggle();
        System.out.println("--> GPIO state should be: OFF");
        Thread.sleep(5000);
        // turn on gpio pin #01 for 1 second and then off
        System.out.println("--> GPIO state should be: ON for only 1 second");
        pin.pulse(1000, true); // set second argument to 'true' use a blocking call
        // stop all GPIO activity/threads by shutting down the GPIO controller
        // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
        gpio.shutdown();
        System.out.println("Exiting ControlGpioExample");
    }
}



注意编号为左右两边最外侧的数字
 
5.编译和运行
javac -classpath .:classes:'*':classes:/opt/pi4j/lib/'*' Led.java
java -classpath .:classes:'*':classes:/opt/pi4j/lib/'*' Led




 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: