Merge pull request #86 from Rotzbua/master

Update examples, use F() macro
This commit is contained in:
Miki Balboa
2015-02-23 09:36:07 -06:00
7 changed files with 114 additions and 113 deletions

View File

@@ -54,7 +54,7 @@ void setup() {
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Warning: this example overwrites the UID of your UID changeable card, use with care!"); Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) { for (byte i = 0; i < 6; i++) {
@@ -83,7 +83,7 @@ void loop() {
// Now a card is selected. The UID and SAK is in mfrc522.uid. // Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID // Dump UID
Serial.print("Card UID:"); Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) { for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX); Serial.print(mfrc522.uid.uidByte[i], HEX);
@@ -92,22 +92,22 @@ void loop() {
// Dump PICC type // Dump PICC type
// byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); // byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Serial.print("PICC type: "); // Serial.print(F("PICC type: "));
// Serial.print(mfrc522.PICC_GetTypeName(piccType)); // Serial.print(mfrc522.PICC_GetTypeName(piccType));
// Serial.print(" (SAK "); // Serial.print(F(" (SAK "));
// Serial.print(mfrc522.uid.sak); // Serial.print(mfrc522.uid.sak);
// Serial.print(")\r\n"); // Serial.print(")\r\n");
// if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI // if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
// && piccType != MFRC522::PICC_TYPE_MIFARE_1K // && piccType != MFRC522::PICC_TYPE_MIFARE_1K
// && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { // && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
// Serial.println("This sample only works with MIFARE Classic cards."); // Serial.println(F("This sample only works with MIFARE Classic cards."));
// return; // return;
// } // }
// Set new UID // Set new UID
byte newUid[] = NEW_UID; byte newUid[] = NEW_UID;
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) { if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
Serial.println("Wrote new UID to card."); Serial.println(F("Wrote new UID to card."));
} }
// Halt PICC and re-select it so DumpToSerial doesn't get confused // Halt PICC and re-select it so DumpToSerial doesn't get confused
@@ -117,7 +117,7 @@ void loop() {
} }
// Dump the new memory contents // Dump the new memory contents
Serial.println("New UID and contents:"); Serial.println(F("New UID and contents:"));
mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(2000); delay(2000);

View File

