NDEF-MFRC522 (community library)

Summary

Name Value
Name NDEF-MFRC522
Version 2.1.2
Installs 187
License BSD License
Author Aaron Rolleraaron.roller@aawhere.com,Don Coleman don.coleman@gmail.com
Maintainer Aaron Roller aaron.roller@aawhere.com
URL https://github.com/aroller/NDEF-MFRC522/releases/tag/2.1.2
Repository https://github.com/aroller/NDEF-MFRC522.git
Download .tar.gz
All Versions 2.1.2, 2.1.1, 2.1.0, 2.0.0

An Arduino/Particle library for NFC Data Exchange Format (NDEF). Read and write NDEF messages to NFC tags and peers. Supports Mifare Ultralight with MFRC522 RFID board.

Example Build Testing

Device OS Version:

This table is generated from an automated build. Success only indicates that the code compiled successfully.

Library Read Me

This content is provided by the library maintainer and has not been validated or approved.

NDEF Library for Arduino/Particle

Read and Write NDEF messages on Mifare Ultralight NFC Tags with Arduino connected to MFRC522 RFID card.

NFC Data Exchange Format (NDEF) is a common data format that operates across all NFC devices, regardless of the underlying tag or device technology.

Originally forked from NDEF library that exclusively worked with NFC Shield, but adapted to work with the MFRC522 Arduino and MFRC522 Particle and limited to Mifare Ultralight NFC tags.

Supports
  • Reading from Mifare Ultralight tags.
  • Writing to Mifare Ultralight tags.
  • Works on Arduino and Particle (Gen 3 xenon/argon/boron)
Requires

Hello Github

This will write this Github URL to your tag which will allow your NFC-Enabled phone to read the URL and open a browser to this page. See WriteTag.ino

#include <SPI.h>
#include <MFRC522.h>
#include "MifareUltralight.h"

#define SS_PIN 10
#define RST_PIN 6

using namespace ndef_mfrc522;

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

void setup()
{
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin();        // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}

void loop()
{
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent())
return;

// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial())
return;

NdefMessage message = NdefMessage();
String url = String("https://github.com/aroller/NDEF-MFRC522");
message.addUriRecord(url);
MifareUltralight writer = MifareUltralight(mfrc522);
bool success = writer.write(message);
}

Now read the tag using similar main code as above, but with this read excerpt from ReadTag example.

MifareUltralight reader = MifareUltralight(mfrc522);
NfcTag tag = reader.read();
tag.print();

... expect something similar to...

NFC Tag - NFC Forum Type 2
UID 04 0D 89 32 F1 4A 80

NDEF Message 1 record, 44 bytes
NDEF Record
TNF 0x1 Well Known
Type Length 0x1 1
Payload Length 0x28 40
Type 55  U
Payload 00 68 74 74 70 73 3A 2F 2F 67 69 74 68 75 62 2E 63 6F 6D 2F 61 72 6F 6C 6C 65 72 2F 4E 44 45 46 2D 4D 46 52 43 35 32 32  .https://github.com/aroller/NDEF-MFRC522
Record is 44 bytes
  • Type 55 U -> Indicates URL
  • Decode the payload from ASCII and it will spell out your URL
  • See the url written
MifareUltralight

The user interacts with the MifareUltralight to read and write NFC tags using the MFRC522.

Read a message from a tag

MifareUltralight reader = MifareUltralight(mfrc522); NfcTag tag = reader.read();

Write a message to a tag

NdefMessage message = NdefMessage(); MifareUltralight writer = MifareUltralight(mfrc522); bool success = writer.write(message);

Clean a tag. Cleaning resets a tag back to a factory-like state. For Mifare Ultralight, the tag is zeroed and left empty.

MifareUltralight writer = MifareUltralight(mfrc522); bool success = writer.clean();

NfcTag

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.

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.

NdefRecord

A NdefRecord carries a payload and info about the payload within a NdefMessage.

Specifications

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.

Tests
  • Unit tests from original repo work. Load them to arduino and look for success.
Usage
Arduino Usage

The library is not yet published in the Library Manager so you must treat it as a private library.

Particle Usage

The library is published and can easily install, but unfortunately there is a conflict between constants in Arduino.h and spark_wiring_arduino_constants.h which requires a little extra effort.

In file included from ./inc/Arduino.h:27:0,
from .../lib/NDEF-MFRC522/src/Ndef.h:9,
from .../lib/NDEF-MFRC522/src/MifareUltralight.h:4,
from .../src/school-tag-station-particle.ino:6:
../wiring/inc/spark_wiring_arduino_constants.h:152:18: error: conflicting declaration 'typedef uint32_t word'
  • Open your project in Particle Workbench
  • Install NDEF-MFRC522
  • Open spark_wiring_arduino_constants.h in your particle library installed
  • MacOS: /Users/{you}/.particle/toolchains/deviceOS/{version}/firmware-{version}/wiring/inc/
  • comment out typedef uint32_t word;
  • Should compile and install without an error.

Releases

See Releases for the latest.

Steps to release:

  1. Update library.properties with the correct semantic version
  2. Update both in the version and the URL reference for explicit src reference
  3. Merge PR into master
  4. Create a Release named with the version in library.properties
  5. particle library publish to update particle
  6. Arduino users rely on the github repo

Known Issues

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.

  • Read and Write in the same session fails
  • Consider breaking NDEF files (NFC.h/Ndef.h) out from I/O files (MifareUltralight.h)
  • Not all examples are converted to MFRC522 yet.
  • Conflict between Particle and Arduino constants typedef uint32_t word;

Book

Need more info? Check out my book Beginning NFC: Near Field Communication with Arduino, Android, and PhoneGap.

Beginning NFC

License

BSD License (c) 2013-2014, Don Coleman BSD License (c) 2019, Aaron Roller

Browse Library Files