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.
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
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
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 -
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.
How to handle NFC tags with multiple System Codes in nfcpy Procedure to install nfcpy on Raspberry Pi --March 2016 private version
Recommended Posts