This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

Posts tagged: libnfc

NFC/RFID for Beagleboard xm with Java

NFC reader   Based on my experience with JavaFX, Beagleboard xm, RxTx and GSM devices, I started my new research project: A simple and cheap time tracking solution.

There are hundreds of tools and hardware solutions available for time tracking, but hardware solutions are expensive and tools without hardware are not practically.

The ideal solution would use a Raspberry Pi and a RFID reader together with a simple JavaFX application. Total costs are about EUR 50,- for the hardware and software should be free :) If you need a case for your hardware and a lcd display, it would be more expensive but EUR 150,- should be the limit for a brilliant system.

I ordered my Raspberry Pi some weeks ago but it is still not available and hopefully it will arrive before Xmas? My fallback board is the Beagleboard xm.

The big question was the RFID reader/writer. Wich devices were supported, and were there any Java APIs for the communication? After a short google search I found a lot of recommendations for RFID breakout board, but it was not too cheap and I don't have an Arduino board and don't want one. I found some other boards but I decided to use PN532 NFC RFID module kit. It is still expensive but seems to be fair.

Why this RFID board?

Firstly, because of a mistake. I saw a screenshot of an example application and the frame icon was a Java icon... So I thought the application was written with Java and there must be a Java API. Not really!
The whole kit was available as developer version with a lot of accessoires like two cards, one dongle, additional cables, ....

After reading the documentation and provided information it was clear that the board was perfect for Arduino boards and the software was written in C/C++, but it was still not too expensive.

But no risk, no fun - ordered.

The delivery time was ok and after unpacking I thought it would be easy to test the included RFID cards - Plug and Play? The truth: No way.

First problem

Included cables were built for Arduino board. I kew that the connection could be a problem and ordered an USB TTL module together with the RFID baord.

The USB module has a connector with 4 pins (GND, VCC, RX, TX) and the RFID board has 4 pins (GND, VDD, RX/SDA, TX/SDL). It was not a big problem to connect the right pins after it was clear that the included standard cable will not work.

Solved.

Second problem

The RFID reader supports 3 different modes (HSU, I2C, SPI). The default mode was I2C because of the Arduino support. But in order to support my Beagleboard, HSU was needed. The board has no jumpers and you must solder a little bit to, but with the right tools it's easy.

Solved.

Third problem

No Arduino board, no chance to test the RFID board? Not really.
There is a great open source project with the name libnfc. It has read/write and diagnostic tools and runs on Linux and should run on Windows too. I compiled libnfc on my Ubuntu VM and had the problem that the nfc-list tool reported a timeout after connecting - but the RFID board was found.
Hm...

It was easy to compile libnfc on Ubuntu - after installing some dependencies - so I thought it should be easy to compile it on my Beagleboard xm? It was a bad idea and I spent some extra hours... but it worked.

How? I can't remember every single step, but try following:

Dependencies:

opkg install cc
opkg install gcc-dev
opkg install task-native-sdk
opkg install libusb-1.0-0 libusb-1.0-dev
opkg install libusb-0.1-4 libusb-0.1-dev
opkg install udev-dev
opkg install autoconf

You need following source packages:

pcsc-lite.1.7.0
libnfc-1.5.1

(Newer versions or different version combinations did not work because libraries were not available for my distribution. But this versions are good enough for tests.)

Start with pcsclite:

./bootstrap
./configure --disable-libhal
make install

Copy /usr/local/include/PCSC directory to /usr/include and /usr/local/lib/pkgconfig/libpcsclite.pc to /usr/lib/pkconfig/

Next, compile libnfc:

./configure --with-drivers=pn532_uart,pn53x_usb --enable-serial-autoprobe --enable-debug
make install

If you get the exception similar to ln: symbol not found -lusb, create a symbolic link:

cd /lib
ln -s libusb-0.1.so.4.4.4 libusb.so

Test, in your libnfc directory:

cd utils
./nfc-list
cd ../examples
# hold a card over the RFID board
./nfc-poll

My output:

# ./nfc-list
Connected to NFC device: PN532 (/dev/ttyUSB0) - PN532 v1.6 (0x07)

# ./nfc-poll
NFC device will poll during 30000 ms (20 pollings of 300 ms for 5 modulations)
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04
       UID (NFCID1): 1b  21  4b  e0
      SAK (SEL_RES): 08

Now you know that your hardware is supported and works with CPP libraries. But what about Java?

I found NFC Tools but the online demo did not work with my reader. Another solution was Open-NFC.

Both solutions were feature rich but horrible because the first one did not compile and maven could not resolve all dependencies. The other solution was too complex for my use case. Why the hell are most libraries so complex. Nobody can handle big libraries.

My use-case was simple: I need a small Java library that can be bundled with my application and it should work on Linux, Windows and optionally on MacOS. It should connect to my Reader via RS232. I stopped evaluating libraries and decided to create my own "simple" library for my use-case.

The topic (NFC, RFID, smart cards) is really complex but I had a PDF that described how read/write basically should work. And I had a reference implementation in cpp. After some problems and night hacking, the problem was solved and I had my library that simply worked (only 2 source files and ~ 1000 LoC).

My first application only reads content from cards that are over the reader. Here is the output:

SAM = true
LOOP PAUSE
UUID length: 4
UUID: 1b 21 4b e0
Authentication receive: 00 FF 03 FD D5 41 00 EA
Authentication successful
Read block successfully:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Read block successfully:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Read block successfully:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Read block successfully:
00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF

And it supports writing to the card, but don't overwrite the 4th block because it contains security settings. If you overwrite the 4th block with wrong values, the whole sector is unusable.

What'll come next? A JavaFX UI for in/out tracking.
The library itself is free but the source is not yet available because it needs some refactoring and beautifying.

Have fun ;-)