LoginSignup
0
0

More than 1 year has passed since last update.

node.js で PN532 を使ってNFCを読む

Posted at

このあいだ、Raspberry pi で node.js + typescriptで書いてるアプリでNFCを読む機会があり、調べたのでメモ。

ハードウェア

NFCリーダーはこれ

中身は PN532

これを、Raspberry Pi のGPIOピンヘッダにGroveコネクタ生やすやつで接続した。こういうの。
https://amzn.to/3wwxkSB

UART を使用可能にする

Raspberry Pi 3以降ではBluetoothの接続でuartを使っているために、そのままではuartが使えない(これ知らずに結構時間をロスした...)。

Device Tree Overlay の miniuart-bt で対処した。

以下など参照。

node.js

PN532のデバドラは以下のライブラリを使用。

使い方はこんな感じ。

import SerialPort from 'serialport';
import pn532 from 'pn532';

const DEVICE_FILE_PATH = '/dev/serial0';
const SERIAL_OPTION = { baudRate: 115200 };
const serialPort = new SerialPort(DEVICE_FILE_PATH, SERIAL_OPTION);
const rfid = new pn532.PN532(serialPort);

// Poll for a tag
rfid.on('ready', async () => {
    console.log('Listening for a tag scan...');
    rfid.on('tag', (tag) => {
        console.log(tag);
        console.log('tag:', tag.uid);
    });
});

動かすとこんな感じ。

Screen Shot 2021-06-03 at 0.39.13.png

実際には、上記をラップしたdomainオブジェクト作成して、tagを検知した時にeventemitterで飛ばしてる。

0
0
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
0
0