mirror of
https://github.com/Vale54321/schafkopf-bot.git
synced 2025-12-16 03:39:34 +01:00
added arduino nfcReader files (#42)
This commit is contained in:
committed by
GitHub
parent
76cb0eaf1a
commit
6259d0bef3
@@ -0,0 +1,115 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
This example will attempt to connect to an FeliCa
|
||||
card or tag and retrieve some basic information about it
|
||||
that can be used to determine what type of card it is.
|
||||
|
||||
Note that you need the baud rate to be 115200 because we need to print
|
||||
out the data and read from the card at the same time!
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
|
||||
*/
|
||||
/**************************************************************************/
|
||||
#include <Arduino.h>
|
||||
|
||||
#if 1
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 0
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_I2C pn532i2c(Wire);
|
||||
PN532 nfc(pn532i2c);
|
||||
#endif
|
||||
|
||||
#include <PN532_debug.h>
|
||||
|
||||
uint8_t _prevIDm[8];
|
||||
unsigned long _prevTime;
|
||||
|
||||
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) {delay(10);}; // 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);
|
||||
|
||||
// Set the max number of retry attempts to read from a card
|
||||
// This prevents us from waiting forever for a card, which is
|
||||
// the default behaviour of the PN532.
|
||||
nfc.setPassiveActivationRetries(0xFF);
|
||||
nfc.SAMConfig();
|
||||
|
||||
memset(_prevIDm, 0, 8);
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
uint8_t ret;
|
||||
uint16_t systemCode = 0xFFFF;
|
||||
uint8_t requestCode = 0x01; // System Code request
|
||||
uint8_t idm[8];
|
||||
uint8_t pmm[8];
|
||||
uint16_t systemCodeResponse;
|
||||
|
||||
// Wait for an FeliCa type cards.
|
||||
// When one is found, some basic information such as IDm, PMm, and System Code are retrieved.
|
||||
Serial.print("Waiting for an FeliCa card... ");
|
||||
ret = nfc.felica_Polling(systemCode, requestCode, idm, pmm, &systemCodeResponse, 5000);
|
||||
|
||||
if (ret != 1)
|
||||
{
|
||||
Serial.println("Could not find a card");
|
||||
delay(500);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( memcmp(idm, _prevIDm, 8) == 0 ) {
|
||||
if ( (millis() - _prevTime) < 3000 ) {
|
||||
Serial.println("Same card");
|
||||
delay(500);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("Found a card!");
|
||||
Serial.print(" IDm: ");
|
||||
nfc.PrintHex(idm, 8);
|
||||
Serial.print(" PMm: ");
|
||||
nfc.PrintHex(pmm, 8);
|
||||
Serial.print(" System Code: ");
|
||||
Serial.print(systemCodeResponse, HEX);
|
||||
Serial.print("\n");
|
||||
|
||||
memcpy(_prevIDm, idm, 8);
|
||||
_prevTime = millis();
|
||||
|
||||
// Wait 1 second before continuing
|
||||
Serial.println("Card access completed!\n");
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
This example will attempt to connect to an FeliCa
|
||||
card or tag and retrieve some basic information about it
|
||||
that can be used to determine what type of card it is.
|
||||
|
||||
Note that you need the baud rate to be 115200 because we need to print
|
||||
out the data and read from the card at the same time!
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
|
||||
*/
|
||||
/**************************************************************************/
|
||||
#include <Arduino.h>
|
||||
|
||||
#if 1
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 0
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_I2C pn532i2c(Wire);
|
||||
PN532 nfc(pn532i2c);
|
||||
#endif
|
||||
|
||||
#include <PN532_debug.h>
|
||||
|
||||
uint8_t _prevIDm[8];
|
||||
unsigned long _prevTime;
|
||||
|
||||
void PrintHex8(const uint8_t d) {
|
||||
Serial.print(" ");
|
||||
Serial.print( (d >> 4) & 0x0F, HEX);
|
||||
Serial.print( d & 0x0F, HEX);
|
||||
}
|
||||
|
||||
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) {delay(10);}; // 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);
|
||||
|
||||
// Set the max number of retry attempts to read from a card
|
||||
// This prevents us from waiting forever for a card, which is
|
||||
// the default behaviour of the PN532.
|
||||
nfc.setPassiveActivationRetries(0xFF);
|
||||
nfc.SAMConfig();
|
||||
|
||||
memset(_prevIDm, 0, 8);
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
uint8_t ret;
|
||||
uint16_t systemCode = 0xFFFF;
|
||||
uint8_t requestCode = 0x01; // System Code request
|
||||
uint8_t idm[8];
|
||||
uint8_t pmm[8];
|
||||
uint16_t systemCodeResponse;
|
||||
|
||||
// Wait for an FeliCa type cards.
|
||||
// When one is found, some basic information such as IDm, PMm, and System Code are retrieved.
|
||||
Serial.print("Waiting for an FeliCa card... ");
|
||||
ret = nfc.felica_Polling(systemCode, requestCode, idm, pmm, &systemCodeResponse, 5000);
|
||||
|
||||
if (ret != 1)
|
||||
{
|
||||
Serial.println("Could not find a card");
|
||||
delay(500);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( memcmp(idm, _prevIDm, 8) == 0 ) {
|
||||
if ( (millis() - _prevTime) < 3000 ) {
|
||||
Serial.println("Same card");
|
||||
delay(500);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("Found a card!");
|
||||
Serial.print(" IDm: ");
|
||||
nfc.PrintHex(idm, 8);
|
||||
Serial.print(" PMm: ");
|
||||
nfc.PrintHex(pmm, 8);
|
||||
Serial.print(" System Code: ");
|
||||
Serial.print(systemCodeResponse, HEX);
|
||||
Serial.print("\n");
|
||||
|
||||
memcpy(_prevIDm, idm, 8);
|
||||
_prevTime = millis();
|
||||
|
||||
uint8_t blockData[3][16];
|
||||
uint16_t serviceCodeList[1];
|
||||
uint16_t blockList[3];
|
||||
|
||||
Serial.println("Write Without Encryption command ");
|
||||
serviceCodeList[0] = 0x0009;
|
||||
blockList[0] = 0x8000;
|
||||
unsigned long now = millis();
|
||||
blockData[0][3] = now & 0xFF;
|
||||
blockData[0][2] = (now >>= 8) & 0xFF;
|
||||
blockData[0][1] = (now >>= 8) & 0xFF;
|
||||
blockData[0][0] = (now >>= 8) & 0xFF;
|
||||
Serial.print(" Writing current millis (");
|
||||
PrintHex8(blockData[0][0]);
|
||||
PrintHex8(blockData[0][1]);
|
||||
PrintHex8(blockData[0][2]);
|
||||
PrintHex8(blockData[0][3]);
|
||||
Serial.print(" ) to Block 0 -> ");
|
||||
ret = nfc.felica_WriteWithoutEncryption(1, serviceCodeList, 1, blockList, blockData);
|
||||
if (ret != 1)
|
||||
{
|
||||
Serial.println("error");
|
||||
} else {
|
||||
Serial.println("OK!");
|
||||
}
|
||||
memset(blockData[0], 0, 16);
|
||||
|
||||
Serial.print("Read Without Encryption command -> ");
|
||||
serviceCodeList[0] = 0x000B;
|
||||
blockList[0] = 0x8000;
|
||||
blockList[1] = 0x8001;
|
||||
blockList[2] = 0x8002;
|
||||
ret = nfc.felica_ReadWithoutEncryption(1, serviceCodeList, 3, blockList, blockData);
|
||||
if (ret != 1)
|
||||
{
|
||||
Serial.println("error");
|
||||
} else {
|
||||
Serial.println("OK!");
|
||||
for(int i=0; i<3; i++ ) {
|
||||
Serial.print(" Block no. "); Serial.print(i, DEC); Serial.print(": ");
|
||||
nfc.PrintHex(blockData[i], 16);
|
||||
}
|
||||
}
|
||||
|
||||
// Wait 1 second before continuing
|
||||
Serial.println("Card access completed!\n");
|
||||
delay(1000);
|
||||
}
|
||||
145
Arduino/nfcReader/PN532/examples/android_hce/android_hce.ino
Normal file
145
Arduino/nfcReader/PN532/examples/android_hce/android_hce.ino
Normal file
@@ -0,0 +1,145 @@
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 1
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
#endif
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("-------Peer to Peer HCE--------");
|
||||
|
||||
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);
|
||||
|
||||
// Set the max number of retry attempts to read from a card
|
||||
// This prevents us from waiting forever for a card, which is
|
||||
// the default behaviour of the PN532.
|
||||
//nfc.setPassiveActivationRetries(0xFF);
|
||||
|
||||
// configure board to read RFID tags
|
||||
nfc.SAMConfig();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
bool success;
|
||||
|
||||
uint8_t responseLength = 32;
|
||||
|
||||
Serial.println("Waiting for an ISO14443A card");
|
||||
|
||||
// set shield to inListPassiveTarget
|
||||
success = nfc.inListPassiveTarget();
|
||||
|
||||
if(success) {
|
||||
|
||||
Serial.println("Found something!");
|
||||
|
||||
uint8_t selectApdu[] = { 0x00, /* CLA */
|
||||
0xA4, /* INS */
|
||||
0x04, /* P1 */
|
||||
0x00, /* P2 */
|
||||
0x07, /* Length of AID */
|
||||
0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* AID defined on Android App */
|
||||
0x00 /* Le */ };
|
||||
|
||||
uint8_t response[32];
|
||||
|
||||
success = nfc.inDataExchange(selectApdu, sizeof(selectApdu), response, &responseLength);
|
||||
|
||||
if(success) {
|
||||
|
||||
Serial.print("responseLength: "); Serial.println(responseLength);
|
||||
|
||||
nfc.PrintHexChar(response, responseLength);
|
||||
|
||||
do {
|
||||
uint8_t apdu[] = "Hello from Arduino";
|
||||
uint8_t back[32];
|
||||
uint8_t length = 32;
|
||||
|
||||
success = nfc.inDataExchange(apdu, sizeof(apdu), back, &length);
|
||||
|
||||
if(success) {
|
||||
|
||||
Serial.print("responseLength: "); Serial.println(length);
|
||||
|
||||
nfc.PrintHexChar(back, length);
|
||||
}
|
||||
else {
|
||||
|
||||
Serial.println("Broken connection?");
|
||||
}
|
||||
}
|
||||
while(success);
|
||||
}
|
||||
else {
|
||||
|
||||
Serial.println("Failed sending SELECT AID");
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
Serial.println("Didn't find anything!");
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void printResponse(uint8_t *response, uint8_t responseLength) {
|
||||
|
||||
String respBuffer;
|
||||
|
||||
for (int i = 0; i < responseLength; i++) {
|
||||
|
||||
if (response[i] < 0x10)
|
||||
respBuffer = respBuffer + "0"; //Adds leading zeros if hex value is smaller than 0x10
|
||||
|
||||
respBuffer = respBuffer + String(response[i], HEX) + " ";
|
||||
}
|
||||
|
||||
Serial.print("response: "); Serial.println(respBuffer);
|
||||
}
|
||||
|
||||
void setupNFC() {
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "emulatetag.h"
|
||||
#include "NdefMessage.h"
|
||||
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
EmulateTag nfc(pn532spi);
|
||||
#elif 1
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
EmulateTag nfc(pn532hsu);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
uint8_t ndefBuf[120];
|
||||
NdefMessage message;
|
||||
int messageSize;
|
||||
|
||||
uint8_t uid[3] = { 0x12, 0x34, 0x56 };
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("------- Emulate Tag --------");
|
||||
|
||||
message = NdefMessage();
|
||||
message.addUriRecord("http://www.elechouse.com");
|
||||
messageSize = message.getEncodedSize();
|
||||
if (messageSize > sizeof(ndefBuf)) {
|
||||
Serial.println("ndefBuf is too small");
|
||||
while (1) { }
|
||||
}
|
||||
|
||||
Serial.print("Ndef encoded message size: ");
|
||||
Serial.println(messageSize);
|
||||
|
||||
message.encode(ndefBuf);
|
||||
|
||||
// comment out this command for no ndef message
|
||||
nfc.setNdefFile(ndefBuf, messageSize);
|
||||
|
||||
// uid must be 3 bytes!
|
||||
nfc.setUid(uid);
|
||||
|
||||
nfc.init();
|
||||
}
|
||||
|
||||
void loop(){
|
||||
// uncomment for overriding ndef in case a write to this tag occured
|
||||
//nfc.setNdefFile(ndefBuf, messageSize);
|
||||
|
||||
// start emulation (blocks)
|
||||
nfc.emulate();
|
||||
|
||||
// or start emulation with timeout
|
||||
/*if(!nfc.emulate(1000)){ // timeout 1 second
|
||||
Serial.println("timed out");
|
||||
}*/
|
||||
|
||||
// deny writing to the tag
|
||||
// nfc.setTagWriteable(false);
|
||||
|
||||
if(nfc.writeOccured()){
|
||||
Serial.println("\nWrite occured !");
|
||||
uint8_t* tag_buf;
|
||||
uint16_t length;
|
||||
|
||||
nfc.getContent(&tag_buf, &length);
|
||||
NdefMessage msg = NdefMessage(tag_buf, length);
|
||||
msg.print();
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
This example will attempt to connect to an ISO14443A
|
||||
card or tag and retrieve some basic information about it
|
||||
that can be used to determine what type of card it is.
|
||||
|
||||
Note that you need the baud rate to be 115200 because we need to print
|
||||
out the data and read from the card at the same time!
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
|
||||
/* When the number after #if set as 1, it will be switch to SPI Mode*/
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
|
||||
/* When the number after #elif set as 1, it will be switch to HSU Mode*/
|
||||
#elif 0
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
|
||||
/* When the number after #if & #elif set as 0, it will be switch to I2C Mode*/
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
#include <NfcAdapter.h>
|
||||
|
||||
PN532_I2C pn532i2c(Wire);
|
||||
PN532 nfc(pn532i2c);
|
||||
#endif
|
||||
|
||||
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);
|
||||
|
||||
// Set the max number of retry attempts to read from a card
|
||||
// This prevents us from waiting forever for a card, which is
|
||||
// the default behaviour of the PN532.
|
||||
nfc.setPassiveActivationRetries(0xFF);
|
||||
|
||||
// configure board to read RFID tags
|
||||
nfc.SAMConfig();
|
||||
|
||||
Serial.println("Waiting for an ISO14443A card");
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
boolean 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[0], &uidLength);
|
||||
|
||||
if (success) {
|
||||
Serial.println("Found a card!");
|
||||
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
|
||||
Serial.print("UID Value: ");
|
||||
for (uint8_t i=0; i < uidLength; i++)
|
||||
{
|
||||
Serial.print(" 0x");Serial.print(uid[i], HEX);
|
||||
}
|
||||
Serial.println("");
|
||||
// Wait 1 second before continuing
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
// PN532 probably timed out waiting for a card
|
||||
Serial.println("Timed out waiting for a card");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
This example attempts to format a clean Mifare Classic 1K card as
|
||||
an NFC Forum tag (to store NDEF messages that can be read by any
|
||||
NFC enabled Android phone, etc.)
|
||||
|
||||
Note that you need the baud rate to be 115200 because we need to print
|
||||
out the data and read from the card at the same time!
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 1
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
#endif
|
||||
/*
|
||||
We can encode many different kinds of pointers to the card,
|
||||
from a URL, to an Email address, to a phone number, and many more
|
||||
check the library header .h file to see the large # of supported
|
||||
prefixes!
|
||||
*/
|
||||
// For a http://www. url:
|
||||
const char * url = "elechouse.com";
|
||||
uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;
|
||||
|
||||
// for an email address
|
||||
//const char * url = "mail@example.com";
|
||||
//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;
|
||||
|
||||
// for a phone number
|
||||
//const char * url = "+1 212 555 1212";
|
||||
//uint8_t ndefprefix = NDEF_URIPREFIX_TEL;
|
||||
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Looking for PN532...");
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
uint8_t success; // Flag to check if there was an error with the PN532
|
||||
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)
|
||||
bool authenticated = false; // Flag to indicate if the sector is authenticated
|
||||
|
||||
// Use the default key
|
||||
uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("PLEASE NOTE: Formatting your card for NDEF records will change the");
|
||||
Serial.println("authentication keys. To reformat your NDEF tag as a clean Mifare");
|
||||
Serial.println("Classic tag, use the mifareclassic_ndeftoclassic example!");
|
||||
Serial.println("");
|
||||
Serial.println("Place your Mifare Classic card on the reader to format with NDEF");
|
||||
Serial.println("and press any key to continue ...");
|
||||
// Wait for user input before proceeding
|
||||
while (!Serial.available());
|
||||
// a key was pressed1
|
||||
while (Serial.available()) Serial.read();
|
||||
|
||||
// Wait for an ISO14443A type card (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);
|
||||
for (uint8_t i = 0; i < uidLength; i++) {
|
||||
Serial.print(uid[i], HEX);
|
||||
Serial.print(' ');
|
||||
}
|
||||
Serial.println("");
|
||||
|
||||
// Make sure this is a Mifare Classic card
|
||||
if (uidLength != 4)
|
||||
{
|
||||
Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
|
||||
return;
|
||||
}
|
||||
|
||||
// We probably have a Mifare Classic card ...
|
||||
Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
|
||||
|
||||
// Try to format the card for NDEF data
|
||||
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya);
|
||||
if (!success)
|
||||
{
|
||||
Serial.println("Unable to authenticate block 0 to enable card formatting!");
|
||||
return;
|
||||
}
|
||||
success = nfc.mifareclassic_FormatNDEF();
|
||||
if (!success)
|
||||
{
|
||||
Serial.println("Unable to format the card for NDEF");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Card has been formatted for NDEF data using MAD1");
|
||||
|
||||
// Try to authenticate block 4 (first block of sector 1) using our key
|
||||
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keya);
|
||||
|
||||
// Make sure the authentification process didn't fail
|
||||
if (!success)
|
||||
{
|
||||
Serial.println("Authentication failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to write a URL
|
||||
Serial.println("Writing URI to sector 1 as an NDEF Message");
|
||||
|
||||
// Authenticated seems to have worked
|
||||
// Try to write an NDEF record to sector 1
|
||||
// Use 0x01 for the URI Identifier Code to prepend "http://www."
|
||||
// to the url (and save some space). For information on URI ID Codes
|
||||
// see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef
|
||||
if (strlen(url) > 38)
|
||||
{
|
||||
// The length is also checked in the WriteNDEFURI function, but lets
|
||||
// warn users here just in case they change the value and it's bigger
|
||||
// than it should be
|
||||
Serial.println("URI is too long ... must be less than 38 characters long");
|
||||
return;
|
||||
}
|
||||
|
||||
// URI is within size limits ... write it to the card and report success/failure
|
||||
success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url);
|
||||
if (success)
|
||||
{
|
||||
Serial.println("NDEF URI Record written to sector 1");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("NDEF Record creation failed! :(");
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a bit before trying again
|
||||
Serial.println("\n\nDone!");
|
||||
delay(1000);
|
||||
Serial.flush();
|
||||
while(Serial.available()) Serial.read();
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
This example attempts to dump the contents of a Mifare Classic 1K card
|
||||
|
||||
Note that you need the baud rate to be 115200 because we need to print
|
||||
out the data and read from the card at the same time!
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 1
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
#endif
|
||||
|
||||
void setup(void) {
|
||||
// has to be fast to dump the entire memory contents!
|
||||
Serial.begin(115200);
|
||||
Serial.println("Looking for PN532...");
|
||||
|
||||
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; // Flag to check if there was an error with the PN532
|
||||
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)
|
||||
uint8_t currentblock; // Counter to keep track of which block we're on
|
||||
bool authenticated = false; // Flag to indicate if the sector is authenticated
|
||||
uint8_t data[16]; // Array to store block data during reads
|
||||
|
||||
// Keyb on NDEF and Mifare Classic should be the same
|
||||
uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
|
||||
// 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: ");
|
||||
for (uint8_t i = 0; i < uidLength; i++) {
|
||||
Serial.print(uid[i], HEX);
|
||||
Serial.print(' ');
|
||||
}
|
||||
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 try to go through all 16 sectors (each having 4 blocks)
|
||||
// authenticating each sector, and then dumping the blocks
|
||||
for (currentblock = 0; currentblock < 64; currentblock++)
|
||||
{
|
||||
// Check if this is a new block so that we can reauthenticate
|
||||
if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false;
|
||||
|
||||
// If the sector hasn't been authenticated, do so first
|
||||
if (!authenticated)
|
||||
{
|
||||
// Starting of a new sector ... try to to authenticate
|
||||
Serial.print("------------------------Sector ");Serial.print(currentblock/4, DEC);Serial.println("-------------------------");
|
||||
if (currentblock == 0)
|
||||
{
|
||||
// This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
|
||||
// or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards using key a,
|
||||
// but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)
|
||||
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
|
||||
}
|
||||
else
|
||||
{
|
||||
// This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
|
||||
// or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards using key a,
|
||||
// but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)
|
||||
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
|
||||
}
|
||||
if (success)
|
||||
{
|
||||
authenticated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Authentication error");
|
||||
}
|
||||
}
|
||||
// If we're still not authenticated just skip the block
|
||||
if (!authenticated)
|
||||
{
|
||||
Serial.print("Block ");Serial.print(currentblock, DEC);Serial.println(" unable to authenticate");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Authenticated ... we should be able to read the block now
|
||||
// Dump the data into the 'data' array
|
||||
success = nfc.mifareclassic_ReadDataBlock(currentblock, data);
|
||||
if (success)
|
||||
{
|
||||
// Read successful
|
||||
Serial.print("Block ");Serial.print(currentblock, DEC);
|
||||
if (currentblock < 10)
|
||||
{
|
||||
Serial.print(" ");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(" ");
|
||||
}
|
||||
// Dump the raw data
|
||||
nfc.PrintHexChar(data, 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Oops ... something happened
|
||||
Serial.print("Block ");Serial.print(currentblock, DEC);
|
||||
Serial.println(" unable to read this block");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
|
||||
}
|
||||
}
|
||||
// Wait a bit before trying again
|
||||
Serial.println("\n\nSend a character to run the mem dumper again!");
|
||||
Serial.flush();
|
||||
while (!Serial.available());
|
||||
while (Serial.available()) {
|
||||
Serial.read();
|
||||
}
|
||||
Serial.flush();
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
This examples attempts to take a Mifare Classic 1K card that has been
|
||||
formatted for NDEF messages using mifareclassic_formatndef, and resets
|
||||
the authentication keys back to the Mifare Classic defaults
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 1
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
#endif
|
||||
|
||||
#define NR_SHORTSECTOR (32) // Number of short sectors on Mifare 1K/4K
|
||||
#define NR_LONGSECTOR (8) // Number of long sectors on Mifare 4K
|
||||
#define NR_BLOCK_OF_SHORTSECTOR (4) // Number of blocks in a short sector
|
||||
#define NR_BLOCK_OF_LONGSECTOR (16) // Number of blocks in a long sector
|
||||
|
||||
// Determine the sector trailer block based on sector number
|
||||
#define BLOCK_NUMBER_OF_SECTOR_TRAILER(sector) (((sector)<NR_SHORTSECTOR)? \
|
||||
((sector)*NR_BLOCK_OF_SHORTSECTOR + NR_BLOCK_OF_SHORTSECTOR-1):\
|
||||
(NR_SHORTSECTOR*NR_BLOCK_OF_SHORTSECTOR + (sector-NR_SHORTSECTOR)*NR_BLOCK_OF_LONGSECTOR + NR_BLOCK_OF_LONGSECTOR-1))
|
||||
|
||||
// Determine the sector's first block based on the sector number
|
||||
#define BLOCK_NUMBER_OF_SECTOR_1ST_BLOCK(sector) (((sector)<NR_SHORTSECTOR)? \
|
||||
((sector)*NR_BLOCK_OF_SHORTSECTOR):\
|
||||
(NR_SHORTSECTOR*NR_BLOCK_OF_SHORTSECTOR + (sector-NR_SHORTSECTOR)*NR_BLOCK_OF_LONGSECTOR))
|
||||
|
||||
// The default Mifare Classic key
|
||||
static const uint8_t KEY_DEFAULT_KEYAB[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Looking for PN532...");
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
uint8_t success; // Flag to check if there was an error with the PN532
|
||||
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)
|
||||
bool authenticated = false; // Flag to indicate if the sector is authenticated
|
||||
uint8_t blockBuffer[16]; // Buffer to store block contents
|
||||
uint8_t blankAccessBits[3] = { 0xff, 0x07, 0x80 };
|
||||
uint8_t idx = 0;
|
||||
uint8_t numOfSector = 16; // Assume Mifare Classic 1K for now (16 4-block sectors)
|
||||
|
||||
Serial.println("Place your NDEF formatted Mifare Classic 1K card on the reader");
|
||||
Serial.println("and press any key to continue ...");
|
||||
|
||||
// Wait for user input before proceeding
|
||||
while (!Serial.available());
|
||||
while (Serial.available()) Serial.read();
|
||||
|
||||
// Wait for an ISO14443A type card (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)
|
||||
{
|
||||
// We seem to have a tag ...
|
||||
// Display some basic information about it
|
||||
Serial.println("Found an ISO14443A card/tag");
|
||||
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
|
||||
Serial.print(" UID Value: ");
|
||||
nfc.PrintHex(uid, uidLength);
|
||||
Serial.println("");
|
||||
|
||||
// Make sure this is a Mifare Classic card
|
||||
if (uidLength != 4)
|
||||
{
|
||||
Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
|
||||
Serial.println("");
|
||||
Serial.println("Reformatting card for Mifare Classic (please don't touch it!) ... ");
|
||||
|
||||
// Now run through the card sector by sector
|
||||
for (idx = 0; idx < numOfSector; idx++)
|
||||
{
|
||||
// Step 1: Authenticate the current sector using key B 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
|
||||
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, BLOCK_NUMBER_OF_SECTOR_TRAILER(idx), 1, (uint8_t *)KEY_DEFAULT_KEYAB);
|
||||
if (!success)
|
||||
{
|
||||
Serial.print("Authentication failed for sector "); Serial.println(numOfSector);
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 2: Write to the other blocks
|
||||
if (idx == 16)
|
||||
{
|
||||
memset(blockBuffer, 0, sizeof(blockBuffer));
|
||||
if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 3, blockBuffer)))
|
||||
{
|
||||
Serial.print("Unable to write to sector "); Serial.println(numOfSector);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ((idx == 0) || (idx == 16))
|
||||
{
|
||||
memset(blockBuffer, 0, sizeof(blockBuffer));
|
||||
if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer)))
|
||||
{
|
||||
Serial.print("Unable to write to sector "); Serial.println(numOfSector);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(blockBuffer, 0, sizeof(blockBuffer));
|
||||
if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 3, blockBuffer)))
|
||||
{
|
||||
Serial.print("Unable to write to sector "); Serial.println(numOfSector);
|
||||
return;
|
||||
}
|
||||
if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer)))
|
||||
{
|
||||
Serial.print("Unable to write to sector "); Serial.println(numOfSector);
|
||||
return;
|
||||
}
|
||||
}
|
||||
memset(blockBuffer, 0, sizeof(blockBuffer));
|
||||
if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 1, blockBuffer)))
|
||||
{
|
||||
Serial.print("Unable to write to sector "); Serial.println(numOfSector);
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 3: Reset both keys to 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
|
||||
memcpy(blockBuffer, KEY_DEFAULT_KEYAB, sizeof(KEY_DEFAULT_KEYAB));
|
||||
memcpy(blockBuffer + 6, blankAccessBits, sizeof(blankAccessBits));
|
||||
blockBuffer[9] = 0x69;
|
||||
memcpy(blockBuffer + 10, KEY_DEFAULT_KEYAB, sizeof(KEY_DEFAULT_KEYAB));
|
||||
|
||||
// Step 4: Write the trailer block
|
||||
if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)), blockBuffer)))
|
||||
{
|
||||
Serial.print("Unable to write trailer block of sector "); Serial.println(numOfSector);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a bit before trying again
|
||||
Serial.println("\n\nDone!");
|
||||
delay(1000);
|
||||
Serial.flush();
|
||||
while(Serial.available()) Serial.read();
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
Updates a sector that is already formatted for NDEF (using
|
||||
mifareclassic_formatndef.pde for example), inserting a new url
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 1
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
We can encode many different kinds of pointers to the card,
|
||||
from a URL, to an Email address, to a phone number, and many more
|
||||
check the library header .h file to see the large # of supported
|
||||
prefixes!
|
||||
*/
|
||||
// For a http://www. url:
|
||||
const char * url = "elechouse.com";
|
||||
uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;
|
||||
|
||||
// for an email address
|
||||
//const char * url = "mail@example.com";
|
||||
//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;
|
||||
|
||||
// for a phone number
|
||||
//const char * url = "+1 212 555 1212";
|
||||
//uint8_t ndefprefix = NDEF_URIPREFIX_TEL;
|
||||
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Looking for PN532...");
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
uint8_t success; // Flag to check if there was an error with the PN532
|
||||
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)
|
||||
bool authenticated = false; // Flag to indicate if the sector is authenticated
|
||||
|
||||
// Use the default NDEF keys (these would have have set by mifareclassic_formatndef.pde!)
|
||||
uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
|
||||
uint8_t keyb[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };
|
||||
|
||||
Serial.println("Place your NDEF formatted Mifare Classic card on the reader to update the");
|
||||
Serial.println("NDEF record and press any key to continue ...");
|
||||
// Wait for user input before proceeding
|
||||
while (!Serial.available());
|
||||
// a key was pressed1
|
||||
while (Serial.available()) Serial.read();
|
||||
|
||||
// Wait for an ISO14443A type card (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("");
|
||||
|
||||
// Make sure this is a Mifare Classic card
|
||||
if (uidLength != 4)
|
||||
{
|
||||
Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
|
||||
return;
|
||||
}
|
||||
|
||||
// We probably have a Mifare Classic card ...
|
||||
Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
|
||||
|
||||
// Check if this is an NDEF card (using first block of sector 1 from mifareclassic_formatndef.pde)
|
||||
// Must authenticate on the first key using 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7
|
||||
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keyb);
|
||||
if (!success)
|
||||
{
|
||||
Serial.println("Unable to authenticate block 4 ... is this card NDEF formatted?");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Authentication succeeded (seems to be an NDEF/NFC Forum tag) ...");
|
||||
|
||||
// Authenticated seems to have worked
|
||||
// Try to write an NDEF record to sector 1
|
||||
// Use 0x01 for the URI Identifier Code to prepend "http://www."
|
||||
// to the url (and save some space). For information on URI ID Codes
|
||||
// see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef
|
||||
if (strlen(url) > 38)
|
||||
{
|
||||
// The length is also checked in the WriteNDEFURI function, but lets
|
||||
// warn users here just in case they change the value and it's bigger
|
||||
// than it should be
|
||||
Serial.println("URI is too long ... must be less than 38 characters!");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Updating sector 1 with URI as NDEF Message");
|
||||
|
||||
// URI is within size limits ... write it to the card and report success/failure
|
||||
success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url);
|
||||
if (success)
|
||||
{
|
||||
Serial.println("NDEF URI Record written to sector 1");
|
||||
Serial.println("");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("NDEF Record creation failed! :(");
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a bit before trying again
|
||||
Serial.println("\n\nDone!");
|
||||
delay(1000);
|
||||
Serial.flush();
|
||||
while(Serial.available()) Serial.read();
|
||||
}
|
||||
50
Arduino/nfcReader/PN532/examples/p2p_raw/p2p_raw.ino
Normal file
50
Arduino/nfcReader/PN532/examples/p2p_raw/p2p_raw.ino
Normal file
@@ -0,0 +1,50 @@
|
||||
// snep_test.ino
|
||||
// send a SNEP message to adnroid and get a message from android
|
||||
|
||||
#include "SPI.h"
|
||||
#include "PN532_SPI.h"
|
||||
#include "llcp.h"
|
||||
#include "snep.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
SNEP nfc(pn532spi);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("-------Peer to Peer--------");
|
||||
}
|
||||
|
||||
uint8_t message[] = {
|
||||
0xD2, 0xA, 0xB, 0x74,0x65, 0x78, 0x74, 0x2F, 0x70, 0x6C,
|
||||
0x61, 0x69, 0x6E, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77,
|
||||
0x6F, 0x72, 0x6C, 0x64};
|
||||
|
||||
uint8_t buf[128];
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
nfc.write(message, sizeof(message));
|
||||
delay(3000);
|
||||
|
||||
int16_t len = nfc.read(buf, sizeof(buf));
|
||||
if (len > 0) {
|
||||
Serial.println("get a SNEP message:");
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
Serial.print(buf[i], HEX);
|
||||
Serial.print(' ');
|
||||
}
|
||||
Serial.print('\n');
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
char c = buf[i];
|
||||
if (c <= 0x1f || c > 0x7f) {
|
||||
Serial.print('.');
|
||||
} else {
|
||||
Serial.print(c);
|
||||
}
|
||||
}
|
||||
Serial.print('\n');
|
||||
}
|
||||
delay(3000);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// send a NDEF message to adnroid or get a NDEF message
|
||||
//
|
||||
// note: [NDEF library](https://github.com/Don/NDEF) is needed.
|
||||
|
||||
#include "SPI.h"
|
||||
#include "PN532_SPI.h"
|
||||
#include "snep.h"
|
||||
#include "NdefMessage.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
SNEP nfc(pn532spi);
|
||||
uint8_t ndefBuf[128];
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("-------Peer to Peer--------");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
#if 1
|
||||
Serial.println("Send a message to Android");
|
||||
NdefMessage message = NdefMessage();
|
||||
message.addUriRecord("http://www.seeedstudio.com");
|
||||
int messageSize = message.getEncodedSize();
|
||||
if (messageSize > sizeof(ndefBuf)) {
|
||||
Serial.println("ndefBuf is too small");
|
||||
while (1) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
message.encode(ndefBuf);
|
||||
if (0 >= nfc.write(ndefBuf, messageSize)) {
|
||||
Serial.println("Failed");
|
||||
} else {
|
||||
Serial.println("Success");
|
||||
}
|
||||
|
||||
delay(3000);
|
||||
#else
|
||||
// it seems there are some issues to use NdefMessage to decode the received data from Android
|
||||
Serial.println("Get a message from Android");
|
||||
int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
|
||||
if (msgSize > 0) {
|
||||
NdefMessage msg = NdefMessage(ndefBuf, msgSize);
|
||||
msg.print();
|
||||
Serial.println("\nSuccess");
|
||||
} else {
|
||||
Serial.println("failed");
|
||||
}
|
||||
delay(3000);
|
||||
#endif
|
||||
}
|
||||
158
Arduino/nfcReader/PN532/examples/readMifare/readMifare.pde
Normal file
158
Arduino/nfcReader/PN532/examples/readMifare/readMifare.pde
Normal file
@@ -0,0 +1,158 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
This example will wait for any ISO14443A card or tag, and
|
||||
depending on the size of the UID will attempt to read from it.
|
||||
|
||||
If the card has a 4-byte UID it is probably a Mifare
|
||||
Classic card, and the following steps are taken:
|
||||
|
||||
- Authenticate block 4 (the first block of Sector 1) using
|
||||
the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
|
||||
- If authentication succeeds, we can then read any of the
|
||||
4 blocks in that sector (though only block 4 is read here)
|
||||
|
||||
If the card has a 7-byte UID it is probably a Mifare
|
||||
Ultralight card, and the 4 byte pages can be read directly.
|
||||
Page 4 is read by default since this is the first 'general-
|
||||
purpose' page on the tags.
|
||||
|
||||
To enable debug message, define DEBUG in PN532/PN532_debug.h
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#if 0
|
||||
#include <SPI.h>
|
||||
#include <PN532_SPI.h>
|
||||
#include "PN532.h"
|
||||
|
||||
PN532_SPI pn532spi(SPI, 10);
|
||||
PN532 nfc(pn532spi);
|
||||
#elif 1
|
||||
#include <PN532_HSU.h>
|
||||
#include <PN532.h>
|
||||
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
PN532 nfc(pn532hsu);
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#include <PN532_I2C.h>
|
||||
#include <PN532.h>
|
||||
PN532_I2C pn532i2c(Wire);
|
||||
PN532 nfc(pn532i2c);
|
||||
#endif
|
||||
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
|
||||
// Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
|
||||
Serial.println("Trying to authenticate block 4 with default KEYA value");
|
||||
uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
|
||||
// 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, keya);
|
||||
|
||||
if (success)
|
||||
{
|
||||
Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
|
||||
uint8_t data[16];
|
||||
|
||||
// If you want to write something to block 4 to test with, uncomment
|
||||
// the following line and this text should be read back in a minute
|
||||
// data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0};
|
||||
// success = nfc.mifareclassic_WriteDataBlock (4, data);
|
||||
|
||||
// Try to read the contents of block 4
|
||||
success = nfc.mifareclassic_ReadDataBlock(4, data);
|
||||
|
||||
if (success)
|
||||
{
|
||||
// Data seems to have been read ... spit it out
|
||||
Serial.println("Reading Block 4:");
|
||||
nfc.PrintHexChar(data, 16);
|
||||
Serial.println("");
|
||||
|
||||
// Wait a bit before reading the card again
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Ooops ... unable to read the requested block. Try another key?");
|
||||
}
|
||||
}
|
||||
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!?");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user