Read and write NFC tags in python using PaSoRi

Overview

Here is a summary of how to read and write inexpensive NFC tags (NTAG) from python on Mac using Sony's PaSoRi (RC-S380), which is a USB-connected NFC / Felica reader / writer. Basically, nfcpy is one shot.

I haven't tried it, but it seems that it can be used in the same way on the Raspberry Pi. (Addition) Even with Raspbian, the installation procedure was OK with just sudo apt-get install python-pip; sudo pip install nfcpy. However, in order to access usb with general privileges, the procedure written in the latter half of Procedure to install nfcpy on Raspberry Pi --March 2016 private version Is required.

Environment setup procedure

Premise

I'm using brew's python 2.7. You should be able to do it with python on the system side, but it is a subtle point what the version of openssl is, so if it does not work, please try and error.

nfcpy The procedure to bring nfcpy with bzr is often introduced, but pip is one shot.

> brew install libusb
> pip install nfcpy

If you are addicted to openssl

It would be nice if I could type import nfc in the python REPL and load it without any problems, but there are cases where the libcrypto included in openssl fails to load.

>>> import nfc
(Omission)
AttributeError: dlsym(0x7fbc82c404a0, EC_KEY_set_public_key_affine_coordinates): symbol not found

As a countermeasure, there are many articles that say that it is OK if you install a new openssl with brew and link it.

> brew link openssl --force
Warning: Refusing to link: openssl

I can't link.

The latest libcrypto.1.0.0.dylib is installed under /usr/local/opt/openssl/lib, so if you specify this as the load path of the dynamic link library, it will work for the time being. What should I do with the correct solution?

export DYLD_LIBRARY_PATH=/usr/local/opt/openssl/lib:$DYLD_LIBRARY_PATH

Reading and writing NFC tags

Official sample of nfcpy

Read the official get-started or [examples](https://github.com/nfcpy/nfcpy/ You can usually see how to use it by looking at the samples in tree / master / examples). The sample alone can also be used as a command (How to use)

For example, you can view NFC tag information with the following command:

> python tagtool.py show

To write the data to open YouTube video to NTAG:

> python ndeftool.py make smartposter -t PPAP https://youtu.be/0E00Zuayv9Q | python tagtool.py load -

Write on your own

If you want to write the necessary processing by yourself using nfcpy, the code will be as follows.

import nfc
import nfc.ndef

def startup(targets):
    print "waiting for new NFC tags..."
    return targets


def connected(tag):
    if not tag.ndef or not tag.ndef.is_writeable:
        print("not a writeable nfc tag")
        return False
    print("old message:")
    print(tag.ndef.message.pretty())

    smartposter = nfc.ndef.SmartPosterRecord("https://youtu.be/0E00Zuayv9Q")
    smartposter.title = "PPAP"
    new_message = nfc.ndef.Message(smartposter)

    if len(str(new_message)) > tag.ndef.capacity:
        print "too long message"
        return True

    if tag.ndef.message == new_message:
        print "already same record"
        return True

    tag.ndef.message = new_message
    print("new message:")
    print(tag.ndef.message.pretty())

    return True


def released(tag):
    print("released:")
    if tag.ndef:
        print(tag.ndef.message.pretty())
    

clf = nfc.ContactlessFrontend('usb')
print(clf)
if clf:
    while clf.connect(rdwr={
        'on-startup': startup,
        'on-connect': connected,
        'on-release': released,
    }):
        pass

Just rewrite the NFC tag to open the YouTube video until you stop with Ctrl-C.

Reference URL

How to handle NFC tags with multiple System Codes in nfcpy Procedure to install nfcpy on Raspberry Pi --March 2016 private version

Recommended Posts

Read and write NFC tags in python using PaSoRi
Read and write JSON files in Python
Read and write single precision floating point in Python
Create and read messagepacks in Python
Write O_SYNC file in C and Python
Write python modules in fortran using f2py
[Python3] Read and write with datetime isoformat with json
Try using ChatWork API and Qiita API in Python
Read DXF in python
Write Python in MySQL
Searching for pixiv tags and saving illustrations using Python
Write tests in Python to profile and check coverage
Read and write csv file
Write Pandoc filters in Python
Write python in Rstudio (reticulate)
Read Euler's formula in Python
Read and write a file
Read Namespace-specified XML in Python
Read Outlook emails in Python
Stack and Queue in Python
Write and read a file
Unittest and CI in Python
Translate using googletrans in Python
Using Python mode in Processing
Read Fortran output in python
Try to make it using GUI and PyQt in Python
Tips for coding short and easy to read in Python
Collect tweets using tweepy in Python and save them in MongoDB
Predict gender from name using Gender API and Pykakasi in Python
Graph time series data in Python using pandas and matplotlib
[Python] Use this to read and write wav files [wavio]
Write a binary search in Python
GUI programming in Python using Appjar
MIDI packages in Python midi and pretty_midi
Precautions when using pit in Python
Difference between == and is in python
View photos in Python and html
Read Protocol Buffers data in Python3
Write JSON Schema in Python DSL
Sorting algorithm and implementation in Python
Authentication using tweepy-User authentication and application authentication (Python)
Write various forms of phylogenetic tree in Python using ETE Toolkit
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Read and use Python files from Python
Write A * (A-star) algorithm in Python
Try using LevelDB in Python (plyvel)
Read PNG chunks in Python (class)
Read big endian binary in Python and convert it to ndarray
Difference in how to write if statement between ruby ​​and python
Check and move directories in Python
Preserve and read order in PyYAML
Using global variables in python functions
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Write selenium test code in python
Build and try an OpenCV & Python environment in minutes using Docker
Hashing data in R and Python
Write a pie chart in Python
Write a vim plugin in Python