2015년 5월 9일 토요일

라즈베리파이 NFC 사용

Step 1. NFC Reader 준비.
 NFC리더는 UART 사용을 위해 점퍼를 0 0 으로 설정 (본인 구매 제품은 땜질 되어 있음)
사진 좌측 상단에

1         1
s0      s1
0        0

으로 인쇄되어 있는데, 사진처럼 s0 + 0, s1 + 0 땜질 되어 있으면  0 0 임,


NFC 리더와 라즈베리파이는 다음과 같이 연결한다.
NFC tx ---> Rpi pin10 (UART rx)
NFX rx ---> Rpi pin8 (UART tx)
송/수신이 바뀔경우 아래와 같이 nfc-poll 을 실행할 경우 에러가 발생한다.(nfc-poll은 Step 1 에서 실행 할 수 없음.)
pi@doorpi ~/libnfc-1.7.1/examples $ sudo ./nfc-poll
/home/pi/libnfc-1.7.1/examples/.libs/lt-nfc-poll uses libnfc 1.7.1
error    libnfc.chip.pn53x    Unexpected PN53x reply!
error    libnfc.driver.pn532_uart    pn53x_check_communication error
lt-nfc-poll: ERROR: Unable to open NFC device.

pinout 에 대한 자세한 정보는 google 에서 검색 해 보시라...


Step 2. 라즈베리파이 UART 통신 enable
기본설정으로 라즈베리파이의 UART는 시스템의 Serial console 용으로 설정되어 외부 장치의 입/출력을 받을 준비가 되어 있지 않다. 아래의 절차를 통해 외부 입/출력이 가능하도록 수정한다.
pi@doorpi ~ $ sudo raspi-config





 8 Advanced Options 선택.


 A8 Serial 선택.


 <No> 선택



 <OK> <Finish>

Raspberry Pi 재부팅.

Step3. libnfc 컴파일


https://bintray.com/nfc-tools/sources/libnfc  에서


libnfc-1.7.1.tar.bz2 를 다운로드.

pi@doorpi ~ $ mkdir libnfc 
pi@doorpi ~ $ cd libnfc/ 
pi@doorpi ~ $ cp libnfc-1.7.1.tar.bz2 ~/libnfc/
pi@doorpi ~ $ bzip2 -d libnfc-1.7.1.tar.bz2
pi@doorpi ~ $ tar -xvf libnfc-1.7.1.tar
libnfc-1.7.1/
libnfc-1.7.1/install-sh
libnfc-1.7.1/CMakeLists.txt
libnfc-1.7.1/aclocal.m4
libnfc-1.7.1/INSTALL
-- snip --

conf 디렉터리 만들고 conf 파일 복사.

pi@doorpi ~ $ mkdir -p /etc/nfc/devices.d

pi@doorpi ~ $ cd libnfc-1.7.1/

sudo cp contrib/libnfc/pn532_uart_on_rpi.conf.sample /etc/nfc/devices.d/pn532_uart_on_rpi.conf
sudo nano /etc/nfc/devices.d/pn532_uart_on_rpi.conf
줄 맨 마지막에  아래의 내용을 추가
allow_intrusive_scan = true

아래와 같이 된다.
pi@doorpi ~ $ cat /etc/nfc/devices.d/pn532_uart_on_rpi.conf
## Typical configuration file for PN532 device on R-Pi connected using UART
## Note: to use UART port on R-Pi, you have to disable linux serial console:
##   http://learn.adafruit.com/adafruit-nfc-rfid-on-raspberry-pi/freeing-uart-on-the-pi
name = "PN532 board via UART"
connstring = pn532_uart:/dev/ttyAMA0
allow_intrusive_scan = true



라즈베리파이를 재부팅.

pi@doorpi / $ sudo reboot


pi@doorpi ~/libnfc-1.7.1 $ sh ./configure --with-drivers=pn532_uart --sysconfdir=/etc --prefix=/usr
fatal: Not a git repository (or any of the parent directories): .git
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
--snip --

Selected drivers:
   acr122_pcsc...... no
   acr122_usb....... no
   acr122s.......... no
   arygon........... no
   pn53x_usb........ no
   pn532_uart....... yes
   pn532_spi.......  no
   pn532_i2c........ no

pn532_uart 를 사용하도록 되었는지 확인.

pi@doorpi ~/libnfc-1.7.1 $ sudo make clean
Making clean in test
make[1]: Entering directory '/home/pi/libnfc-1.7.1/test'
test -z "" || rm -f
test -z "" || rm -f
rm -rf .libs _libs
test -z "" || rm -f
rm -f *.o
--snip --


pi@doorpi ~/libnfc-1.7.1 $ sudo make install all
Making install in libnfc
make[1]: Entering directory '/home/pi/libnfc-1.7.1/libnfc'
Making install in chips
make[2]: Entering directory '/home/pi/libnfc-1.7.1/libnfc/chips'
  CC     libnfcchips_la-pn53x.lo
  CCLD   libnfcchips.la
make[3]: Entering directory '/home/pi/libnfc-1.7.1/libnfc/chips'
--snip --



nfc-poll 을 실행 한 후 리더에 스마트 폰이나 nfc 를 접촉하면 데이터 가 수신된다.
pi@doorpi ~/libnfc-1.7.1 $ cd examples/
pi@doorpi ~/libnfc-1.7.1/examples $ ./nfc-poll
/home/pi/libnfc-1.7.1/examples/.libs/lt-nfc-poll uses libnfc 1.7.1
NFC reader: pn532_uart:/dev/ttyAMA0 opened
NFC device will poll during 30000 ms (20 pollings of 300 ms for 5 modulations)
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04 
       UID (NFCID1): e3  28  ca  de 
      SAK (SEL_RES): 08 
nfc_initiator_target_is_present: Target Released
Waiting for card removing...done.


이제 nfc-poll을 샘플삼아 내게 맞는 코딩을 작성하면 되겠다.

#### PN532 uart for raspberry pi 3 (2016/07/24)

Raspberry pi 3 에서 /dev/AMA0 는 bluetooth 를 위해 사용되고. GPIO를 통한 uart는 /dev/serial0 으로 바뀌었다.

/boot/config.txt 의 맨 마지막 줄에 다음과 같은 라인 추가.
core_freq=250
enable_uart=1

라즈베리파이 재부팅

/etc/nfc/devices.d/pn532_uart_on_rpi.conf  파일내 uart port 변경
pi@raspberrypi:~/libnfc-1.7.1/examples $ cat /etc/nfc/devices.d/pn532_uart_on_rip.conf
## Typical configuration file for PN532 device on R-Pi connected using UART
## Note: to use UART port on R-Pi, you have to disable linux serial console:
##   http://learn.adafruit.com/adafruit-nfc-rfid-on-raspberry-pi/freeing-uart-on-the-pi
allow_intrusive_scan=true
name = "PN532 board via UART"
connstring = pn532_uart:/dev/serial0

####  테스트
pi@raspberrypi:~/libnfc-1.7.1/examples $ ./nfc-poll
/home/pi/libnfc-1.7.1/examples/.libs/lt-nfc-poll uses libnfc 1.7.1
NFC reader: pn532_uart:/dev/serial0 opened
NFC device will poll during 30000 ms (20 pollings of 300 ms for 5 modulations)
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04
       UID (NFCID1): 83  b3  a6  ef
      SAK (SEL_RES): 08
nfc_initiator_target_is_present: Target Released
Waiting for card removing...done.


2015년 4월 27일 월요일

Raspberry pi A+ 를 이용한 BMO 만들기.

3D objects : http://www.thingiverse.com/thing:697480

3Derp 가 제작 하고 공개한 라즈베리파이 A+ 를 이용한 BMO 만들기에 도전한다.


사용 부품 : 
1. 3D print parts
BMO 3D 프린팅을 원하면 이곳을 클릭!! 3D Print
2. 기판 - 양면인쇄 된 것. = \3,200
3. Tactile switch 7.8x7.8x5.0mm SKPDAMD010 x 12개 = 약\6,000
4. Raspberry Pi Model A+ 256MB RAM x 1개 = \34,100
5. Mini Metal Speaker w/ Wires - 8 ohm 0.5W x 1개 = 약\4,000
6. SparkFun Mono Audio Amp Breakout - TPA2005D1 x 1개 = 약 \17,000
7. Teensy (ATmega32u4 USB dev board) 2.0 - ATmega32u4 x 1개  = \18,000 
8. 3.5 Inch TFT LCD Monitor for Car / Automobile x 1개 = 약\20,000
9. Li-Ion 18650 7.4V 2600mAh Rechargeable Battery x1개 = \25,000
10. Charger for Li-Ion battery = \10,000
11. DC/DC Step-Down 5V @ 1.5A = \6,500
12. Size M Panel-mount Coaxial Power Jack  = \500
13. DPDT Submini Slide Switch = \500

제작기간 : 부품 주문하고 기다리고 제작 하는데 1개월 조금 넘음.

총 구매비용 : 대충 \144,800
3D 프린팅 비용은 제외 했는데, 포함 시키면 총 18만원 정도 할라나?? 그냥 닌텐도 게임기 하나 사는 금액 이다.

원작자는 $159.74 에 만들었다고 공개 했다. 뭐가 다른가 봤더니 원작자는 대부분 adafruit 에서 구매 했다. 좀 비싼곳 이긴 하다. 나 는 aliexpress 에서 구매...

Tactile switch SKPDAMD010: 이거 구하기 정말 어려웠다. 이 부품은 누름 스위치 인데, 고무(실리콘) 재질로 되어 있어 흡사 TV리모콘 누르듯 물렁물렁하게 작동하는 스위치 이다.
Tactile switch를 구하고자 하는분 용산으로 가자! 온라인에서는 구하기 힘들다.

TEENSY2.0 : TEENSY 는 USB Joystick 을 만들기 위해 필요함. aliexpress 에서 구매. 약 16달러 정도.

SparkFun Mono Audio Amp Breakout - TPA2005D1  : mono amp 2개로 stereo 구현을 위한 것. raspberry pi 에서 오디오 출력이 약해 amp를 써야 스피커로 들을 수 있음. aliexpress 에서 구매.

배터리 : 용산에 친절하게 배터리 제작해 주시는 분들 있음. 보호회로 있는 18650 배터리 2개 + 충전 port 달아 달라고 하면 잘 만들어 주심.

해외구매를 제외하고 대부분 부품은 용산 전자랜드 지하 에 있는 동신전자 와 주변 가게 에서 구했다.

제작자는 너무도 친절하게 사이트에 모든 구성에 대한 설계를 올렸다. 회로에 대한 지식이 없는 본인도 만들 수 있도록 잘 만들어져 있다. 아래 3개 파일을 보면 대충 아~ 이렇게 만드는군... 할 수 있다.
Routing_Board_Schematic.pdf
RPi_Wiring_Guide.pdf
Controller_Assembly_.PDF

http://www.thingiverse.com/thing:697480 에서 다운로드 하여 보길 바란다.

Controller_Assembly_.PDF 를 보고 3.1mm 드릴로 구멍 만들고,


 tactike switch를 구성.

 BMO 몸체와 대조 하여 정확하게 일치 하는지 확인.



가조립 상태.




 뒷면에 Raspberry pi가 장착 된다.

joypad 부분은 아래와 같이 wiring 하고.

audio amp 부분은 아래와 같이 wiring.

막 조립.

 까만것은 배터리 이다. 배터리와 연결된 빨간 커넥터는 충전용 배선.


조립! 완성!



Retropie 2.6 이미지 를 설치 http://blog.petrockblock.com/retropie/

mame를 실행해 추억의 겔러그를 실행한 모습. 처음 mame 구동시 joypad가 작동하지 않는데, 조립전 USB hub를 이용해 keyboard와 joypad를 연결한 후 설정을 해야 한다.(Raspberry pi A+ 는 USB port가 1개 제공되므로, USB hub로 연결.)

mame 구동 -> TAB 키 누름 -> joypad 설정.


제비우스도 됨.ㅋ