Read and Write NDEF messages on NFC Tags with Arduino.
NFC Data Exchange Format (NDEF) is a common data format that operates across all NFC devices, regardless of the underlying tag or device technology.
This code works with the cheap MFRC522 tag reader.
- Reading from Mifare Classic Tags with 4 byte UIDs.
- Writing to Mifare Classic Tags with 4 byte UIDs.
- Reading from Mifare Ultralight tags.
- Writing to Mifare Ultralight tags.
To use the Ndef library in your code, include the following in your sketch:
#include <SPI.h>
#include <MFRC522.h>
#include <NfcAdapter.h>
MFRC522 mfrc522(CS_PIN, UINT8_MAX);
NfcAdapter ndef = NfcAdapter(&mfrc522);
void setup() {
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
nfc.begin();
}
The user interacts with the NfcAdapter to read and write NFC tags using the NFC shield.
Read a message from a tag
if (nfc.tagPresent()) {
NfcTag tag = nfc.read();
tag.print();
}
Write a message to a tag
if (nfc.tagPresent()) {
NdefMessage message = NdefMessage();
message.addTextRecord("Hello, Arduino!");
success = nfc.write(message);
}
Erase a tag. Tags are erased by writing an empty NDEF message. Tags are not zeroed out the old data may still be read off a tag using an application like NXP's TagInfo.
if (nfc.tagPresent()) {
success = nfc.erase();
}
Format a Mifare Classic tag as NDEF.
if (nfc.tagPresent()) {
success = nfc.format();
}
Clean a tag. Cleaning resets a tag back to a factory-like state. For Mifare Classic, tag is zeroed and reformatted as Mifare Classic (non-NDEF). For Mifare Ultralight, the tag is zeroed and left empty.
if (nfc.tagPresent()) {
success = nfc.clean();
}
Reading a tag with the shield, returns a NfcTag object. The NfcTag object contains meta data about the tag UID, technology, size. When an NDEF tag is read, the NfcTag object contains a NdefMessage.
A NdefMessage consist of one or more NdefRecords.
The NdefMessage object has helper methods for adding records.
ndefMessage.addTextRecord("hello, world");
ndefMessage.addUriRecord("http://arduino.cc");
The NdefMessage object is responsible for encoding NdefMessage into bytes so it can be written to a tag. The NdefMessage also decodes bytes read from a tag back into a NdefMessage object.
A NdefRecord carries a payload and info about the payload within a NdefMessage.
This code is based on the "NFC Data Exchange Format (NDEF) Technical Specification" and the "Record Type Definition Technical Specifications" that can be downloaded from the NFC Forum.
This software is in development. It works for the happy path. Error handling could use improvement. It runs out of memory, especially on the Uno board. Use small messages with the Uno. The Due board can write larger messages. Please submit patches.
Need more info? Check out my book Beginning NFC: Near Field Communication with Arduino, Android, and PhoneGap.
BSD License (c) 2013-2014, Don Coleman