I've foud a project on the internet that suits my needs.
A door opener using the lowcost RFID RD522 and a Arduino
The mechanism is the following:
Scan the Master Card followed bij a unknown card.
The unknown card ID is stored on the SDcard and is a 'known' card from that moment on.
And the use of a known card grants access.
Removing a card is don by scanning the master followed by the card to be removed.
But the function removeCard empties the complete database.
And that is a unwanted behaviour.
What I found out is that the file cards.txt (containing valid cards) is replaced by an empty file called cardsTemp.txt
I've already contacted the poster of the source and he admits that his code is flaw, but on my help request there is nou action from him.
And I very much want to make things working.
Is there anyone of you(cracks) who can help me with this problem.
I had to truncate the code, because the 9000 character limit.
Thanx in advance
regards Piet.
#include <SPI.h>
#include <Wire.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <SdFat.h>
#define MFRC_RST_PIN 9
#define MFRC_SS_PIN 10
#define SD_SS_PIN 8
#define STATE_STARTUP 0
#define STATE_STARTUP_ERROR 1
#define STATE_STARTING 2
#define STATE_WAITING 3
#define STATE_SCAN_INVALID 4
#define STATE_SCAN_VALID 5
#define STATE_SCAN_MASTER 6
#define STATE_ADDED_CARD 7
#define STATE_REMOVED_CARD 8
#define REDPIN 6
#define GREENPIN 7
#define RELAYPIN 5
const int cardSize = 4;
byte masterCard[cardSize] = {86,187,140,117};
byte readCard[cardSize] = {0,0,0,0};
// Create MFRC522 instance
MFRC522 mfrc522(MFRC_SS_PIN, MFRC_RST_PIN);
// Set the LCD I2C address
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
SdFat sd;
byte currentState = STATE_STARTUP;
unsigned long LastStateChangeTime;
unsigned long StateWaitTime;
char cardFile[] = "cards.txt";
char cardTempFile[] = "cardsTemp.txt";
//------------------------------------------------------------------------------------
void PrintCard(byte printCard[cardSize])
{
int index;
Serial.print("Card - ");
for(index = 0; index < 4; index++)
{
if (index > 0)
{
Serial.print(",");
}
Serial.print(printCard[index]);
}
Serial.println(" ");
}
//------------------------------------------------------------------------------------
boolean findCard()
{
byte currentCard[cardSize];
char text[10];
char c1;
int index;
int value;
Serial.print("find ");
PrintCard(readCard);
// open input file
ifstream readStr(cardFile);
// check for open error
if (!readStr.is_open())
{
return false;
}
index = 0;
// read until input fails
while (!readStr.eof())
{
readStr >> value >> c1;
if (readStr.fail())
{
break;
}
currentCard[index] = value;
index++;
if (index > 3)
{
Serial.print("file read ");
PrintCard(currentCard);
if ((memcmp(currentCard, readCard, 4)) == 0)
{
return true;
}
index = 0;
}
}
return false;
}
//------------------------------------------------------------------------------------
void addCard()
{
int index;
SdFile writeFile;
Serial.print("add ");
PrintCard(readCard);
Serial.println(cardFile);
Serial.println(O_RDWR);
Serial.println(O_CREAT);
Serial.println(O_AT_END);
if (writeFile.open(cardFile, O_RDWR | O_CREAT | O_AT_END))
{
for(index = 0; index < 4; index++)
// Serial.print(index);
{
writeFile.print(readCard[index]);
writeFile.print(",");
}
writeFile.close();
}
return;
}
//------------------------------------------------------------------------------------
void removeCard()
{
byte currentCard[cardSize];
char text[10];
char c1;
int readIndex, writeIndex;
int value;
SdFile writeFile;
//Serial.print("remove ");
//PrintCard(readCard);
// open input file
ifstream readStr(cardFile);
// check for open error
if (!readStr.is_open())
{
return;
}
if (writeFile.open(cardTempFile, O_RDWR | O_CREAT | O_AT_END))
{
readIndex = 0;
while (!readStr.eof())
{
readStr >> value >> c1;
if (readStr.fail())
{
break;
}
currentCard[readIndex] = value;
readIndex++;
if (readIndex > 3)
{
//Serial.print("file write ");
//PrintCard(currentCard);
if (!((memcmp(currentCard, readCard, 4)) == 0))
{
for (writeIndex = 0; writeIndex < 4; writeIndex++)
{
writeFile.print(currentCard[writeIndex]);
writeFile.print(",");
}
writeFile.close();
}
}
readIndex = 0;
}
}
sd.remove(cardFile);
sd.rename(cardTempFile, cardFile);
return;
}
Rest of the code is ommited, 9000 character limit oof the forum.