[JAVA] I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi

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).

Python-to-Nodejs.png

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).

npm-node-nfcpy-id.png

Operating environment

Raspberry Pi side

IC card reader

P6110607.jpg

About card ID and card type

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.

Installation

Install Node.js

Install the latest version of Node.js and npm by referring to Installing the latest version of Node.js and npm on Raspberry Pi.

Settings for using nfcpy and PaSoRi

Python 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.

how to use

Feature Description

import

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';

Initialization (specify mode)

You can specify the operation mode at initialization

loop mode

Operate 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 mode

Place 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 mode

Once 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'});

Method

start () method

Start 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 () method

Stop reading the card with the card reader

nfc.pause();

Event

You can register an event in the form of EventEmitter.

touchstart event

Occurs 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 event

Occurs when the card leaves the card reader (not available in non-touch end mode)

Getter

ʻIsRunning` property

Returns ture if the card reader is running, otherwise it returns false.

console.log(nfc.isRunning); // true or false

At the end of Node.js

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.

Illustration of each mode method event

node-nfcpy-id-modes.gif

sample

loop mode example

When 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');
});

Execution example

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

Example of non-touch end mode

When 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');
});

Known issues

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.

LT announcement

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) wejs-slide-1.png

Try making an npm package

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

KP-NFLEW2

Reference site

Use PaSoRi USB Felica Reader (RC-S380) with Raspberry pi Npm module in 3 minutes

Recommended Posts

I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
I tried to automate the watering of the planter with Raspberry Pi
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
I sent the data of Raspberry Pi to GCP (free)
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
I made a web server with Raspberry Pi to watch anime
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
I tried to get the movie information of TMDb API with Python
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Connect to the console of Raspberry PI and display local IP and SD information
I made you to express the end of the IP address with L Chika
I made an appdo command to execute a command in the context of the app
With LINEBot, I made an app that informs me of the "bus time"
Get CPU information of Raspberry Pi with Python
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
Get temperature and humidity with DHT11 and Raspberry Pi
WeWork office keys can now be unlocked / locked with an IC card using the smart lock "SESAME mini" and Raspberry Pi Zero WH.
I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
An introduction to cross-platform GUI software made with Python / Tkinter! (And many Try and Error)! (In the middle of writing)
[Blender] How to get the selection order of vertices, edges and faces of an object
When I tried to do socket communication with Raspberry Pi, the protocol was different
I tried to automate the article update of Livedoor blog with Python and selenium.
I just wanted to extract the data of the desired date and time with Django
I tried to compare the processing speed with dplyr of R and pandas of Python
I want to be notified of the connection environment when the Raspberry Pi connects to the network
Easy IoT to start with Raspberry Pi and MESH
Try to visualize the room with Raspberry Pi, part 1
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Try to get the contents of Word with Golang
A story that I wanted to realize the identification of parking lot fullness information using images obtained with a Web camera and Raspberry Pi and deep learning.
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
I made a tool to get the answer links of OpenAI Gym all at once
Make a note of what you want to do in the future with Raspberry Pi
I made a class to get the analysis result by MeCab in ndarray with python
I made an API with Docker that returns the predicted value of the machine learning model
Easy introduction to home hack with Raspberry Pi and discord.py
I tried to find the entropy of the image with python
Try to get the function list of Python> os package
I tried to get the location information of Odakyu Bus
Get the id of a GPU with low memory usage
I made a package to filter time series with python
I tried running Movidius NCS with python of Raspberry Pi3
Create an LCD (16x2) game with Raspberry Pi and Python
Get the package version to register with PyPI from Git
I want to get the operation information of yahoo route
Production of temperature control system with Raspberry Pi and ESP32 (1)
I made a function to check the model of DCGAN
I made a resource monitor for Raspberry Pi with a spreadsheet
I tried using the DS18B20 temperature sensor with Raspberry Pi
I want to know the features of Python and pip
I made a surveillance camera with my first Raspberry PI.
Keras I want to get the output of any layer !!
Get the weather using the API and let the Raspberry Pi speak!
Get the source of the page to load infinitely with python.
I made a Line bot that guesses the gender and age of a person from an image