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

1. 安装JDK(用SecuritFX)

上传jdk-8u151-linux-arm32-vfp-hflt.tar.gz到树莓派/home/pi

clip_image002

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

AA3322C2BA8478DE1FEE605DA453D62A

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值