Raspberry PI + PI4J + led blink test

URL : http://pi4j.com/install.html

1. 설치 (쉬운 설치)

pi@was02 ~ $ curl -s get.pi4j.com | sudo bash
====================================================
INSTALLING Pi4J GPG PUBLIC KEY
====================================================
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100  1761  100  1761    0     0   3070      0 –:–:– –:–:– –:–:–  5792
OK
====================================================
ADDING Pi4J APT REPOSITORY
====================================================
–2015-07-05 11:06:55–  http://get.pi4j.com/pi4j.list
Resolving get.pi4j.com (get.pi4j.com)… 54.231.112.122
Connecting to get.pi4j.com (get.pi4j.com)|54.231.112.122|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 42 [application/octet-stream]
Saving to: `/etc/apt/sources.list.d/pi4j.list’

100%[=========================================================>] 42          –.-K/s   in 0s

2015-07-05 11:06:55 (394 KB/s) – `/etc/apt/sources.list.d/pi4j.list’ saved [42/42]

====================================================
UPDATING APT REPOSITORIES
====================================================
Get:1 http://repository.pi4j.com wheezy Release.gpg [514 B]
Get:2 http://repository.pi4j.com wheezy Release [1,778 B]
Get:3 http://repository.pi4j.com wheezy/rpi armhf Packages [377 B]
Ign http://repository.pi4j.com wheezy/rpi Translation-en_GB
Ign http://repository.pi4j.com wheezy/rpi Translation-en
Fetched 2,669 B in 4s (581 B/s)
Reading package lists… Done
====================================================
INSTALLING Pi4J
====================================================
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
pi4j
0 upgraded, 1 newly installed, 0 to remove and 113 not upgraded.
Need to get 616 kB of archives.
After this operation, 919 kB of additional disk space will be used.
Get:1 http://repository.pi4j.com/ wheezy/rpi pi4j all 1.0 [616 kB]
Fetched 616 kB in 3s (162 kB/s)
Selecting previously unselected package pi4j.
(Reading database … 74928 files and directories currently installed.)
Unpacking pi4j (from …/apt/archives/pi4j_1.0_all.deb) …
Setting up pi4j (1.0) …
====================================================
Pi4J INSTALLATION COMPLETE
====================================================

The Pi4J JAR files are located at:
/opt/pi4j/lib 

Example Java programs are located at:
/opt/pi4j/examples

You can compile the examples using this script:
sudo /opt/pi4j/examples/build

Please see http://www.pi4j.com for more information.

pi@was02 ~ $

PI4J 라이브러리 위치 : /opt/pi4j/lb
PI4J 샘플 : /opt/pi4j/examplesPI4J 샘플 실행 : sudo /opt/pi4j/examples/build

2. 샘플빌드
pi@was02 /opt/pi4j/examples $ ./build
———————————————————
[01 of 34] … compiling : BlinkGpioExample.java
[02 of 34] … compiling : BlinkTriggerGpioExample.java
[03 of 34] … compiling : ComputeModuleGpioExample.java
[04 of 34] … compiling : ControlGpioExample.java
[05 of 34] … compiling : CylonGpioExample.java
[06 of 34] … compiling : DebounceGpioExample.java
[07 of 34] … compiling : FrequencyGpioExample.java
[08 of 34] … compiling : I2CWiiMotionPlusExample.java
[09 of 34] … compiling : ListenGpioExample.java
[10 of 34] … compiling : ListenMultipleGpioExample.java
[11 of 34] … compiling : MCP23017GpioExample.java
[12 of 34] … compiling : MCP23S17GpioExample.java
[13 of 34] … compiling : MCP4725GpioExample.java
[14 of 34] … compiling : MultipurposePinGpioExample.java
[15 of 34] … compiling : OlimexGpioExample.java
[16 of 34] … compiling : PCF8574GpioExample.java
[17 of 34] … compiling : PCA9685GpioExample.java
[18 of 34] … compiling : PCA9685GpioServoExample.java
[19 of 34] … compiling : PibrellaExample.java
[20 of 34] … compiling : PiFaceExample.java
[21 of 34] … compiling : PiFaceGpioExample.java
[22 of 34] … compiling : SerialExample.java
[23 of 34] … compiling : ShutdownGpioExample.java
[24 of 34] … compiling : SpiExample.java
[25 of 34] … compiling : StepperMotorGpioExample.java
[26 of 34] … compiling : SystemInfoExample.java
[27 of 34] … compiling : TriggerGpioExample.java
[28 of 34] … compiling : UsageGpioExample.java
[29 of 34] … compiling : WiringPiGpioExample.java
[30 of 34] … compiling : WiringPiGpioInterruptExample.java
[31 of 34] … compiling : WiringPiGpioInterruptExample2.java
[32 of 34] … compiling : WiringPiSerialExample.java
[33 of 34] … compiling : WiringPiSoftPWMExample.java
[34 of 34] … compiling : WiringPiSPIExample.java
———————————————————
sudo java -classpath .:classes:/opt/pi4j/lib/’*’ BlinkGpioExample
3. 소스코드