@@ -52,7 +52,7 @@ void setup() {
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 mfrc522.PCD_Init(); // Init MFRC522
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
Serial.println("Scan PICC to see UID, type, and data blocks..."); Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
} }
void loop() { void loop() {
@@ -73,17 +73,17 @@ void loop() {
void ShowReaderDetails() { void ShowReaderDetails() {
// Get the MFRC522 software version // Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print("MFRC522 Software Version: 0x"); Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX); Serial.print(v, HEX);
if (v == 0x91) if (v == 0x91)
Serial.print(" = v1.0"); Serial.print(F(" = v1.0"));
else if (v == 0x92) else if (v == 0x92)
Serial.print(" = v2.0"); Serial.print(F(" = v2.0"));
else else
Serial.print(" (unknown)"); Serial.print(F(" (unknown)"));
Serial.println(""); Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed // When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) { if ((v == 0x00) || (v == 0xFF)) {
Serial.println("WARNING: Communication failure, is the MFRC522 properly connected?"); Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
} }
} }

View File

@@ -52,7 +52,7 @@ void setup() {
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Warning: this example clears your mifare UID, use with care!"); Serial.println(F("Warning: this example clears your mifare UID, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) { for (byte i = 0; i < 6; i++) {
@@ -62,7 +62,7 @@ void setup() {
void loop() { void loop() {
if ( mfrc522.MIFARE_UnbrickUidSector(false) ) { if ( mfrc522.MIFARE_UnbrickUidSector(false) ) {
Serial.println("Cleared sector 0, set UID to 1234. Card should be responsive again now."); Serial.println(F("Cleared sector 0, set UID to 1234. Card should be responsive again now."));
} }
delay(1000); delay(1000);
} }

View File

@@ -53,12 +53,12 @@ void setup() {
key.keyByte[i] = 0xFF; key.keyByte[i] = 0xFF;
} }
Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Block mode."); Serial.println(F("Scan a MIFARE Classic PICC to demonstrate Value Block mode."));
Serial.print("Using key (for A and B):"); Serial.print(F("Using key (for A and B):"));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE); dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
Serial.println(); Serial.println();
Serial.println("BEWARE: Data will be written to the PICC, in sector #1"); Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
} }
/** /**
@@ -74,10 +74,10 @@ void loop() {
return; return;
// Show some details of the PICC (that is: the tag/card) // Show some details of the PICC (that is: the tag/card)
Serial.print("Card UID:"); Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(); Serial.println();
Serial.print("PICC type: "); Serial.print(F("PICC type: "));
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType)); Serial.println(mfrc522.PICC_GetTypeName(piccType));
@@ -85,7 +85,7 @@ void loop() {
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) { && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println("This sample only works with MIFARE Classic cards."); Serial.println(F("This sample only works with MIFARE Classic cards."));
return; return;
} }
@@ -101,16 +101,16 @@ void loop() {
long value; long value;
// Authenticate using key A // Authenticate using key A
Serial.println("Authenticating using key A..."); Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
// Show the whole sector as it currently is // Show the whole sector as it currently is
Serial.println("Current data in sector:"); Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector); mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println(); Serial.println();
@@ -156,10 +156,10 @@ void loop() {
mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3); mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3);
// Read the sector trailer as it is currently stored on the PICC // Read the sector trailer as it is currently stored on the PICC
Serial.println("Reading sector trailer..."); Serial.println(F("Reading sector trailer..."));
status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size); status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: "); Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -169,20 +169,20 @@ void loop() {
&& buffer[7] != trailerBuffer[7] && buffer[7] != trailerBuffer[7]
&& buffer[8] != trailerBuffer[8]) { && buffer[8] != trailerBuffer[8]) {
// They don't match (yet), so write it to the PICC // They don't match (yet), so write it to the PICC
Serial.println("Writing new sector trailer..."); Serial.println(F("Writing new sector trailer..."));
status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16); status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
} }
// Authenticate using key B // Authenticate using key B
Serial.println("Authenticating again using key B..."); Serial.println(F("Authenticating again using key B..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -198,20 +198,20 @@ void loop() {
Serial.print("Adding 1 to value of block "); Serial.println(valueBlockA); Serial.print("Adding 1 to value of block "); Serial.println(valueBlockA);
status = mfrc522.MIFARE_Increment(valueBlockA, 1); status = mfrc522.MIFARE_Increment(valueBlockA, 1);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Increment() failed: "); Serial.print(F("MIFARE_Increment() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
status = mfrc522.MIFARE_Transfer(valueBlockA); status = mfrc522.MIFARE_Transfer(valueBlockA);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Transfer() failed: "); Serial.print(F("MIFARE_Transfer() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
// Show the new value of valueBlockA // Show the new value of valueBlockA
status = mfrc522.MIFARE_GetValue(valueBlockA, &value); status = mfrc522.MIFARE_GetValue(valueBlockA, &value);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("mifare_GetValue() failed: "); Serial.print(F("mifare_GetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -222,31 +222,31 @@ void loop() {
Serial.print("Subtracting 10 from value of block "); Serial.println(valueBlockB); Serial.print("Subtracting 10 from value of block "); Serial.println(valueBlockB);
status = mfrc522.MIFARE_Decrement(valueBlockB, 10); status = mfrc522.MIFARE_Decrement(valueBlockB, 10);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Decrement() failed: "); Serial.print(F("MIFARE_Decrement() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
status = mfrc522.MIFARE_Transfer(valueBlockB); status = mfrc522.MIFARE_Transfer(valueBlockB);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Transfer() failed: "); Serial.print(F("MIFARE_Transfer() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
// Show the new value of valueBlockB // Show the new value of valueBlockB
status = mfrc522.MIFARE_GetValue(valueBlockB, &value); status = mfrc522.MIFARE_GetValue(valueBlockB, &value);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("mifare_GetValue() failed: "); Serial.print(F("mifare_GetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
Serial.print("New value of value block "); Serial.print(valueBlockB); Serial.print(F("New value of value block ")); Serial.print(valueBlockB);
Serial.print(" = "); Serial.println(value); Serial.print(F(" = ")); Serial.println(value);
// Check some boundary... // Check some boundary...
if (value <= -100) { if (value <= -100) {
Serial.println("Below -100, so resetting it to 255 = 0xFF just for fun..."); Serial.println(F("Below -100, so resetting it to 255 = 0xFF just for fun..."));
status = mfrc522.MIFARE_SetValue(valueBlockB, 255); status = mfrc522.MIFARE_SetValue(valueBlockB, 255);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("mifare_SetValue() failed: "); Serial.print(F("mifare_SetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -280,10 +280,10 @@ void formatValueBlock(byte blockAddr) {
byte size = sizeof(buffer); byte size = sizeof(buffer);
byte status; byte status;
Serial.print("Reading block "); Serial.println(blockAddr); Serial.print(F("Reading block ")); Serial.println(blockAddr);
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size); status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: "); Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -301,10 +301,10 @@ void formatValueBlock(byte blockAddr) {
&& (buffer[12] == (byte)~buffer[13]) && (buffer[12] == (byte)~buffer[13])
&& (buffer[12] == buffer[14]) && (buffer[12] == buffer[14])
&& (buffer[12] == (byte)~buffer[15])) { && (buffer[12] == (byte)~buffer[15])) {
Serial.println("Block has correct Value Block format."); Serial.println(F("Block has correct Value Block format."));
} }
else { else {
Serial.println("Formatting as Value Block..."); Serial.println(F("Formatting as Value Block..."));
byte valueBlock[] = { byte valueBlock[] = {
0, 0, 0, 0, 0, 0, 0, 0,
255, 255, 255, 255, 255, 255, 255, 255,
@@ -312,7 +312,7 @@ void formatValueBlock(byte blockAddr) {
blockAddr, ~blockAddr, blockAddr, ~blockAddr }; blockAddr, ~blockAddr, blockAddr, ~blockAddr };
status = mfrc522.MIFARE_Write(blockAddr, valueBlock, 16); status = mfrc522.MIFARE_Write(blockAddr, valueBlock, 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
} }
} }

View File

@@ -52,12 +52,12 @@ void setup() {
key.keyByte[i] = 0xFF; key.keyByte[i] = 0xFF;
} }
Serial.println("Scan a MIFARE Classic PICC to demonstrate read and write."); Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
Serial.print("Using key (for A and B):"); Serial.print(F("Using key (for A and B):"));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE); dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
Serial.println(); Serial.println();
Serial.println("BEWARE: Data will be written to the PICC, in sector #1"); Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
} }
/** /**
@@ -73,10 +73,10 @@ void loop() {
return; return;
// Show some details of the PICC (that is: the tag/card) // Show some details of the PICC (that is: the tag/card)
Serial.print("Card UID:"); Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(); Serial.println();
Serial.print("PICC type: "); Serial.print(F("PICC type: "));
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType)); Serial.println(mfrc522.PICC_GetTypeName(piccType));
@@ -84,7 +84,7 @@ void loop() {
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) { && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println("This sample only works with MIFARE Classic cards."); Serial.println(F("This sample only works with MIFARE Classic cards."));
return; return;
} }
@@ -104,82 +104,82 @@ void loop() {
byte size = sizeof(buffer); byte size = sizeof(buffer);
// Authenticate using key A // Authenticate using key A
Serial.println("Authenticating using key A..."); Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
// Show the whole sector as it currently is // Show the whole sector as it currently is
Serial.println("Current data in sector:"); Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector); mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println(); Serial.println();
// Read data from the block // Read data from the block
Serial.print("Reading data from block "); Serial.print(blockAddr); Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
Serial.println(" ..."); Serial.println(F(" ..."));
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size); status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: "); Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
} }
Serial.print("Data in block "); Serial.print(blockAddr); Serial.println(":"); Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
dump_byte_array(buffer, 16); Serial.println(); dump_byte_array(buffer, 16); Serial.println();
Serial.println(); Serial.println();
// Authenticate using key B // Authenticate using key B
Serial.println("Authenticating again using key B..."); Serial.println(F("Authenticating again using key B..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
// Write data to the block // Write data to the block
Serial.print("Writing data into block "); Serial.print(blockAddr); Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
Serial.println(" ..."); Serial.println(F(" ..."));
dump_byte_array(dataBlock, 16); Serial.println(); dump_byte_array(dataBlock, 16); Serial.println();
status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16); status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
} }
Serial.println(); Serial.println();
// Read data from the block (again, should now be what we have written) // Read data from the block (again, should now be what we have written)
Serial.print("Reading data from block "); Serial.print(blockAddr); Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
Serial.println(" ..."); Serial.println(F(" ..."));
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size); status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: "); Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
} }
Serial.print("Data in block "); Serial.print(blockAddr); Serial.println(":"); Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
dump_byte_array(buffer, 16); Serial.println(); dump_byte_array(buffer, 16); Serial.println();
// Check that data in block is what we have written // Check that data in block is what we have written
// by counting the number of bytes that are equal // by counting the number of bytes that are equal
Serial.println("Checking result..."); Serial.println(F("Checking result..."));
byte count = 0; byte count = 0;
for (byte i = 0; i < 16; i++) { for (byte i = 0; i < 16; i++) {
// Compare buffer (= what we've read) with dataBlock (= what we've written) // Compare buffer (= what we've read) with dataBlock (= what we've written)
if (buffer[i] == dataBlock[i]) if (buffer[i] == dataBlock[i])
count++; count++;
} }
Serial.print("Number of bytes that match = "); Serial.println(count); Serial.print(F("Number of bytes that match = ")); Serial.println(count);
if (count == 16) { if (count == 16) {
Serial.println("Success :-)"); Serial.println(F("Success :-)"));
} else { } else {
Serial.println("Failure, no match :-("); Serial.println(F("Failure, no match :-("));
Serial.println(" perhaps the write didn't work properly..."); Serial.println(F(" perhaps the write didn't work properly..."));
} }
Serial.println(); Serial.println();
// Dump the sector data // Dump the sector data
Serial.println("Current data in sector:"); Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector); mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println(); Serial.println();

View File

@@ -56,7 +56,7 @@ void setup() {
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Try the most used default keys to print block 0 of a MIFARE PICC."); Serial.println(F("Try the most used default keys to print block 0 of a MIFARE PICC."));
} }
/* /*
@@ -82,10 +82,10 @@ boolean try_key(MFRC522::MIFARE_Key *key)
byte block = 0; byte block = 0;
byte status; byte status;
// Serial.println("Authenticating using key A..."); // Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
// Serial.print("PCD_Authenticate() failed: "); // Serial.print(F("PCD_Authenticate() failed: "));
// Serial.println(mfrc522.GetStatusCodeName(status)); // Serial.println(mfrc522.GetStatusCodeName(status));
return false; return false;
} }
@@ -94,17 +94,17 @@ boolean try_key(MFRC522::MIFARE_Key *key)
byte byteCount = sizeof(buffer); byte byteCount = sizeof(buffer);
status = mfrc522.MIFARE_Read(block, buffer, &byteCount); status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
// Serial.print("MIFARE_Read() failed: "); // Serial.print(F("MIFARE_Read() failed: "));
// Serial.println(mfrc522.GetStatusCodeName(status)); // Serial.println(mfrc522.GetStatusCodeName(status));
} }
else { else {
// Successful read // Successful read
result = true; result = true;
Serial.print("Success with key:"); Serial.print(F("Success with key:"));
dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE); dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
Serial.println(); Serial.println();
// Dump block data // Dump block data
Serial.print("Block "); Serial.print(block); Serial.print(":"); Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
dump_byte_array(buffer, 16); dump_byte_array(buffer, 16);
Serial.println(); Serial.println();
} }
@@ -128,10 +128,10 @@ void loop() {
return; return;
// Show some details of the PICC (that is: the tag/card) // Show some details of the PICC (that is: the tag/card)
Serial.print("Card UID:"); Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(); Serial.println();
Serial.print("PICC type: "); Serial.print(F("PICC type: "));
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType)); Serial.println(mfrc522.PICC_GetTypeName(piccType));

View File

@@ -1,16 +1,16 @@
/* /*
* Write personal data of a MIFARE RFID card using a RFID-RC522 reader * Write personal data of a MIFARE RFID card using a RFID-RC522 reader
* Uses MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. * Uses MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
----------------------------------------------------------------------------- * -----------------------------------------------------------------------------------------
* Pin layout should be as follows: * MFRC522 Arduino Arduino Arduino Arduino Arduino
* Signal Pin Pin Pin * Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Arduino Uno Arduino Mega MFRC522 board * Signal Pin Pin Pin Pin Pin Pin
* ------------------------------------------------------------ * -----------------------------------------------------------------------------------------
* Reset 9 5 RST * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS 10 53 SDA * SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI 11 52 MOSI * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO 12 51 MISO * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK 13 50 SCK * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
* *
* Hardware required: * Hardware required:
* Arduino * Arduino
@@ -30,7 +30,7 @@ void setup() {
Serial.begin(9600); // Initialize serial communications with the PC Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Write personal data on a MIFARE PICC "); Serial.println(F("Write personal data on a MIFARE PICC "));
} }
void loop() { void loop() {
@@ -47,12 +47,12 @@ void loop() {
// Select one of the cards // Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) return; if ( ! mfrc522.PICC_ReadCardSerial()) return;
Serial.print("Card UID:"); //Dump UID Serial.print(F("Card UID:")); //Dump UID
for (byte i = 0; i < mfrc522.uid.size; i++) { for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX); Serial.print(mfrc522.uid.uidByte[i], HEX);
} }
Serial.print(" PICC type: "); // Dump PICC type Serial.print(F(" PICC type: ")); // Dump PICC type
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType)); Serial.println(mfrc522.PICC_GetTypeName(piccType));
@@ -62,33 +62,34 @@ void loop() {
Serial.setTimeout(20000L) ; // wait until 20 seconds for input from serial Serial.setTimeout(20000L) ; // wait until 20 seconds for input from serial
// Ask personal data: Family name // Ask personal data: Family name
Serial.println("Type Family name, ending with #"); Serial.println(F("Type Family name, ending with #"));
len=Serial.readBytesUntil('#', (char *) buffer, 30) ; // read family name from serial len=Serial.readBytesUntil('#', (char *) buffer, 30) ; // read family name from serial
for (byte i = len; i < 30; i++) buffer[i] = '\s'; // pad with spaces for (byte i = len; i < 30; i++) buffer[i] = '\s'; // pad with spaces
block = 1; block = 1;
//Serial.println("Authenticating using key A..."); //Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
else Serial.println(F("PCD_Authenticate() success: "));
// Write block // Write block
status = mfrc522.MIFARE_Write(block, buffer, 16); status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
else Serial.println("MIFARE_Write() success: "); else Serial.println(F("MIFARE_Write() success: "));
block = 2; block = 2;
//Serial.println("Authenticating using key A..."); //Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -96,22 +97,22 @@ void loop() {
// Write block // Write block
status = mfrc522.MIFARE_Write(block, &buffer[16], 16); status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
else Serial.println("MIFARE_Write() success: "); else Serial.println(F("MIFARE_Write() success: "));
// Ask personal data: First name // Ask personal data: First name
Serial.println("Type First name, ending with #"); Serial.println(F("Type First name, ending with #"));
len=Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial len=Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial
for (byte i = len; i < 20; i++) buffer[i] = '\s'; // pad with spaces for (byte i = len; i < 20; i++) buffer[i] = '\s'; // pad with spaces
block = 4; block = 4;
//Serial.println("Authenticating using key A..."); //Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -119,17 +120,17 @@ void loop() {
// Write block // Write block
status = mfrc522.MIFARE_Write(block, buffer, 16); status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
else Serial.println("MIFARE_Write() success: "); else Serial.println(F("MIFARE_Write() success: "));
block = 5; block = 5;
//Serial.println("Authenticating using key A..."); //Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: "); Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
@@ -137,11 +138,11 @@ void loop() {
// Write block // Write block
status = mfrc522.MIFARE_Write(block, &buffer[16], 16); status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) { if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: "); Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status)); Serial.println(mfrc522.GetStatusCodeName(status));
return; return;
} }
else Serial.println("MIFARE_Write() success: "); else Serial.println(F("MIFARE_Write() success: "));
Serial.println(" "); Serial.println(" ");