• Stars
    star
    282
  • Rank 146,549 (Top 3 %)
  • Language
    C++
  • License
    Other
  • Created almost 12 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

NDEF Library for Arduino. Read and Write NDEF Messages to NFC tags with Arduino.

NDEF Library for Arduino

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 Adafruit NFC Shield, Seeed Studio NFC Shield v2.0 and the Seeed Studio NFC Shield. The library supports I2C for the Adafruit shield and SPI with the Seeed shields. The Adafruit Shield can also be modified to use SPI. It should also work with the Adafruit NFC Breakout Board.

Supports

  • 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.
  • Peer to Peer with the Seeed Studio shield

Requires

Yihui Xiong's PN532 Library

Getting Started

To use the Ndef library in your code, include the following in your sketch

For the Adafruit Shield using I2C

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

For the Seeed Shield using SPI

#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_SPI pn532spi(SPI, 10);
NfcAdapter nfc = NfcAdapter(pn532spi);

NfcAdapter

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();
}

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.

Peer to Peer

Peer to Peer is provided by the LLCP and SNEP support in the Seeed Studio library. P2P requires SPI and has only been tested with the Seeed Studio shield. Peer to Peer was tested between Arduino and Android or BlackBerry 10. (Unfortunately Windows Phone 8 did not work.) See P2P_Send and P2P_Receive for more info.

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

To run the tests, you'll need ArduinoUnit. To "install", I clone the repo to my home directory and symlink the source into ~/Documents/Arduino/libraries/ArduinoUnit.

$ cd ~
$ git clone [email protected]:mmurdoch/arduinounit.git
$ cd ~/Documents/Arduino/libraries/
$ ln -s ~/arduinounit/src ArduinoUnit

Tests can be run on an Uno without a NFC shield, since the NDEF logic is what is being tested.

Warning

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.

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

More Repositories

1

BluetoothSerial

Cordova (PhoneGap) Plugin for Serial Communication over Bluetooth
Objective-C
1,069
star
2

cordova-plugin-ble-central

Bluetooth Low Energy (BLE) Central plugin for Apache Cordova (aka PhoneGap)
Java
950
star
3

node-eddystone-beacon

Create an Eddystone Beacon using Node.js https://github.com/google/eddystone
JavaScript
315
star
4

cordova-plugin-hello

Sample Hello World Cordova Plugin
Objective-C
192
star
5

ionic-ble-examples

Ionic Framework Bluetooth Low Energy Examples
TypeScript
102
star
6

phonegap-nfc-reader

PhoneGap NFC reader demo
JavaScript
101
star
7

cordova-filechooser

Cordova Plugin that supplies a File Chooser
Java
96
star
8

ionic-nfc-reader

NFC Reader using Cordova, Ionic Framework and phonegap-nfc plugin
JavaScript
78
star
9

KeyboardToolbarRemover

Cordova Plugin to remove the Previous, Next and Done buttons from above the iOS Keyboard
Objective-C
57
star
10

cordova-plugin-rfduino

Cordova (PhoneGap) plugin for RFduino
Objective-C
46
star
11

ionic-ble

JavaScript
34
star
12

html-cam

Use Device API and File API to take and display a photo using the Android browser
JavaScript
30
star
13

cordova-plugin-ble-peripheral

Apache Cordova plugin for implementing BLE (Bluetooth Low Energy) peripherals.
Java
30
star
14

cordova-plugin-hce

Cordova plugin for Host Card Emulation (HCE)
Java
28
star
15

phonegap-p2p

PhoneGap NFC peer to peer demo
JavaScript
22
star
16

ndef-js

Library to create and parse NDEF messages
JavaScript
21
star
17

mifare-classic-js

Read and write NDEF bytes to Mifare Classic Tags
JavaScript
21
star
18

Arduino101BluetoothMIDI

Bluetooth MIDI controller built with the Arduino 101
Arduino
16
star
19

ITP-BluetoothLE

Code for ITP Bluetooth LE class at NYU
JavaScript
15
star
20

ble-background

Use cordova-plugin-ble-central to scan, connect, & receive notifications while app is in background
JavaScript
14
star
21

locus

Parser for LOCUS log files
Python
14
star
22

ITP-DeviceToDatabase

C++
13
star
23

phonegap-nfc-writer

PhoneGap NFC writer demo
JavaScript
13
star
24

cordova-hce-demo

