0

この記事は最終更新日から1年以上が経過しています。

Raspberry Pi で nfcpy + PN532 NFC RFID module

投稿日

image.png

(Felica/Mifare/NFC チャレンジシリーズ) その他の記事はこちら 「Felica/Mifare/NFC でいろいろ実験」
https://qiita.com/nanbuwks/items/1f416d6e45a87250ee0a


「Raspberry Pi で libnfc + PN532 NFC RFID module」
https://qiita.com/nanbuwks/items/0c73add503b354774035
では Mifare を読むテストをしました。

さて、FeliCa を読むにはどうしたらいいかな? libnfc では FeliCa 用の機能が揃っていないので、nfcpy をベースに考えてみました。

nfcpy は PN532 自体には対応しています。PN532 は SPI/I2C/高速シリアル でインターフェースが用意されていますが、現在 nfcpy で使えるのは高速シリアルインターフェース接続のみのようです。ソースを見てみると、SPI/I2C接続対応にもできそうですが今回は既存のコードを使うことにします。

環境

  • PN532 NFC RFID module
  • Raspberry Pi Model B+ V1.2
    • Raspberry Pi OS Lite armhf-2020-12-04
  • Python 3.7.3
  • 1.0.4 version of nfcpy

接続

RaspberryPi と PN532 NFC RFID module の接続はUSBシリアルモジュール経由してもいいですが、今回はGPIO上のシリアルTX/RXピンにつなげてみます。

image.png

Raspberry Pi PN532 NFC RFID module 写真上のケーブル色
PIN4 5V VCC
PIN6 GND GND
PIN8 TX RX
PIN10 RX TX

また、基板上のスイッチを以下のように設定します。
image.png

インストール

nfcpy と PaSoRi RC-S380/P をセットアップ
https://qiita.com/nanbuwks/items/348aa1849a12079d75cd

でセットアップします。
なお、今回は


sudo pip3 install -U nfcpy

でインストールし、nfcpy のバージョンを 1.0.4 としています。

( sudo pip3 install nfcpy だと 1.0.3 が入る )

動作確認

$ python3 -m nfc
This is the 1.0.4 version of nfcpy run in Python 3.7.3
on Linux-5.10.63+-armv6l-with-debian-10.11
I'm now searching your system for contactless devices
I'm not trying serial devices because you haven't told me
-- add the option '--search-tty' to have me looking
-- but beware that this may break other serial devs
Sorry, but I couldn't find any contactless device

と出たので --search-tty オプションをつけました。


$ python3 -m nfc --search-tty
This is the 1.0.4 version of nfcpy run in Python 3.7.3
on Linux-5.10.63+-armv6l-with-debian-10.11
I'm now searching your system for contactless devices
** found PN532v1.6 at /dev/ttyAMA0

認識しました。

FeliCa 読んでみる

tagtool.py を使ってみました。


$ python3 tagtool.py
[nfc.clf] searching for reader on path usb
[nfc.clf] no reader available on path usb
[main] no contactless reader found on usb
[main] no contactless reader available

と出たのでヘルプを見てみます。


$ python3 tagtool.py --help
usage: tagtool.py [-h] [-p PASSWORD] [--wait] [--technology {A,B,F}]
                  [-d MODULE] [-v] [-q] [-f LOGFILE] [--reltime] [--abstime]
                  [--device PATH] [-l]
                  {show,dump,load,format,protect,emulate} ...

optional arguments:
  -h, --help            show this help message and exit
  -p PASSWORD           unlock with password if supported
  -l, --loop            restart after termination

commands:
  {show,dump,load,format,protect,emulate}
    show                pretty print ndef data
    dump                read ndef data from tag
    load                write ndef data to tag
    format              format ndef tag
    protect             write protect a tag
    emulate             emulate an ndef tag

Reader Mode Options:
  --wait                wait until tag removed (implicit with '-l')
  --technology {A,B,F}  poll for a single technology (default: all)

Debug Options:
  -d MODULE             enable debug log for MODULE (main, nfc.clf, ...)
  -v, --verbose         show more information
  -q, --quiet           show less information
  -f LOGFILE            write debug logs to LOGFILE (with date and time)
  --reltime             show relative timestamps in screen log
  --abstime             show absolute timestamps in screen log

Device Options:
  --device PATH         use contactless reader at: 'usb[:vid[:pid]]' (with
                        vendor and product id), 'usb[:bus[:dev]]' (with bus
                        and device number), 'tty:port:driver' (with
                        /dev/tty<port> and <driver>), 'com:port:driver' (with
                        COM<port> and <driver>), 'udp[:host[:port]]' (with
                        <host> name/addr and <port> number)

と出たので以下のようにしてみました。


$ python3 tagtool.py --device tty:AMA0
[nfc.clf] searching for reader on path tty:AMA0
[nfc.clf] using PN532v1.6 at /dev/ttyAMA0
** waiting for a tag **

手元の nanaco カードをかざすと


Type3Tag 'FeliCa Standard (RC-S960)' ID=0114B4276B115A23 PMM=0F0D23042F7783FF SYS=8B61

うまくいきました

新規登録して、もっと便利にQiitaを使ってみよう

  1. あなたにマッチした記事をお届けします
  2. 便利な情報をあとで効率的に読み返せます
ログインすると使える機能について

コメント

この記事にコメントはありません。
あなたもコメントしてみませんか :)
新規登録
すでにアカウントを持っている方はログイン
0

Qiitaにログインしてダークテーマを使ってみませんか?🌙

ログインするとOSの設定にあわせたテーマカラーを使用できます!