Contoh Kode Arduino Akses Pintu dengan RFID

Untuk Teori dasar RFID  mifare bisa dibaca disini
#include <SPI.h>
#include <MFRC522.h>

#define selenoid 4
#define RST_PIN 9
#define SS_PIN 10
#define jumlahdatabaseTAG 5

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

MFRC522::MIFARE_Key key; // prepare struct key for authentification

/**
* Initialize.
*/

char* databaseTAG[] =
{
“123456789ABCDE1”,// Tag 0
“123456789ABCDE2”,// Tag 1
“123456789ABCDE3”, // Tag 2
“123456789ABCDE4”, // Tag 3
“123456789ABCDE5”, // Tag 4
“123456789ABCDE6”, // Tag 5
“123456789ABCDE7”, // Tag 6
“123456789ABCDE8”, // Tag 7
“123456789ABCDE9”, // Tag 8
“123456789ABCDEF”, // Tag 9
};

char* Name[] =
{
“abdullah”, // Tag 1
“dodi”, // Tag 2
“uus”,
“yudi”,
“mario”,
“usman”,
“fatih”,
“fayiz”,
“herman”,
“dede”,

// Tag 3
};

int led1 = 5;
int led2= 6;
int pintu = 7;
int buzzer = 8;

void setup() {

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(pintu, OUTPUT);
pinMode(buzzer, OUTPUT);

Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();

// Prepare the key: 0xff 0xff 0xff 0xff 0xff 0xff
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}

}

//===============================================
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;

// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
Serial.print(“Card UID:”);
kirimkePC(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
int tagNo =0;
byte sector = 1;
byte blockAddr = 4;
byte trailerBlock = 7;
byte status;
byte buffer[18];
byte size = sizeof(buffer);

char bufferchar[16];
byte sizechar = sizeof(bufferchar);

status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(“Authenticate failed: “);
return;
}

// Read block
Serial.print(“hasil baca blok = “); Serial.print(blockAddr);
Serial.println();

status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK)
{
Serial.print(“Read failed”);
return;
}

kirimkePC(buffer, 16); Serial.println();

//konversi buffer dari byte ke string
for(byte j=0;j<16;j++)
{
bufferchar[j]=buffer[j];
}

bufferchar[15]=0x00; // string adalah array char yg diakhiri NULL (0x00)

Serial.print(bufferchar);
Serial.println();

//compare string hasil baca dengan string di database
tagNo=0xff;
for(byte i=0;i<10;i++)
{
if(strcmp(bufferchar, databaseTAG[i]) == 0)
{
tagNo=i;
}
}

if(tagNo !=0xff) // TAG cocok
{
Serial.print(“kunci terbuka”);Serial.println();
Serial.print(“selamat datang : “);
Serial.print(Name[tagNo]);
Serial.println();
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(pintu, HIGH);
delay(2000);
digitalWrite(led1, LOW);

}
else // TAG tdk cocok
{
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(buzzer, HIGH);
delay(100);
Serial.print(“anda tak berhak masuk”);
Serial.println();
}

mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
} // end of main loop
//========================================================

void kirimkePC(byte *buffer, byte bufferSize)
{
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? ” 0″ : ” “);
Serial.print(buffer[i], HEX);
}
}

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 16/07/2016, in Arduino, Contoh-contoh Aplikasi, Daftar isi. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: