Created
September 27, 2016 14:56
-
-
Save ar1a/71ec0fad3f56d38a693c148830aaace2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <MFRC522.h> | |
#include <MD5.h> | |
#include <Servo.h> | |
const int servo_pin = 3; | |
const int servo_lock = 0; | |
const int servo_unlock = 180; | |
Servo lock; | |
//ss, reset | |
MFRC522 reader(10, 9); | |
char readCard[5]; | |
char* readHash; | |
char* generateHash(char* input) | |
{ | |
unsigned char* hash = MD5::make_hash(input); | |
char* md5 = MD5::make_digest(hash, 16); | |
free(hash); | |
return md5; | |
} | |
const unsigned int authorized_size = 2; | |
const char* authorized[authorized_size] = {"bf8c4bfcf2aad137ce4be664df92088f", "fc6c79494efb943d81ee94ece571ecfa"}; | |
int getID() { | |
// Getting ready for Reading PICCs | |
if ( ! reader.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue | |
return 0; | |
} | |
if ( ! reader.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue | |
return 0; | |
} | |
for (int i = 0; i < 4; i++) { // | |
readCard[i] = reader.uid.uidByte[i]; | |
} | |
readCard[5] = 0x00; //null terminate string | |
readHash = generateHash((char*)readCard); | |
Serial.println(readHash); | |
reader.PICC_HaltA(); // Stop reading | |
return 1; | |
} | |
void turn(int rot) | |
{ | |
lock.write(rot); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
SPI.begin(); | |
reader.PCD_Init(); | |
reader.PCD_DumpVersionToSerial(); | |
lock.attach(servo_pin); | |
turn(servo_lock); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
while (!getID()); | |
bool auth = false; | |
for (int i = 0; i < authorized_size; ++i) { | |
if (strcmp(authorized[i], readHash) == 0) { | |
auth = true; | |
} | |
} | |
if (!auth) return; // not authed so lets not unlock the door. | |
turn(servo_unlock); | |
delay(3000); //give us 3 seconds to open the door | |
turn(servo_lock); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment