LoginSignup
71
80

More than 3 years have passed since last update.

Raspberry PiでFelicaのIDmを表示する

Last updated at Posted at 2016-02-07

概要

簡単な入室管理のために、まずはRaspberry PiとPaSoRiでカードのIDmを取得する.

動作環境

  • Mac OSX(EI Capitan)
  • Raspberry Pi2 Model B
  • PaSoRi RC-S380P

Raspberry Piセットアップ

OSダウンロード

日本のミラーサイトからダウンロード
https://www.raspbian.org/RaspbianMirrors

イメージのインストール

SD cardの刺さっている場所を調べる

$ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            499.2 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *32.2 GB    disk1
   1:             Windows_FAT_32 NO NAME                 32.2 GB    disk1s1

今回はdisk1。
違う人は、これ以降のdisk1を自分の相応しい値に変更。

アンマウントしておく

$ diskutil unmountDisk /dev/disk1

SD cardにデータをコピー

$ sudo dd bs=1m if=2015-11-21-raspbian-jessie.img of=/dev/rdisk1

Ctrl+Tを押すと途中経過が見える

動作確認

Micro SDカードを基盤に刺して、優先LANを接続し、USBから給電

ip 確認し、ssh接続

$ arp -a
$ ssh pi@[ipアドレス]  #パスワードはraspberry

初期設定

$ sudo raspi-config

Expand Filesystem

3.5GBしか認識してないので、必要なら拡張を行う

Change User Password

piのパスワード変更(多分passwdでも大丈夫)

Internationalisation Options

Timezoneを変更 Asia/Tokyoへ

リポジトリサーバーの変更

/etc/apt/sources.list を編集
ミラーリストをコメントアウトし、日本のリポジトリサーバーを追加

deb http://ftp.jaist.ac.jp/raspbian jessie main contrib non-free rpi
$ sudo apt-get update
$ sudo apt-get upgrade

MicroSDカードの寿命を延ばす

以下を参考にさせてもらった

スワップをゼロにしておく

In Raspberry PI

pi@raspberrypi:~ $ free
             total       used       free     shared    buffers     cached
Mem:        948108     158008     790100      12796      13480      84156
-/+ buffers/cache:      60372     887736
Swap:       102396          0     102396
pi@raspberrypi:~ $ sudo swapoff --all
pi@raspberrypi:~ $ free
             total       used       free     shared    buffers     cached
Mem:        948108     158604     789504      12796      13500      84648
-/+ buffers/cache:      60456     887652
Swap:            0          0          0

再起動後もスワップを設定しないようにする

$ sudo apt-get remove dphys-swapfile

電源をぶっこぬいてチェック

pi@raspberrypi:~ $ free
             total       used       free     shared    buffers     cached
Mem:        948108     157400     790708      12748      13448      83668
-/+ buffers/cache:      60284     887824
Swap:            0          0          0

PaSoRi

以下の記事を参考にさせてもらいました

PaSoRiを接続

pi@raspberrypi:~ $ lsusb 
Bus 001 Device 004: ID 054c:06c3 Sony Corp. 
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Sonyデバイスを発見

nfcpyをインストール

$ sudo apt-get install python-pip
$ sudo pip2 install --upgrade pip
$ sudo pip2 install nfcpy
$ pip show nfcpy
Name: nfcpy
Version: 0.11.1
Summary: Python module for Near Field Communication
Home-page: https://launchpad.net/nfcpy
Author: Stephen Tiedemann
Author-email: stephen.tiedemann@gmail.com
License: EUPL
Location: /usr/local/lib/python2.7/dist-packages
Requires: pyserial, libusb1

pipを更新してインストール

IDmの取得(サンプルを実行してみる)

$ git clone https://github.com/nfcpy/nfcpy.git
$ cd nfcpy
$ git checkout stable/0.11
$ sudo python examples/tagtool.py show
[nfc.clf] searching for reader on path usb
[nfc.clf] using SONY RC-S380/P NFC Port-100 v1.11 at usb:001:005
** waiting for a tag **

試しにSuica をタッチすると、IDmとかが表示される

sudoを使わずに実行できるようにする

lsusb で確認したReaderのIDをつかって実行すると、打つべきコマンドが表示される(nfcpy 0.10以降)

$ python examples/tagtool.py --device usb:054c:06c3
[nfc.clf] searching for reader on path usb:054c:06c3
[main] access denied for device with path usb:054c:06c3
[main] first match for path usb:054c:06c3 is usb:001:005
[main] usb:001:005 is owned by root but you are pi
[main] members of the root group may use usb:001:005
[main] you may want to add a udev rule to access this device
[main] sudo sh -c 'echo SUBSYSTEM==\"usb\", ACTION==\"add\", ATTRS{idVendor}==\"054c\", ATTRS{idProduct}==\"06c3\", GROUP=\"plugdev\" >> /etc/udev/rules.d/nfcdev.rules'
[main] no contactless reader available

指定されたコマンドを実行した後、ReaderのUSBを抜き差しをするとsudo をつけなくても実行できるようになります。

example以外で実行

simple sample

IDmだけとれればいいので、exampleを参考にIDmだけとるスクリプトを作成.
止めるためには、別ターミナルからプロセスをkillする必要あり
とりあえず、SuicaのIDmはとれました

import binascii
import nfc


class MyCardReader(object):

    def on_connect(self, tag):
        print "touched"
        self.idm = binascii.hexlify(tag.idm)
        return True

    def read_id(self):
        clf = nfc.ContactlessFrontend('usb')
        try:
            clf.connect(rdwr={'on-connect': self.on_connect})
        finally:
            clf.close()

if __name__ == '__main__':
    cr = MyCardReader()
    while True:
        print "touch card:"
        cr.read_id()
        print "released"
        print cr.idm
71
80
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
71
80