Libnfc:Arduino

From NFC Tools
(Difference between revisions)
Jump to: navigation, search
(Libnfc)
m (Prepare Libnfc)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
'''How to use a PN532 board with Arduino to make a reader compatible with libnfc'''
 
'''How to use a PN532 board with Arduino to make a reader compatible with libnfc'''
  
=Libnfc=
+
=Prepare Libnfc=
 
Download libnfc 1.7.1
 
Download libnfc 1.7.1
 
{{#fileanchor: libnfc_arduino.sh}}
 
{{#fileanchor: libnfc_arduino.sh}}
 
<source lang=bash>
 
<source lang=bash>
git clone https://code.google.com/p/libnfc/
+
git clone https://github.com/nfc-tools/libnfc.git
 
cd libnfc
 
cd libnfc
 
git checkout libnfc-1.7.1
 
git checkout libnfc-1.7.1
Line 33: Line 33:
 
{{#fileanchor: libnfc_arduino.sh}}
 
{{#fileanchor: libnfc_arduino.sh}}
 
<source lang=bash>
 
<source lang=bash>
wget "http://nfc-tools.org/index.php?title=Libnfc:Arduino&action=raw&anchor=pn532_uart.patch"
+
wget --content-disposition "http://nfc-tools.org/index.php?title=Libnfc:Arduino&action=raw&anchor=pn532_uart.patch"
 
</source>
 
</source>
 
Then apply it:
 
Then apply it:
Line 43: Line 43:
 
{{#fileanchor: libnfc_arduino.sh}}
 
{{#fileanchor: libnfc_arduino.sh}}
 
<source lang=bash>
 
<source lang=bash>
 +
autoreconf -vis
 
./configure --with-drivers=pn532_uart --sysconfdir=/etc --prefix=/usr
 
./configure --with-drivers=pn532_uart --sysconfdir=/etc --prefix=/usr
 
make clean all
 
make clean all
Line 52: Line 53:
 
</source>
 
</source>
 
(if libnfc was installed before, it should be deleted before installing it)
 
(if libnfc was installed before, it should be deleted before installing it)
==TODO ONGOING==
 
Above steps are available in [{{#filelink: libnfc_arduino.sh}} this script libnfc_arduino.sh]
 
==set config==
 
(maybe sudo)
 
  
mkdir /etc/nfc
+
Set configuration by creating /etc/nfc/[{{#file: libnfc.conf}} libnfc.conf] as
mkdir -p /etc/nfc/devices.d
+
<source lang=ini>
touch /etc/nfc/libnfc.conf
+
# Allow device auto-detection (default: true)
 +
# Note: if this auto-detection is disabled, user has to set manually a device
 +
# configuration using file or environment variable
 +
allow_autoscan = false
  
and '''/etc/nfc/libnfc.conf''' should look like this:
+
# Allow intrusive auto-detection (default: false)
 +
# Warning: intrusive auto-detection can seriously disturb other devices
 +
# This option is not recommended, user should prefer to add manually his device.
 +
allow_intrusive_scan = false
  
# Allow device auto-detection (default: true)
+
# Set log level (default: error)
# Note: if this auto-detection is disabled, user has to set manually a device
+
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# configuration using file or environment variable
+
# Note: if you compiled with --enable-debug option, the default log level is "debug"
allow_autoscan = false
+
log_level = 2
+
# Allow intrusive auto-detection (default: false)
+
# Warning: intrusive auto-detection can seriously disturb other devices
+
# This option is not recommended, user should prefer to add manually his device.
+
allow_intrusive_scan = false
+
+
# Set log level (default: error)
+
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
+
# Note: if you compiled with --enable-debug option, the default log level is "debug"
+
log_level = 2
+
+
# Manually set default device (no default)
+
# To set a default device, you must set both name and connstring for your device
+
# Note: if autoscan is enabled, default device will be the first device available in device list.
+
device.name = "PN532 NFC Board on Arduino"
+
device.connstring = "pn532_uart:/dev/ttyACM0:230400"
+
  
(maybe '''/dev/ttyACM0''' is not your arduino port)
+
# Manually set default device (no default)
 +
# To set a default device, you must set both name and connstring for your device
 +
# Note: if autoscan is enabled, default device will be the first device available in device list.
 +
device.name = "PN532 NFC Board on Arduino"
 +
device.connstring = "pn532_uart:/dev/ttyACM0:230400"
 +
</source>
  
<br>
+
{{#fileanchor: libnfc_arduino.sh}}
 +
<source lang=bash>
 +
sudo mkdir -p /etc/nfc/devices.d
 +
cd /etc/nfc/
 +
wget --content-disposition "http://nfc-tools.org/index.php?title=Libnfc:Arduino&action=raw&anchor=libnfc.conf"
 +
</source>
  
=Arduino=
+
All above steps are available in [{{#filelink: libnfc_arduino.sh}} this script libnfc_arduino.sh]
==download==
+
(we only need the arduino files)
+
  
svn checkout http://mfocuino.googlecode.com/svn/trunk/nfcreader/arduino/ uartnfc
+
Check if '''/dev/ttyACM0''' is well your Arduino port, else adapt this script.
  
 +
=Prepare Arduino=
 +
Download:
 +
<source lang=bash>
 +
svn checkout http://mfocuino.googlecode.com/svn/trunk/nfcreader/arduino/ uartnfc
 +
cd uartnfc
 +
</source>
 
now you have the lib and the sketch for your arduino :)
 
now you have the lib and the sketch for your arduino :)
  
<br>
+
Change baudrate in the '''uartnfc.ino''' file to 230400:
==change baudrate==
+
<source lang=c>
in the '''uartnfc.ino''' file:
+
void setup(void)
 
+
{
void setup(void)
+
  Serial.begin(230400); //115200, 230400(, 460800, 921600)
{
+
  Serial.flush();
  Serial.begin('''921600'''); //460800, 115200
+
  nfc.begin();
  Serial.flush();
+
}
  nfc.begin();
+
</source>
}
+
 
+
change to:
+
 
+
void setup(void)
+
{
+
  Serial.begin('''230400'''); //115200, 230400(, 460800, 921600)
+
  Serial.flush();
+
  nfc.begin();
+
}
+
  
<br>
+
Upload the sketch to the Arduino, e.g. via the Arduino IDE.
==upload==
+
I used the Arduino IDE.
+
  
<br>
+
=Prepare hardware=
==pins==
+
Check if your PN532 board needs some soldering to activate the SPI, then wire it to the Arduino as following:
maybe some solder work should be done to use SPI, if not this should work:
+
  
 
   Arduino      PN532 board (SPI)
 
   Arduino      PN532 board (SPI)
Line 132: Line 120:
 
   3,3V    <-->  3,3V (VCC)
 
   3,3V    <-->  3,3V (VCC)
  
<br>
 
 
=Test=
 
=Test=
  
Line 144: Line 131:
 
       SAK (SEL_RES): 08   
 
       SAK (SEL_RES): 08   
  
<br>
 
 
=Links=
 
=Links=
  
http://code.google.com/p/mfocuino/<br>
+
* http://code.google.com/p/mfocuino/
http://www.libnfc.org/community/topic/1169/how-to-connect-a-pn532-nfc-modul-to-an-arduino-to-use-libnfc-with-it/<br>
+
* http://www.libnfc.org/community/topic/1169/how-to-connect-a-pn532-nfc-modul-to-an-arduino-to-use-libnfc-with-it/
http://regnerischernachmittag.wordpress.com/2013/05/13/libnfc-mit-arduino-nfc-shield-nutzen/ (german)<br>
+
* http://regnerischernachmittag.wordpress.com/2013/05/13/libnfc-mit-arduino-nfc-shield-nutzen/ (german)
http://robospatula.blogspot.de/2014/01/configure-install-libnfc-linux-PN532-breakout-board.html<br>
+
* http://robospatula.blogspot.de/2014/01/configure-install-libnfc-linux-PN532-breakout-board.html
http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=90_93&products_id=2205<br>
+
* http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=90_93&products_id=2205
  
 
[[Category:Libnfc]]
 
[[Category:Libnfc]]
 
[[Category:Libnfc:Examples]]
 
[[Category:Libnfc:Examples]]

Latest revision as of 09:24, 6 January 2017

How to use a PN532 board with Arduino to make a reader compatible with libnfc

Contents

[edit] Prepare Libnfc

Download libnfc 1.7.1

git clone https://github.com/nfc-tools/libnfc.git
cd libnfc
git checkout libnfc-1.7.1

Download this patch:

--- libnfc/drivers/pn532_uart.c	(unpatched)
+++ libnfc/drivers/pn532_uart.c	(patched)
@@ -48,7 +48,7 @@
 #include "chips/pn53x-internal.h"
 #include "uart.h"
 
-#define PN532_UART_DEFAULT_SPEED 115200
+#define PN532_UART_DEFAULT_SPEED 230400
 #define PN532_UART_DRIVER_NAME "pn532_uart"
 
 #define LOG_CATEGORY "libnfc.driver.pn532_uart"
@@ -306,6 +306,7 @@ pn532_uart_wakeup(nfc_device *pnd)
   const uint8_t pn532_wakeup_preamble[] = { 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
   int res = uart_send(DRIVER_DATA(pnd)->port, pn532_wakeup_preamble, sizeof(pn532_wakeup_preamble), 0);
   CHIP_DATA(pnd)->power_mode = NORMAL; // PN532 should now be awake
+  sleep(4);
   return res;
 }

This can be done with

wget --content-disposition "http://nfc-tools.org/index.php?title=Libnfc:Arduino&action=raw&anchor=pn532_uart.patch"

Then apply it:

patch -p0 < pn532_uart.patch

Compile with pn532_uart driver (or all drivers if you remove "--with-drivers=..."):

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

and install it on your system:

sudo make install

(if libnfc was installed before, it should be deleted before installing it)

Set configuration by creating /etc/nfc/libnfc.conf as

# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
# configuration using file or environment variable
allow_autoscan = false
 
# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, user should prefer to add manually his device.
allow_intrusive_scan = false
 
# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
log_level = 2
 
# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "PN532 NFC Board on Arduino"
device.connstring = "pn532_uart:/dev/ttyACM0:230400"


sudo mkdir -p /etc/nfc/devices.d
cd /etc/nfc/
wget --content-disposition "http://nfc-tools.org/index.php?title=Libnfc:Arduino&action=raw&anchor=libnfc.conf"

All above steps are available in this script libnfc_arduino.sh

Check if /dev/ttyACM0 is well your Arduino port, else adapt this script.

[edit] Prepare Arduino

Download:

svn checkout http://mfocuino.googlecode.com/svn/trunk/nfcreader/arduino/ uartnfc
cd uartnfc

now you have the lib and the sketch for your arduino :)

Change baudrate in the uartnfc.ino file to 230400:

void setup(void)
{
  Serial.begin(230400); //115200, 230400(, 460800, 921600)
  Serial.flush();
  nfc.begin();
}

Upload the sketch to the Arduino, e.g. via the Arduino IDE.

[edit] Prepare hardware

Check if your PN532 board needs some soldering to activate the SPI, then wire it to the Arduino as following:

 Arduino       PN532 board (SPI)
 Pin 13  <-->  SCK
 Pin 12  <-->  MISO
 Pin 11  <-->  MOSI (/SDA/TX)
 Pin 10  <-->  SS   (/NSS/SCL/RX)
 GND     <-->  GND
 3,3V    <-->  3,3V (VCC)

[edit] Test

root@kali:~# nfc-list
nfc-list uses libnfc libnfc-1.7.0-91-g491db20
NFC device: pn532_uart:/dev/ttyACM0 opened
1 ISO14443A passive target(s) found:
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04  
       UID (NFCID1): a5  09  fd  e5  
      SAK (SEL_RES): 08  

[edit] Links

Personal tools
Namespaces

Variants
Actions
Navigation
Documentation
Misc
Toolbox
Partners