In the article I wrote earlier Connecting Raspberry Pi and TV via HDMI and checking TV ON / OFF, TV ON / OFF (STANDBY) I told you that you can check the situation from the Raspberry Pi, and if you use it, you can make an elderly watching device without electronic work.
Specifically, there is a package called node-cec
in npm, and if you install this, Node.js will output HDMI-CEC signals. It was possible to send and receive.
However, in order to obtain a card ID with Raspberry Pi using Sony's IC card reader PaSoRi (RC-S380), use the Python library nfcpy
. There is no choice but to use it.
To use PaSoRi (RC-S380) from Node.js, it is necessary to link Python and Node.js (such as receiving JSON including card ID using standard input / output).
Even with PaSoRi (RC-S380), just connect the cable to HDMI and node-cec
and install the npm package. I wish I could get the ID of the card ... and included a Python script to handle nfcpy
[node-nfcpy-id
](https://www.npmjs.com/package/ I made an npm package called node-nfcpy-id).
ssh
in the root directory of MicroSD
--For details, refer to Connect Raspberry Pi and Mac or Windows PC directly by wire and make a quick SSH connection.NFC cards that can be read by PaSoRi (RC-S380) are mainly FeliCa standard and MIFARE standard.
In the FeliCa standard, the card ID is called IDm, and in the MIFARE standard, the card ID is called UID.
With node-nfcpy-id
, you can get the card ID as a string (lowercase alphabetic hexadecimal number).
The card standard can be obtained with the following numerical values (2
, 3
, 4
) according to nfcpy
.
Install the latest version of Node.js and npm by referring to Installing the latest version of Node.js and npm on Raspberry Pi.
nfcpy
and PaSoRiPython 2 is installed by default on Raspbian, so you don't need to install Python itself.
Install python-usb
and python-pip
with apt-get
Raspbian
pi@raspberrypi:~ $ sudo apt-get install python-usb python-pip -y
Install nfcpy-id-reader
(Python script that works with node-nfcpy-id
) with pip.
nfcpy
is also installed when you install nfcpy-id-reader
.
Raspbian
pi@raspberrypi:~ $ sudo pip install -U nfcpy-id-reader
Create a file called /etc/udev/rules.d/nfcdev.rules
to make PaSoRi available even with user privileges.
Raspbian
pi@raspberrypi:~ $ cat << EOF | sudo tee /etc/udev/rules.d/nfcdev.rules
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="06c3", GROUP="plugdev"
EOF
Reboot once
Raspbian
pi@raspberrypi:~ $ sudo reboot
Please install node-nfcpy-id
on the appropriate directory
Raspbian
pi@raspberrypi:~ $ npm install node-nfcpy-id --save
If you get an error like npm ERR! 404'types / node' is not in the npm registry.
when you do npm install
, you may have an older version of npm.
Please refer to Installing the latest version of Node.js and npm on Raspberry Pi to install the latest version of npm.
From version 0.1.0, import (require
-ing) in CommonJS requires default
as shown below.
const NfcpyId = require('node-nfcpy-id').default;
Click here if you want to use the ʻimport` statement in TypeScript, ECMAScript 2015 (using Babel)
import NfcpyId from 'node-nfcpy-id';
You can specify the operation mode at initialization
loop
modeOperate the card reader without stopping each card so that multiple cards can be read in succession. You can get the event when the card leaves the card reader. This is the default mode when nothing is specified in the argument of the constructor.
// const nfc = new NfcpyId({mode: 'loop'});
const nfc = new NfcpyId(); // loop mode
non-loop
modePlace the card on the card reader and release it to stop the card reader. You can get the event when the card leaves the card reader.
const nfc = new NfcpyId({mode: 'non-loop'});
non-touch end
modeOnce the card is placed on the card reader, it will stop the card reader. I can't get the event when the card leaves the card reader.
const nfc = new NfcpyId({mode: 'non-touchend'});
start ()
methodStart reading the card with the card reader
nfc.start();
You can also use the start ()
method at the same time as initialization
const nfc = new NfcpyId().start();
pause ()
methodStop reading the card with the card reader
nfc.pause();
You can register an event in the form of EventEmitter.
touchstart
eventOccurs when a card is placed on a card reader. The card ID and card type are stored in the argument of the callback function.
nfc.on('touchstart', (card) => {
console.log('touchstart', 'id:', card.id, 'type:', card.type);
});
touchend
eventOccurs when the card leaves the card reader (not available in non-touch end
mode)
Returns ture
if the card reader is running, otherwise it returns false
.
console.log(nfc.isRunning); // true or false
When terminating the Node.js process (including control + C etc.), at the same time, make sure that PaSoRi does not emit radio waves, and then Python also terminates the process.
loop
mode exampleWhen the card is placed on the card reader, it prints the card ID and card type, and prints touchend
when the card leaves the card reader.
const NfcpyId = require('node-nfcpy-id').default;
const nfc = new NfcpyId().start();
nfc.on('touchstart', (card) => {
console.log('touchstart', 'id:', card.id, 'type:', card.type);
});
nfc.on('touchend', () => {
console.log('touchend');
});
nfc.on('error', (err) => {
// standard error output (color is red)
console.error('\u001b[31m', err, '\u001b[0m');
});
Raspbian
pi@raspberrypi:~ $ node app
touchstart id: aa7dxxxx type: 2
touchend
touchstart id: 0114b3fxxxxxxxxx type: 3
touchend
touchstart id: 01103f0xxxxxxxxx type: 3
touchend
touchstart id: 0114b3fxxxxxxxxx type: 3
touchend
touchstart id: 04192xxxxxxxxx type: 4
touchend
non-touch end
modeWhen the card is placed on the card reader, the card ID and card type will be output, and reading by the card reader will resume after 5 seconds.
const NfcpyId = require('node-nfcpy-id').default;
const nfc = new NfcpyId({mode: 'non-touchend'}).start();
nfc.on('touchstart', (card) => {
console.log('touchstart:', card.id, 'type:', card.type);
console.log('Resume loading after 5 seconds');
setTimeout(() => {
nfc.start();
}, 5000);
});
nfc.on('error', (err) => {
// standard error output (color is red)
console.error('\u001b[31m', err, '\u001b[0m');
});
In loop
mode or non-loop
mode, if you touch some MIFARE cards such as Aime, the touchend
event will occur immediately after the touchstart
event occurs, and in the loop
mode, this will be repeated. A phenomenon like chattering occurs.
The same phenomenon occurs when nfcpy
is used only in Python.
About this npm package, 2 days in a row !! (1st day) We Are JavaScripters! @ 8th [Beginners welcome LT tournament] "Node.js + Easy IoT with Raspberry Pi "(Slide)
--I thought about publishing it as an npm package and using it, I wrote a process that terminates Node.js with control + C at the same time as Python.
--I bought something like a tester that emits light from an LED with an induced current (Kyoritsu Products KP-NFLEW2), but Python does not finish well. I found that PaSoRi remained emitting radio waves (power did not turn off), and I had to look at the nfcpy
documentation and sample code to signal it well to turn it off.
--Because the signal is used, it is not compatible with Win32.
――I feel that I can control PaSoRi better than writing Python alone.
--Because I had to work with Python, I used signals and command line arguments in addition to standard I / O, which made the code complicated.
――It may have been unavoidable in the sense that it absorbs the troublesome part of linking Python and Node.js.
――I tried to write the document and commit log in English (whether they are grammatically correct or not) with the intention of publishing them on npm.
――I felt the difficulty of creating a document in English.
――This Qiita article became like a Japanese document
--When I introduced ESLint after someone was talking about coding rules in We Are JavaScripters !, I found a habit of making only the first indent 4 half-width spaces (git diff
). Was disappointing)
Use PaSoRi USB Felica Reader (RC-S380) with Raspberry pi Npm module in 3 minutes
Recommended Posts