NFC MODULE V3 Raspberry pi



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-PI.png

Figure 1: 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.

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.

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.

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.

autoreconf -vis
./configure --with-drivers=pn532_uart --sysconfdir=/etc --prefix=/usr
sudo make clean && sudo make install all

Install the python library

pip install -U nfcpy

To check everything is working try running the command below to make sure your reader is detected and listed.

nfc-scan-device

Now create your first python program to make sure everything is working.

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)

Comments