Ok, so i think i'm getting closer to the way this should be made.
the bellow code works. The object is to check if the id of the card is in the EEPROM. You can see the commented lines i use to setup cards and to manually put the card id into the EEPROM. This works fine. In a few I will post the added code that is meant to take a key id (put into block 4) and put that into the EEPROM. When i do that, I get jibberish. which I do not understand because i'm basicialy doing the same thing.
but with your help i think i'm getting closer. thank you
#include <SPI.h>//include the SPI bus library
#include <MFRC522.h>//include the RFID reader library
#include <EEPROM.h>
#define SS_PIN 10 //slave select pin
#define RST_PIN 9 //reset pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key', which will hold the card information
byte nuidPICC[4];
int PasswordBlock = 2; //this is the block number we will write into and then read. Do not write into 'sector trailer' block, since this can make the block unusable.
int BanKeyBlock = 4;//this is the block that stores a key programmed into a tag to be deactivated when that tag is used
byte blockcontent[16] = {"ppcc"}; //an array with 16 bytes to be written into one of the 64 card blocks is defined!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
byte deactivatecardblockcontent[16] = {"deactivate"};//all zeros. This can be used to delete a block.
byte readbackblock[18]; //This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.
void setup(){
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
Serial.println("Scan a MIFARE Classic card");
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF; //keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
}
}
void loop() {
// Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
if (!mfrc522.PICC_IsNewCardPresent()) { //if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
return; //if it did not find a new card is returns a '0' and we return to the start of the loop
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) { //if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
return; //if it returns a '0' something went wrong and we return to the start of the loop
}
Serial.println("card selected");
String txt; //This is where we store block 2, the password
//Store Card UID
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = mfrc522.uid.uidByte[i];
}
/*
/////These 3 lines are used to setup cards manually//////
//writeBlock(PasswordBlock, blockcontent);//the blockcontent array is written into the card block
//writeBlock(BanKeyBlock, nuidPICC);//the blockcontent array is written into the card block
//writeBlock(PasswordBlock, deactivatecardblockcontent);//the blockcontent array is written into the card block
//return;
//////these lines are to add banned card to eeprom manually/////
//EEPROM.write(4, nuidPICC[0]);
//EEPROM.write(5, nuidPICC[1]);
//EEPROM.write(6, nuidPICC[2]);
//EEPROM.write(7, nuidPICC[3]);
*/
//check if uid is in eprom and needs to be deactivated
int EEPROMIndex = LookForUidInEEPROM();
if (EEPROMIndex > -1){
Serial.println("deleting card");
Serial.print("first byte of card id in eeprom is: ");
Serial.println(EEPROMIndex);
delay(1000);
//uid is in eeprom, we need to deactivate it and remove from eeprom
// writeBlock(PasswordBlock, deactivatecardblockcontent);
// EEPROM.write(EEPROMIndex, 255);
// EEPROM.write(EEPROMIndex + 1, 255);
// EEPROM.write(EEPROMIndex + 2, 255);
// EEPROM.write(EEPROMIndex + 3, 255);
return;
}
//uid is not in eeprom. continue
/* Serial.println("Continuing, card isn't banned, reading block 2");
//read block 2 for password or other keywords
readBlock(PasswordBlock, readbackblock);
for (int j = 0; j < 16; j++){
Serial.write(readbackblock[j]); //Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
txt = txt + (byte) readbackblock[j]; //adds the acii character to the string. the data ends up being "1121129999000000000000" It needs to be converted to "ppcc000000000000"
}
//handle keyworkds from tag
byte KeyToBeBanned[4];
if (txt == "100101979911610511897116101000000") {
Serial.println("deactivate card used");
//get key from block BanKeyBlock
readBlock(BanKeyBlock, readbackblock);
for (int j = 0; j < 4; j++){
Serial.write(readbackblock[j]); //Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
KeyToBeBanned[0] = readbackblock[j];
KeyToBeBanned[1] = readbackblock[j];
KeyToBeBanned[2] = readbackblock[j];
KeyToBeBanned[3] = readbackblock[j];
}
Serial.println();
Serial.println("new text is");
Serial.println(txt);
if (KeyToBeBanned[0] == nuidPICC[0] && KeyToBeBanned[1] == nuidPICC[1] && KeyToBeBanned[2] == nuidPICC[2] && KeyToBeBanned[3] == nuidPICC[3]){
Serial.println("they match");
}else {
Serial.println("they don't match");
}
delay(99999);
return;
*/
//reset card reader
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
}
///// functions ////
int LookForUidInEEPROM(){
byte compare[4];
for (int i = 0; i < EEPROM.length()+1; i = i + 4){
compare[0] = EEPROM.read(i);
compare[1] = EEPROM.read(i+1);
compare[2] = EEPROM.read(i+2);
compare[3] = EEPROM.read(i+3);
if(compare[0] == nuidPICC[0] && compare[1] == nuidPICC[1] && compare[2] == nuidPICC[2] && compare[3] == nuidPICC[3]){
return i;
}
}
return -1;
}
int FindFirstAvailableEEPROMSetOf8(){
for (int i = 0; EEPROM.length(); i = i + 4){
if(EEPROM.read(i)== 255){
return(i);
}else if(i > EEPROM.length()){
return(32767);
}
}
}
//// rfid functions ////
int readBlock(int blockNumber, byte arrayAddress[]) {
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector
/*****************************************authentication of the desired block for access***********************************************************/
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, & key, & (mfrc522.uid));
//byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
//this method is used to authenticate a certain block for writing or reading
//command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A = 0x60 (=1100000), // this command performs authentication with Key A
//blockAddr is the number of the block from 0 to 15.
//MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF
//Uid *uid is a pointer to the UID struct that contains the user ID of the card.
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed (read): ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3; //return "3" as error message
}
//it appears the authentication needs to be made before every block read/write within a specific sector.
//If a different sector is being authenticated access to the previous one is lost.
/*****************************************reading a block***********************************************************/
byte buffersize = 18; //we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size...
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, & buffersize); //&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4; //return "4" as error message
}
Serial.println("block was read");
}