LoginSignup
2
1

More than 1 year has passed since last update.

RaspberryPi + PN532 NFC RFID module + Waveshare Python プログラムを使って Mifare を読む

Last updated at Posted at 2022-03-17

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


Waveshare ライブラリ

WaveshareのPN532HAT
「PN532 NFC HAT for Raspberry Pi, I2C / SPI / UART」
https://www.waveshare.com/pn532-nfc-hat.htm

解説ページ
「PN532 NFC HAT - Waveshare Wiki」
https://www.waveshare.com/wiki/PN532_NFC_HAT

は NXP PN532 を搭載していますが、他の PN532 のボードと同じように使えそうです。
今回は、Waveshare 用に用意されているプログラムを、PN532 NFC RFID moduleで試してみます。

環境

ダウンロード

$ mkdir waveshare
$ cd waveshare
$ wget https://www.waveshare.com/w/upload/6/67/Pn532-nfc-hat-code.7z

7zの解凍ツールをインストールし、アーカイブを解凍します。


$ sudo apt install p7zip
$ 7zr x Pn532-nfc-hat-code.7z 

$ ls
arduino  Pn532-nfc-hat-code.7z  raspberrypi  stm32

arduino,raspberrypi,stm32 用のソフトウェアが用意されているようです。


$ cd raspberrypi
$ ls
c  python

今回は python で試してみます。


$ cd python
$ ls
example_dump_mifare.py  example_get_uid.py    example_rw_mifare.py  example_uart_hex.py    LICENSE
example_dump_ntag2.py   example_read_gpio.py  example_rw_ntag2.py   example_write_gpio.py  pn532

LICENSE ファイルを確認すると、MITライセンスでした。

今回は example_get_uid.py を試してみます。

"""
This example shows connecting to the PN532 with I2C (requires clock
stretching support), SPI, or UART. SPI is best, it uses the most pins but
is the most reliable and universally supported.
After initialization, try waving various 13.56MHz RFID cards over it!
"""

import RPi.GPIO as GPIO

from pn532 import *


if __name__ == '__main__':
    try:
        pn532 = PN532_SPI(debug=False, reset=20, cs=4)
        #pn532 = PN532_I2C(debug=False, reset=20, req=16)

        ic, ver, rev, support = pn532.get_firmware_version()
        print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))

        # Configure PN532 to communicate with MiFare cards
        pn532.SAM_configuration()

        print('Waiting for RFID/NFC card...')
        while True:
            # Check if a card is available to read
            uid = pn532.read_passive_target(timeout=0.5)
            print('.', end="")
            # Try again if no card is available.
            if uid is None:
                continue
            print('Found card with UID:', [hex(i) for i in uid])
    except Exception as e:
        print(e)
    finally:
        GPIO.cleanup()

pn532 設定部分を以下に書き換えます。


        #pn532 = PN532_SPI(debug=False, reset=20, cs=4)
        #pn532 = PN532_I2C(debug=False, reset=20, req=16)
        pn532 = PN532_UART(debug=False, reset=20)

pn532/init.py を編集します。



__all__ = [
    'pn532',
    'i2c',
    'spi',
    'uart',
    'PN532_I2C',
    'PN532_SPI',
    'PN532_UART'
]
from . import pn532
from .i2c import PN532_I2C
from .spi import PN532_SPI
from .uart import PN532_UART

上記の i2c と spi 関係の行をすべてコメントアウトします。
また、RaspberryPi3以降ではない場合はpn532/uart.py のシリアルポート設定箇所も以下のように書き換えます。


DEV_SERIAL          = '/dev/ttyAMA0'

実行

mifare カードをかざしてみます。

$ python3 example_get_uid.py 
Found PN532 with firmware version: 1.6
Waiting for RFID/NFC card...
......................Found card with UID: ['0xc5', '0xb2', '0xf', '0xad']
.Found card with UID: ['0xc5', '0xb2', '0xf', '0xad']
.Found card with UID: ['0xc5', '0xb2', '0xf', '0xad']
.Found card with UID: ['0xc5', '0xb2', '0xf', '0xad']
.Found card with UID: ['0xc5', '0xb2', '0xf', '0xad']
.Found card with UID: ['0xc5', '0xb2', '0xf', '0xad']
.Found card with UID: ['0xc5', '0xb2', '0xf', '0xad']

動作しました。

2
1
0

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
2
1