Arduino RFID-RC522 - DOOR ACCES CONTROL/ RFID DOOR LOCK

Toon hier Uw afgeronde projecten aan anderen.
Berichten: 9
Geregistreerd: 12 Mei 2016, 01:43

Arduino RFID-RC522 - DOOR ACCES CONTROL/ RFID DOOR LOCK

Berichtdoor arduinosensorsNL » 16 Mei 2016, 00:57

Hello world! Gisteren heb ik een RFID DOOR LOCK gemaakt.
Het lampje stelt het slot voor, deze had ik niet liggen. Je kan meerdere kaarten Instellen en er is een reset knop die je ingedrukt moet houden om alle kaarten te wissen,
terwijl je hem ingedrukt houd, reset je arduino, je hebt 5 seconden om te annuleren.

You can uncomment this line below (line 68) in the code it will increase reading distance : mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);

Parts list.

1: Arduino uno, I used the Robotdyn Uno. http://arduinosensors.nl/index.php?route=product/product&path=25_29&product_id=92
2: RFID RC522 - Blue colored version. http://goo.gl/cl7Rv8
3: 1 channel Relay KY-019 http://arduinosensors.nl/index.php?route=product/product&product_id=86&search=ky-019
4: KY-004 Button module. (Om de database te legen)
5: Jumper wires set. MM/FF/MF
6: Electrical door lock, (Optie, Niet nodig om de code te testen.)
7: Mini Bread Board (Optie, ik heb het gebruikt om de RFID module recht te zetten)
Good luck!

Bekijk de video hier. https://www.youtube.com/watch?v=M6ipeiGxtmk

Connect alles zoals de schematic.
Afbeelding

Upload de code! and het zou moeten werken.
Wees er zeker van dat je de volgende Libraries hebt geinstalleerd
<EEPROM.h>
<SPI.h>
<MFRC522.h>


cpp code
#include <EEPROM.h>     // We are going to read and write PICC's UIDs from/to EEPROM
#include <SPI.h> // RC522 Module uses SPI protocol
#include <MFRC522.h> // Library for Mifare RC522 Devices


#define COMMON_ANODE

#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif

#define redLed 7 // Set Led Pins
#define greenLed 6
#define blueLed 5

#define relay 4 // Set Relay Pin
#define wipeB 3 // Button pin for WipeMode, HOLD it then reset arduino for wipe mode.

boolean match = false; // initialize card match to false
boolean programMode = false; // initialize programming mode to false

int successRead; // Variable integer to keep if we have Successful Read from Reader

byte storedCard[4]; // Stores an ID read from EEPROM
byte readCard[4]; // Stores scanned ID read from RFID Module
byte masterCard[4]; // Stores master card's ID read from EEPROM

/*
We need to define MFRC522's pins and create instance
Pin layout should be as follows (on Arduino Uno):
MOSI: Pin 11 / ICSP-4
MISO: Pin 12 / ICSP-1
SCK : Pin 13 / ICSP-3
SS : Pin 10 (Configurable)
RST : Pin 9 (Configurable)
look MFRC522 Library for
other Arduinos' pin configuration
*/

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
//Arduino Pin Configuration
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
pinMode(relay, OUTPUT);
//Be careful how relay circuit behave on while resetting or power-cycling your Arduino
digitalWrite(relay, HIGH); // Make sure door is locked
digitalWrite(redLed, LED_OFF); // Make sure led is off
digitalWrite(greenLed, LED_OFF); // Make sure led is off
digitalWrite(blueLed, LED_OFF); // Make sure led is off

//Protocol Configuration
Serial.begin(9600); // Initialize serial communications with PC
SPI.begin(); // MFRC522 Hardware uses SPI protocol
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware

//If you set Antenna Gain to Max it will increase reading distance
//mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);

Serial.println(F("BlueCore Tech Acces Control")); // For debugging purposes
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details

//Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
Serial.println(F("Wipe Button Pressed"));
Serial.println(F("You have 5 seconds to Cancel"));
Serial.println(F("All records will be removed. This cannot be undone!"));
delay(5000); // Give user enough time to cancel operation
if (digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
Serial.println(F("CLEARING DATABASE"));
for (int x = 0; x < EEPROM.length(); x = x + 1) { //Loop end of EEPROM address
if (EEPROM.read(x) == 0) { //If EEPROM address 0
// do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
}
else {
EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
}
}
Serial.println(F("DATABASE Successfully Wiped"));
digitalWrite(redLed, LED_OFF); // visualize successful wipe
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
}
else {
Serial.println(F("Wiping Cancelled"));
digitalWrite(redLed, LED_OFF);
}
}
// Check if master card defined, if not let user choose a master card
// This also useful to just redefine Master Card
// You can keep other EEPROM records just write other than 143 to EEPROM address 1
// EEPROM address 1 should hold magical number which is '143'
if (EEPROM.read(1) != 143) {
Serial.println(F("No Master Card Set"));
Serial.println(F("Scan A RFID Card to Set as Master Card"));
do {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
delay(200);
digitalWrite(blueLed, LED_OFF);
delay(200);
}
while (!successRead); // Program will not go further while you not get a successful read
for ( int j = 0; j < 4; j++ ) { // Loop 4 times
EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
}
EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
Serial.println(F("Master Card Set"));
}
Serial.println(F("-------------------"));
Serial.println(F("Master Card's UID = "));
for ( int i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM
masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
Serial.print(masterCard[i], HEX);
}
Serial.println("");
Serial.println(F("-------------------"));
Serial.println(F("Everything Ready"));
Serial.println(F("Waiting for Keys or cards to be scanned"));
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
}


