Search

08.(211115)JAVA라이브러리 : Pi4J

Pi4j
라즈베리 파이 GPIO 제어를 위한 자바(JAVA) 라이브러리
공식 깃허브 사이트 주소 : https://github.com/Pi4J/pi4j
라즈베리파이에 Pi4J 설치방법
curl –s get.pi4j.com | sudo bash
C
복사
성공이 완료 되었다면 다음 메시지가 나와야한다.
==================================================== Pi4J INSTALLATION COMPLETE ====================================================
C
복사
설치 진행 시 로그 기록
pi@raspberrypi:~/glory/Desktop_glory $ curl –s get.pi4j.com | sudo bash curl: (3) Failed to convert –s to ACE; string contains a disallowed character % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1670 100 1670 0 0 2044 0 --:--:-- --:--:-- --:--:-- 2041 ==================================================== INSTALLING Pi4J GPG PUBLIC KEY ==================================================== OK ==================================================== ADDING Pi4J APT REPOSITORY ==================================================== ==================================================== UPDATING APT REPOSITORIES ==================================================== Get:1 https://repository.pi4j.com wheezy InRelease [2,341 B] Get:2 https://repository.pi4j.com wheezy/rpi armhf Packages [385 B] Fetched 2,726 B in 1s (2,055 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 118 not upgraded. Need to get 1,703 kB of archives. After this operation, 2,542 kB of additional disk space will be used. Get:1 https://repository.pi4j.com wheezy/rpi armhf pi4j all 1.2 [1,703 kB] Fetched 1,703 kB in 2s (962 kB/s) Selecting previously unselected package pi4j. (Reading database ... 175228 files and directories currently installed.) Preparing to unpack .../apt/archives/pi4j_1.2_all.deb ... Unpacking pi4j (1.2) ... Setting up pi4j (1.2) ... ==================================================== 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 https://pi4j.com for more information.
C
복사
/opt/pi4j/lib 폴더를 확인해 보면 아래의 화면과 같이 Pi4J프로젝트 JAR 파일이 놓여져 있습니다.
pi@raspberrypi:~/glory/Desktop_glory $ cd /opt/pi4j/lib/ pi@raspberrypi:/opt/pi4j/lib $ ls -l total 1024 -rw-r--r-- 1 root root 711098 Feb 28 2019 pi4j-core.jar -rw-r--r-- 1 root root 235536 Feb 28 2019 pi4j-device.jar -rw-r--r-- 1 root root 96756 Feb 28 2019 pi4j-gpio-extension.jar pi@raspberrypi:/opt/pi4j/lib $
C
복사
JAR 파일 확인이 되었으면 아래의 명령어로 BlueJ 설정파일에 라이브러리 경로를 추가합니다.
sudo nano /usr/share/bluej/bluej.defs
C
복사
nano 에디터로 BlueJ 설정파일을 열고나서 Ctrl키와 W키를 눌러 "userlibL"를 입력하여 편집할 위치로 이동합니다. 그리고 나서 아래의 설정정보를 입력하고 저장하면 됩니다.
bluej.userlibLocation=/opt/pi4j/lib
C
복사
그럼 설정한 라이브러리가 잘 로드되었는지를 확인해 보겠습니다.
BlueJ를 실행하여 “Tools” 메뉴에서 “Preferences”를 선택합니다.
“Preferences” 대화 상자가 열리면 아래와 같은 화면이 표시가 됩니다.
“Libraries” 탭을 클릭하면 아래와 같이 설치한 버전의 Pi4J가 로드되어 있는 것을 확인할 수 있습니다.
소스코드가 잘 동작하는지 간단한 테스트를 해보겠습니다. 테스트를 위해 LED 클래스를 만들고 소스코드를 컴파일 해보겠습니다.
"Complie"버튼을 클릭하여 컴파일합니다. 에러없이 정상적으로 컴파일 되었네요.
참고로 Pi4J 라이브러리 설정이 되어 있지 않으면 컴파일시에 오류가 발생합니다. 이상으로 BlueJ에서 Pi4J 프로젝트 사용하는 방법을 마치겠습니다.

안녕하세요

한국전자기술연구원 김영광입니다.
관련 기술 문의와 R&D 공동 연구 사업 관련 문의는 “glory@keti.re.kr”로 연락 부탁드립니다.

Hello

I'm Yeonggwang Kim from the Korea Electronics Research Institute.
For technical and business inquiries, please contact me at “glory@keti.re.kr”