import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

/**
* This example code demonstrates how to perform simple
* blinking LED logic of a GPIO pin on the Raspberry Pi
* using the Pi4J library.
*
* @author Robert Savage
*/
public class BlinkGpioExample {

public static void main(String[] args) throws InterruptedException {

System.out.println("<--Pi4J--> GPIO Blink Example ... started.");

// create gpio controller
final GpioController gpio = GpioFactory.getInstance();

// provision gpio pin #01 & #03 as an output pins and blink
final GpioPinDigitalOutput led1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);
final GpioPinDigitalOutput led2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03);

// provision gpio pin #02 as an input pin with its internal pull down resistor enabled
final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

// create and register gpio pin listener
myButton.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// when button is pressed, speed up the blink rate on LED #2
System.out.println("event.getState().isHigh() => " +event.getState().isHigh());
if(event.getState().isHigh()){
led2.blink(200);
}
else{
led2.blink(1000);
}
}
});

// continuously blink the led every 1/2 second for 15 seconds
led1.blink(500, 15000);

// continuously blink the led every 1 second
led2.blink(500);

System.out.println(" ... the LED will continue blinking until the program is terminated.");
System.out.println(" ... PRESS <CTRL-C> TO STOP THE PROGRAM.");

// keep program running until user aborts (CTRL-C)
for (;;) {
Thread.sleep(500);
}

// stop all GPIO activity/threads
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
// gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO controller
}
}

4. 회로도

20150707_010424

5. 실행화면

Advertisements

2 thoughts on “Raspberry PI + PI4J + led blink test”

  1. 안녕하세요
    저는 라즈베리파이에 막 입문했는데 제가 하고싶은건 간단합니다.
    바로 위에서 구현하신 것과 같이 LED켜는 문제인데
    꼭 pi4j를 활용해서 자바로 하고싶습니다.
    제가 시도했던것은 위에 있는 예제와 유사한
    http://pi4j.com/example/control.html
    의 문제입니다. 링크의 예제를 제 라즈베리파이에서 실행했을때 콘솔에 정상적으로 불이켜졌다는 문구가 들어오지만 실제 LED에 불이 들어오지 않습니다.
    혹시나 제가 LED와 라즈베리파이 등의 연결을 잘못했나 생각해서 파이썬 코드를 통해서 불을 켜보니 동일한 연결에서 LED 불이 들어오는걸 봐서 물리적인 연결이 잘못된것은 아닌거 같습니다.
    구글에서 검색해보려해도 도저히 방법이 안보이네요..
    혹시라도 방법을 아신다면 도움 부탁드립니다.
    감사합니다.

    1. 안녕하세요~ 프로그램 실행자체는 정상적으로수행되었으니 클래스패스 문제는 아닌것 같습니다. 파이썬 소스로 실행했을때 불이 잘들어온걸 보면 하드웨어 문제도 아니구요.

      제가 위에 올린 소스의 26~27번째 라인에 실제 사용할 GPIO PIN 번호를 지정한 부분이 있습니다.

      아마도 이부분 문제로 보이는데요. 라즈베리 파이B 버전을 기준으로한다면

      소스의 상의 GPIO_01 -> 물리적인 GPIO 핀번호 : 12번

      과 연결하면 됩니다.

      이걸로 해결되시길 바라며…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s