///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
do {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
if (programMode) {
cycleLeds(); // Program Mode cycles through RGB waiting to read a new card
}
else {
normalModeOn(); // Normal mode, blue Power LED is on, all others are off
}
}
while (!successRead); //the program will not go further while you not get a successful read
if (programMode) {
if ( isMaster(readCard) ) { //If master card scanned again exit program mode
Serial.println(F("Master Card Scanned"));
Serial.println(F("Exiting Programming Mode"));
Serial.println(F("-----------------------------"));
programMode = false;
return;
}
else {
if ( findID(readCard) ) { // If scanned card is known delete it
Serial.println(F("I know this key, removing..."));
deleteID(readCard);
Serial.println("-----------------------------");
}
else { // If scanned card is not known add it
Serial.println(F("I do not know this key, adding..."));
writeID(readCard);
Serial.println(F("-----------------------------"));
}
}
}
else {
if ( isMaster(readCard) ) { // If scanned card's ID matches Master Card's ID enter program mode
programMode = true;
Serial.println(F("Hello Master - Entered Programming Mode"));
int count = EEPROM.read(0); // Read the first Byte of EEPROM that
Serial.print(F("I have ")); // stores the number of ID's in EEPROM
Serial.print(count);
Serial.print(F(" record(s) in DATABASE"));
Serial.println("");
Serial.println(F("Scan a Card or key to ADD or REMOVE"));
Serial.println(F("-----------------------------"));
}
else {
if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
Serial.println(F("Welcome, Acces Granted"));
granted(300); // Open the door lock for 300 ms
}
else { // If not, show that the ID was not valid
Serial.println(F("Acces Denied!"));
denied();
}
}
}
}

///////////////////////////////////////// Access Granted ///////////////////////////////////
void granted (int setDelay) {
digitalWrite(blueLed, LED_OFF); // Turn off blue LED
digitalWrite(redLed, LED_OFF); // Turn off red LED
digitalWrite(greenLed, LED_ON); // Turn on green LED
digitalWrite(relay, LOW); // Unlock door!
delay(10000); // Hold door lock open for given seconds
digitalWrite(relay, HIGH); // Relock door
delay(1000); // Hold green LED on for a second
}

///////////////////////////////////////// Access Denied ///////////////////////////////////
void denied() {
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_ON); // Turn on red LED
delay(1000);
}


///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
int getID() {
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
// I think we should assume every PICC as they have 4 byte UID
// Until we support 7 byte PICCs
Serial.println(F("Scanned KEY's UID:"));
for (int i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}

void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x11)
Serial.print(F(" = BlueCore Tech. RFID Acces v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the RFID-MFRC522 properly connected?"));
while(true); // do not go further
}
}

///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
void cycleLeds() {
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
}

//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
void normalModeOn () {
digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
digitalWrite(relay, HIGH); // Make sure Door is Locked
}

//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
void readID( int number ) {
int start = (number * 4 ) + 2; // Figure out starting position
for ( int i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes
storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
}
}

///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
void writeID( byte a[] ) {
if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before!
int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
int start = ( num * 4 ) + 6; // Figure out where the next slot starts
num++; // Increment the counter by one
EEPROM.write( 0, num ); // Write the new count to the counter
for ( int j = 0; j < 4; j++ ) { // Loop 4 times
EEPROM.write( start + j, a[j] ); // Write the array values to EEPROM in the right position
}
successWrite();
Serial.println(F("Succesfully added ID record to DATABASE"));
}
else {
failedWrite();
Serial.println(F("Failed! There is something wrong with ID or bad DATABASE"));
}
}

///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
void deleteID( byte a[] ) {
if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
failedWrite(); // If not
Serial.println(F("Failed! There is something wrong with ID or bad DATABASE"));
}
else {
int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
int slot; // Figure out the slot number of the card
int start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
int looping; // The number of times the loop repeats
int j;
int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
slot = findIDSLOT( a ); // Figure out the slot number of the card to delete
start = (slot * 4) + 2;
looping = ((num - slot) * 4);
num--; // Decrement the counter by one
EEPROM.write( 0, num ); // Write the new count to the counter
for ( j = 0; j < looping; j++ ) { // Loop the card shift times
EEPROM.write( start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
}
for ( int k = 0; k < 4; k++ ) { // Shifting loop
EEPROM.write( start + j + k, 0);
}
successDelete();
Serial.println(F("Succesfully removed ID record from DATABASE"));
}
}

