The below guide is to setup a pn532 nfc reader on a raspberry pi.
First up make surer the PN532 is setup in UART mode, and connect to the pi in the same manor as the diagram below.
PN532 Rfid read connected to a Raspberry PI
Before getting started setup your pi with the latest debian image and complete your standard setup. Once the pi is setup run the commands below installing the necessary libraries.
Run the below commands to install the tools and libraries we will need for the rest of the setup.
1 2 |
sudo apt-get install git build-essential autoconf libtool libpcsclite-dev
sudo apt-get install libusb-dev libcurl4-openssl-dev libjson-c-dev python-pip
|
Download the nfc library from github.
1 2 3 |
git clone https://github.com/nfc-tools/libnfc.git
cd libnfc
sudo mkdir -p /etc/nfc/devices.d/
|
Copy the relevant config to the correct place on the pi.
1 |
sudo cp contrib/libnfc/pn532_uart_on_rpi_3.conf.sample /etc/nfc/devices.d/pn532_uart_on_rpi_3.conf
|
Compile the libnfc library.
1 2 3 |
autoreconf -vis
./configure --with-drivers=pn532_uart --sysconfdir=/etc --prefix=/usr
sudo make clean && sudo make install all
|
Install the python library
1 |
pip install -U nfcpy
|
To check everything is working try running the command below to make sure your reader is detected and listed.
1 |
nfc-scan-device
|
Now create your first python program to make sure everything is working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import nfc
import ndef
from nfc.tag import tt1
from nfc.tag import tt2
from nfc.tag import tt3
from nfc.tag import tt4
tagtypes = (
('uid', nfc.tag.tt1.Type1Tag),
('uid', nfc.tag.tt2.Type2Tag),
('idm', nfc.tag.tt3.Type3Tag),
('uid', nfc.tag.tt4.Type4Tag)
)
def connected(tag):
print tag.type
for uid, type in tgtypes:
if isinstance(tag, type):
print str(attr(tag, uid)).encode("hex")
return
print "error: unknown tag type"
with nfc.ContactlessFrontend('tty:S0:pn532') as clf:
print('waiting for connection')
print(clf)
tag = clf.connect(rdwr={'on-connect': lambda tag: False})
print(str(tag.identifier).encode('hex'))
print(tag.type)
if not tag.ndef:
print('no ndef data')
else:
for record in tag.ndef.records:
print(record)
|