Rapid prototyping with NFC

We make an effort to bring NFC technology to you. For Arduino, we provide PN532 library to read/write nfc tags, emulate NFC type 4 tags and communicate with android 4.0+. Now, the NFC library is ready for mbed platform. Here is a hardware list and programs for you to get started.

Hardware

  • NFC Shield
  • Mifare Classic tag
  • Seeeduino Arch or Arch Pro
  • Android powered with NFC

Programs

  • Read a NFC tag

Import programPN532_read_tag

PN532_read_tag: b238c6772bc4 main.cpp

view main.cpp @ 2:b238c6772bc4

read a NFC tag
author yihui
date Thu, 21 Nov 2013 07:11:15 +0000
parents a9df716f8a64
children
line wrap: on
line source
#include "mbed.h"
#include "PN532_SPI.h"
#include "NfcAdapter.h"

#if 0                           // Using Seeeduino Arch
#define LOG(args...)
SPI spi(P1_22, P1_21, P1_20);  // SPI(mosi, miso, clk)
PN532_SPI pn532spi(spi, P0_2);

#else                           // Using Arch Pro
#define LOG(args...)        pc.printf(args)
Serial pc(USBTX, USBRX);
SPI spi(P0_18, P0_17, P0_15);
PN532_SPI pn532spi(spi, P0_6);
#endif

NfcAdapter nfc(pn532spi);
DigitalOut led(LED1);


int main() {
    LOG("NDEF Reader\n");
    nfc.begin();
    while (1) {
        LOG("\nScan a NFC tag\n");
        if (nfc.tagPresent()) {
            NfcTag tag = nfc.read();        // tag.print() doesn't work yet
            if (tag.hasNdefMessage()) {
                LOG("Get a NDEF message.");
                NdefMessage message = tag.getNdefMessage();
            }
        }
        wait(5);
    }
}
  • Write a NFC tag

Import programPN532_write_tag

PN532_write_tag: 59afaef9fb24 main.cpp

view main.cpp @ 3:59afaef9fb24

remove USBDevice lib to reduce memory usage
author yihui
date Thu, 21 Nov 2013 06:52:36 +0000
parents 56402cae2c3c
children
line wrap: on
line source
#include "mbed.h"
#include "PN532_SPI.h"
#include "NfcAdapter.h"

#if 1                           // Using Seeeduino Arch
#define LOG(args...)
SPI spi(P1_22, P1_21, P1_20);  // SPI(mosi, miso, clk)
PN532_SPI pn532spi(spi, P0_2);

#else                           // Using Arch Pro
#define LOG(args...)        pc.printf(args)
Serial pc(USBTX, USBRX);
SPI spi(P0_18, P0_17, P0_15);
PN532_SPI pn532spi(spi, P0_6);
#endif

NfcAdapter nfc(pn532spi);
DigitalOut led1(LED1);
DigitalOut led2(LED2);

int main() {
    led1 = 1;
    LOG("NFC Writer\n");
    nfc.begin();
    while (1) {
        LOG("\nPlace a formatted Mifare Classic NFC tag on the reader.");
        if (nfc.tagPresent()) {
            led2 = 1;
            NdefMessage message = NdefMessage();
            message.addUriRecord("http://seeedstudio.com");
            bool success = nfc.write(message);
            if (success) {
                LOG("Success. Try reading this tag with your phone.");        
            } else {
                LOG("Write failed.");
            }
            led2 = 0;
        }
        wait(5);
    }
}
  • Communicate with Android

Import programPN532_P2P

PN532_P2P: f228fdc41598 main.cpp

view main.cpp @ 1:f228fdc41598

communicate with android 4.0+
author yihui
date Thu, 21 Nov 2013 08:27:37 +0000
parents 21ac4fe1ccf8
children
line wrap: on
line source
#include "mbed.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"

#if 1                           // Using Seeeduino Arch
#define LOG(args...)
SPI spi(P1_22, P1_21, P1_20);  // SPI(mosi, miso, clk)
PN532_SPI pn532spi(spi, P0_2);

#else                           // Using Arch Pro
#define LOG(args...)        pc.printf(args)
Serial pc(USBTX, USBRX);
SPI spi(P0_18, P0_17, P0_15);
PN532_SPI pn532spi(spi, P0_6);
#endif

DigitalOut led(LED1);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];

int main()
{
    while (1) {
#if 1
        LOG("Send a message to Android");
        NdefMessage message = NdefMessage();
        message.addUriRecord("http://www.seeedstudio.com");
        int messageSize = message.getEncodedSize();
        if (messageSize > sizeof(ndefBuf)) {
            LOG("ndefBuf is too small");
            while (1) {
            }
        } 
        message.encode(ndefBuf);
        if (0 >= nfc.write(ndefBuf, messageSize)) {
            LOG("Failed\n");
        } else {
            LOG("Success\n");
        }
#else
        LOG("Get a message from Android");
        int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
        if (msgSize > 0) {
            NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
            msg.print();
            LOG("\nSuccess");
        } else {
            LOG("failed");
        }
#endif
        wait(5);
    }
}

Library

This library is ported from https://github.com/Seeed-Studio/PN532.

Import libraryPN532

PN532 NFC library for Seeed Studio's NFC Shield


Report

2 comments on Rapid prototyping with NFC :

15 Nov 2013
24 Mar 2015

Can this library read Uid of ISO14443A card??. I need a example please.

Please log in to post comments.