///////////////////////////////////////// Check Bytes ///////////////////////////////////
boolean checkTwo ( byte a[], byte b[] ) {
if ( a[0] != NULL ) // Make sure there is something in the array first
match = true; // Assume they match at first
for ( int k = 0; k < 4; k++ ) { // Loop 4 times
if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail
match = false;
}
if ( match ) { // Check to see if if match is still true
return true; // Return true
}
else {
return false; // Return false
}
}

///////////////////////////////////////// Find Slot ///////////////////////////////////
int findIDSLOT( byte find[] ) {
int count = EEPROM.read(0); // Read the first Byte of EEPROM that
for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
// is the same as the find[] ID card passed
return i; // The slot number of the card
break; // Stop looking we found it
}
}
}

///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
boolean findID( byte find[] ) {
int count = EEPROM.read(0); // Read the first Byte of EEPROM that
for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
return true;
break; // Stop looking we found it
}
else { // If not, return false
}
}
return false;
}

///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
// Flashes the green LED 3 times to indicate a successful write to EEPROM
void successWrite() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
}

///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
// Flashes the red LED 3 times to indicate a failed write to EEPROM
void failedWrite() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
}

///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
void successDelete() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
}

////////////////////// Check readCard IF is masterCard ///////////////////////////////////
// Check to see if the ID passed is the master programing card
boolean isMaster( byte test[] ) {
if ( checkTwo( test, masterCard ) )
return true;
else
return false;
}
Sensors voor arduino! http://arduinosensors.nl

Advertisement

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Arduino RFID-RC522 - DOOR ACCES CONTROL/ RFID DOOR LOCK

Berichtdoor shooter » 16 Mei 2016, 10:32

heel leuk, ik zag echter nog wel een hoop delays in je programma zitten , waardoor er langzaam gereageerd wordt, als je dat anders oplost waardoor de loop snel wordt gescand, zul je zien dat je herkenning veel sneller gaat,
paul deelen
shooter@home.nl

Berichten: 9
Geregistreerd: 12 Mei 2016, 01:43

Re: Arduino RFID-RC522 - DOOR ACCES CONTROL/ RFID DOOR LOCK

Berichtdoor arduinosensorsNL » 16 Mei 2016, 14:36

Hey, ja klopt.. maar de delays zijn alleen voor de leds. 1 delay is om je 5 seconden de tijd te geven om de database wipe te annuleren . En nog 1 van 10 seconden om de deur van slot te houden 10 seconden lang.

greetz
Sensors voor arduino! http://arduinosensors.nl

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Arduino RFID-RC522 - DOOR ACCES CONTROL/ RFID DOOR LOCK

Berichtdoor nicoverduin » 16 Mei 2016, 17:58

Nou dat is denk ik niet de impliciete boodschap die Paul je geeft. delay betekent dat dat ding gewoon niets doet. Door timers te gebruiken (zie blink_without_delay) kun je de code vele malen compacter maken en veel effectiever reageren op andere input signalen. Als er een delay loopt dan doet de processor verder helemaal niets. En moet je wachten totdat de delay voorbij is voordat je een volgende instructie uitvoert. Het is dus eigenlijk een "slechte" vorm van programmeren en "slecht aangeleerd' betekent hier meestal helaas "nooit meer afgeleerd".
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 9
Geregistreerd: 12 Mei 2016, 01:43

Re: Arduino RFID-RC522 - DOOR ACCES CONTROL/ RFID DOOR LOCK

Berichtdoor arduinosensorsNL » 16 Mei 2016, 20:28

Ja, daar ben ik mijzelf van bewust, Maar ben nog maar 3 maanden bezig met programmeren. Moet het inderdaad afleren, maar de meeste codes van de 43 die ik ondertussen geschreven heb zijn delay vrij. Het is gewoon dat de meeste mensen het verkeerd gebruiken en de eerste code waar ik van leerde had inderdaad delays. Deze code heb ik ook zelf niet geschreven trouwens. Heb gewoon dit project gedeeld omdat ik het heb gemaakt en de code wat heb aangepast omdat hij niet werkte voor vele mensen, en ook zelf niks werkende kon vinden voor mijn RC522 module.
Sensors voor arduino! http://arduinosensors.nl

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Arduino RFID-RC522 - DOOR ACCES CONTROL/ RFID DOOR LOCK

Berichtdoor shooter » 17 Mei 2016, 19:39

tja in blink zit delay en dat is het eerste programma van 90% dus het is niet raar, maar les twee is al blinken without delay.
paul deelen
shooter@home.nl

Terug naar Afgeronde projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 6 gasten