Demo project for Cordova Host Card Emulation (HCE) plugin
JavaScript
11
star
25

rockpaperscissors

PhoneGap NFC demo - writes peer to peer and reads tags
JavaScript
10
star
26

sencha-exercise

JavaScript
10
star
27

rfduino-js

JavaScript
9
star
28

BluetoothLock

Arduino
9
star
29

MKR1000-Azure

MKR1000 with DHT22 to Azure
Arduino
7
star
30

ninja-thing

Send temperature and humidity data from Ninja Blocks to The Thing System via TSRP
JavaScript
7
star
31

nfc-launch

Demo - launch a Cordova app when scanning a NFC tag
Java
7
star
32

phonegap-nfc-ios

JavaScript
5
star
33

iotdevfest

Arduino Workshop for IoT DevFest www.iotdevfest.com
C++
5
star
34

slides

JavaScript
5
star
35

sencha-datastore

Simple Sencha Touch Data Store Example
JavaScript
5
star
36

nfc-acr122

Java
5
star
37

cordova-bluetooth-midi

JavaScript
5
star
38

apachecon-nfc-demos

Java
3
star
39

rfduino-logreader

JavaScript
3
star
40

ArduinoByteStream

Read-only Stream from a byte array for Ardunio
Arduino
3
star
41

phonegap-ble-workshop

PhoneGap Day 2016 & 2017 Hands on Bluetooth Low Energy Wokshop
JavaScript
3
star
42

mfba2016-arduino-ble

Maker Faire Bay Area 2016 - Creating Bluetooth Low Energy peripherals with Arduino http://don.github.io/slides/2016-05-21-arduino-ble
Arduino
3
star
43

ble-scan

Simple demo using cordova-plugin-ble-central to scan for Bluetooth low energy peripherals
HTML
3
star
44

mfny2016-arduino-ble

Arduino
2
star
45

respeaker-lights

Python
2
star
46

sencha-virtual-model-field

Sencha Touch Virtual Model Field Example
JavaScript
2
star
47

mfny2015-rpi-ble

Examples from Maker Faire 2015 Raspberry PI BLE presentation
JavaScript
2
star
48

itp-conndev

JavaScript
2
star
49

ninja-nfc

NFC driver for Ninja Blocks
JavaScript
2
star
50

cocoaheadsnyc

Sample code for 2016-06-09 CocoaHeadsNYC presentation
Objective-C
1
star
51

nfc-test

JavaScript
1
star
52

mfba2016-wireless-sensors

Maker Faire Bay Area 2016 - Building Wireless Sensors http://don.github.io/slides/2016-05-22-wireless-sensors
HTML
1
star
53

phonegap-nfc-issue-247

JavaScript
1
star
54

techcast-mobile

Experimenting with JQuery Mobile
Ruby
1
star
55

solidcon-hands-on-ble

JavaScript
1
star
56

nfc-share

Test project for phonegap-nfc share
CSS
1
star
57

ble-issue196

JavaScript
1
star
58

BluetoothSerialApiDev

JavaScript
1
star
59

sencha-drilldown

iPhone drill down example using Sencha Touch
JavaScript
1
star
60

incubator-cordova-tizen

Mirror of Apache Cordova Tizen
JavaScript
1
star
61

pg-beam

android beam phonegap experiment
JavaScript
1
star
62

chariot-iot-workshop

Chariot Solutions IoT Workshop Philly Tech Week 2019
C++
1
star
63

mfny2018-lora

Sample code from Maker Faire NY 2018 presentation https://makerfaire.com/maker/entry/68112/
C++
1
star
64

sencha-localstore

Sencha Touch Local Storage Example
JavaScript
1
star
65

sencha-xpn

JavaScript
1
star
66

weinre-offline

1
star
67

MemoryFree

Arduino Available Memory http://playground.arduino.cc/Code/AvailableMemory
C++
1
star
68

many-ble-notifications

JavaScript
1
star
69

iot-workshop

JavaScript
1
star
70

ibeacon-demo

iBeacon Scanner demo
Objective-C
1
star
71

phonegap-nfc-issue-190

nfc test code
CSS
1
star
72

phonegap-nfc-issue144

JavaScript
1
star
73

phonegap-plugins

Slides for PhoneGap Day 2012 presentation
JavaScript
1
star
74

cordova-filetransfer

Server for Cordova FileTransfer Jasmine tests
JavaScript
1
star