Skip to content

Instantly share code, notes, and snippets.

@don
Created January 15, 2013 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save don/4535719 to your computer and use it in GitHub Desktop.
Save don/4535719 to your computer and use it in GitHub Desktop.
Write hello world NDEF message. Cheat by hardcoding bytes.
//
// Write "Hello, world!" in a plain text NDEF message
// Tag must be a Mifare Classic with a 4 byte ID
// Tested with Samsung TecTile
//
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
#define IRQ (2)
#define RESET (3) // Not connected by default on the NFC Shield
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A Card ...");
}
void loop(void) {
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
// Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc.PrintHex(uid, uidLength);
Serial.println("");
if (uidLength == 4)
{
// We probably have a Mifare Classic card ...
Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
// Now we need to try to authenticate it for read/write access
Serial.println("Trying to authenticate block 4 with default KEYA value");
//uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }; // this is the NDEF header key
// TODO check the spec and rename I think this is the A key
uint8_t key[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 }; // this is Sector 1 - 15 key
// Start with block 4 (the first block of sector 1) since sector 0
// contains the manufacturer data and it's probably better just
// to leave it alone unless you know what you're doing
success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, key);
if (success)
{
Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
// output from javascript message builder
// 00 00 03 14 d1 01 10 54 02 65 6e 48 65 6c 6c 6f
// 2c 20 77 6f 72 6c 64 21 fe
// TLV 0x03 with a length of 0x14
// TNF 0xD1 - TNF_WELL_KNOWN, mb=true, me=true, c=false, sr=true, il=false
// Type Length 0x1
// Payload Length 0x10
// RTD_TEXT 0x54
// begin payload
// Text Encoding Length 0x2
// Text Encoding [0x65, 0x6E] "en"
// Message [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21] "Hello, world!"
// End 0xFE
uint8_t sectorbuffer1[16] = {0x00, 0x00, 0x03, 0x14, 0xD1, 0x01, 0x10, 0x54, 0x02, 0x65, 0x6E, 0x48, 0x65, 0x6C, 0x6C, 0x6F};
uint8_t sectorbuffer2[16] = {0x2C, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int write_success = nfc.mifareclassic_WriteDataBlock (4, sectorbuffer1);
if (write_success) {
Serial.println("wrote block 4");
} else {
Serial.println("write failed");
}
write_success = nfc.mifareclassic_WriteDataBlock (5, sectorbuffer2);
if (write_success) {
Serial.println("wrote block 5");
} else {
Serial.println("write failed");
}
Serial.println("Try and read the tag with your phone");
// Wait a bit before writing the card again
delay(2000);
}
else
{
Serial.println("Ooops ... authentication failed: Try another key?");
}
}
if (uidLength == 7)
{
// We probably have a Mifare Ultralight card ...
Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
// Try to read the first general-purpose user page (#4)
Serial.println("Reading page 4");
uint8_t data[32];
success = nfc.mifareultralight_ReadPage (4, data);
if (success)
{
// Data seems to have been read ... spit it out
nfc.PrintHexChar(data, 4);
Serial.println("");
// Wait a bit before reading the card again
delay(1000);
}
else
{
Serial.println("Ooops ... unable to read the requested page!?");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment