Getting the RFID-RC522 to work! [SOLVED]

Heya @perucat, you are most welcome to reuse some code I recently implemented for the spark-bouncer

// Authenticate a block access
bool rfidAuth(int block) {
	MFRC522::MIFARE_Key key;
	
	// All Mifare chips have their factory default keys set to HIGH
	memset(&key, 0xFF, sizeof(key));

	byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
	if (status != MFRC522::STATUS_OK) {
		return false;
	}

	// All good!
	return true;
}
bool rfidWrite(int block, byte (&data)[16]) {
	if (mfrc522.MIFARE_Write(block, data, 16) != MFRC522::STATUS_OK) {
                return false;
	}
	return true;
}

How to use it:

byte buffer[16]; 
int block = 1;
...
if (rfidAuth(block))
    if (rfidWrite(block, buffer))
        ...  // SUCCESS

Take a look how its being used in the mentioned project to update a 16 byte key stored in the first RFID block: https://github.com/rastapasta/spark-bouncer/blob/master/application.cpp#L417-L457

Enjoy!

2 Likes