1. 程式人生 > >第一個樹莓派JAVA測試程式-LED燈控制

第一個樹莓派JAVA測試程式-LED燈控制

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");
    }
}

j8header-3b.png

注意編號為左右兩邊最外側的數字

5.編譯和執行

javac -classpath .:classes:'*':classes:/opt/pi4j/lib/'*' Led.java

java -classpath .:classes:'*':classes:/opt/pi4j/lib/'*' Led

AA3322C2BA8478DE1FEE605DA453D62A