Giter VIP home page Giter VIP logo

ndef's Introduction

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

ndef's People

Contributors

don avatar ccreutzig avatar tigoe avatar gluca avatar xiongyihui avatar ffortat avatar jlkalberer avatar cgiussani avatar millerabel avatar

ndef's Issues

reading type 4 tag broken due to response length limit in inDataExchange()

I'm trying to read a RuuviTag to fetch configuration (ie MAC address)

the tag is recognized, but the code crashes in NfcTag NTAGType4::read() while parsing the NDEF content. The crash happens during the third record, when trying to parse unread content.

The reason is that the isoReadNTAGType4NdefFile method assumes the NDEF message can be read in one go by one inDataExchange() call

however inDataExchange() is limited to below 64 bytes (60 or so) actual return buffer size is limited to 64 bytes including overhead, see the pn532_packetbuffer definition

I see two ways of dealing with the issue:

  • in isoReadNTAGType4NdefFile loop over 60byte chunks until the whole file is read
  • consider implementing Extended Frame support

nfc-tools implemented extended frames

with Adafruit-PN532, there has been an attempt io address the issue:
adafruit/Adafruit-PN532#38 but that never was merged

so for a start the chunked read variant looks easier to do
while at it, it might make sense to factor out a generic file reading method (file id->(buffer, length)

this is what NXP Tag Info has to say about it:

** TagInfo Scan (version 4.28.0) 04-Dez.-23 18:58:39 **
Report Type: -- IC INFO ------------------------------

# IC Manufacturer:
Nordic Semiconductor

# IC Type:
Unknown IC implementing ISO/IEC 14443-4

# NFC Forum NDEF-compliant tag:
Type 4 Tag

# Application information:
Type 4 Tag v2 application present

-- NDEF ------------------------------

# NFC data set information:
NDEF message containing 4 records
Current message size: 129 bytes
Maximum message size: 238 bytes
NFC data set access: Read & Write

# Record #1: Text record:
Type Name Format: NFC Forum well-known type
Short Record
type: "T"
encoding: UTF-8
lang: "id"
text: "ID: C5:5A:B4:FC:52:5C:F4:B2"
Payload length: 30 bytes
Payload data:

[00] 02 69 64 49 44 3A 20 43 35 3A 35 41 3A 42 34 3A |.idID: C5:5A:B4:|

[10] 46 43 3A 35 32 3A 35 43 3A 46 34 3A 42 32       |FC:52:5C:F4:B2  |


# Record #2: Text record:
Type Name Format: NFC Forum well-known type
Short Record
type: "T"
encoding: UTF-8
lang: "ad"
text: "MAC: C2:6E:D1:70:2B:44"
Payload length: 25 bytes
Payload data:

[00] 02 61 64 4D 41 43 3A 20 43 32 3A 36 45 3A 44 31 |.adMAC: C2:6E:D1|

[10] 3A 37 30 3A 32 42 3A 34 34                      |:70:2B:44       |


# Record #3: Text record:
Type Name Format: NFC Forum well-known type
Short Record
type: "T"
encoding: UTF-8
lang: "sw"
text: "SW: Ruuvi FW v3.31.1+default"
Payload length: 31 bytes
Payload data:

[00] 02 73 77 53 57 3A 20 52 75 75 76 69 20 46 57 20 |.swSW: Ruuvi FW |

[10] 76 33 2E 33 31 2E 31 2B 64 65 66 61 75 6C 74    |v3.31.1+default |


# Record #4: Text record:
Type Name Format: NFC Forum well-known type
Short Record
type: "T"
encoding: UTF-8
lang: "dt"
text: "��`A��������$}V����n�p+D"
Payload length: 27 bytes
Payload data:

[00] 02 64 74 05 16 60 41 87 FF FF 03 10 FE 14 02 24 |.dt..`A........$|

[10] 7D 56 1A 00 0B C2 6E D1 70 2B 44                |}V....n.p+D     |


# NDEF message:
[00] 91 01 1E 54 02 69 64 49 44 3A 20 43 35 3A 35 41 |...T.idID: C5:5A|

[10] 3A 42 34 3A 46 43 3A 35 32 3A 35 43 3A 46 34 3A |:B4:FC:52:5C:F4:|

[20] 42 32 11 01 19 54 02 61 64 4D 41 43 3A 20 43 32 |B2...T.adMAC: C2|

[30] 3A 36 45 3A 44 31 3A 37 30 3A 32 42 3A 34 34 11 |:6E:D1:70:2B:44.|

[40] 01 1F 54 02 73 77 53 57 3A 20 52 75 75 76 69 20 |..T.swSW: Ruuvi |

[50] 46 57 20 76 33 2E 33 31 2E 31 2B 64 65 66 61 75 |FW v3.31.1+defau|

[60] 6C 74 51 01 1B 54 02 64 74 05 16 60 41 87 FF FF |ltQ..T.dt..`A...|

[70] 03 10 FE 14 02 24 7D 56 1A 00 0B C2 6E D1 70 2B |.....$}V....n.p+|

[80] 44                                              |D               |


# Capability Container (CC) file content:
Mapping version 2.0
CC length: 15 bytes
Maximum Le value: 248 bytes
Maximum Lc value: 248 bytes
NDEF File Control TLV:
* Length: 6 bytes
* NDEF file ID: 0xE104
* Maximum NDEF data size: 240 bytes
* NDEF access: Read & Write
[0] 00 0F 20 00 F8 00 F8 04 06 E1 04 00 F0 00 00    |.. ............ |


# Type 4 Tag File Control Information (FCI):
Type 4 Tag v2 application FCI: [none]
CC file FCI: [none]
NDEF file FCI: [none]

# NDEF file contents:
[00] 00 8D 81 01 00 00 00 1E 54 02 69 64 49 44 3A 20 |........T.idID: |

[10] 43 35 3A 35 41 3A 42 34 3A 46 43 3A 35 32 3A 35 |C5:5A:B4:FC:52:5|

[20] 43 3A 46 34 3A 42 32 01 01 00 00 00 19 54 02 61 |C:F4:B2......T.a|

[30] 64 4D 41 43 3A 20 43 32 3A 36 45 3A 44 31 3A 37 |dMAC: C2:6E:D1:7|

[40] 30 3A 32 42 3A 34 34 01 01 00 00 00 1F 54 02 73 |0:2B:44......T.s|

[50] 77 53 57 3A 20 52 75 75 76 69 20 46 57 20 76 33 |wSW: Ruuvi FW v3|

[60] 2E 33 31 2E 31 2B 64 65 66 61 75 6C 74 41 01 00 |.31.1+defaultA..|

[70] 00 00 1B 54 02 64 74 05 16 60 41 87 FF FF 03 10 |...T.dt..`A.....|

[80] FE 14 02 24 7D 56 1A 00 0B C2 6E D1 70 2B 44    |...$}V....n.p+D |


-- EXTRA ------------------------------

# TagInfo Version:
Version :4.28.0

# Device Info:
Device Model :samsung ( SM-A136B )
Android OS Version :13

-- FULL SCAN ------------------------------

--------------------------------------

````

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.