PROJETO 28 - UTILIZANDO UM CARTÃO RFID

Olá pessoal, neste projeto iremos utilizar o módulo de identificação por rádio frequência o RFID-RC522 que você encontra aqui em nossa loja. Este projeto consiste em apenas visualizar os dados contidos no cartão. Posteriormente iremos acionar um rele a partir do código do cartão. Então mãos aos componentes !! Os códigos serão todos comentados para melhor entendimento, podendo ser retirados após a compreensão de cada linha. Bom trabalho !!!

Obs.: Para visualizar o Serial Monitor, basta ir em Tools e Serial Monitor ou pressionar Crtl+Shift+M e pronto.

COMPONENTES NECESSÁRIOS

1 Shield RFID-RC522

Fios jumpers do tipo macho/fêmea

CONECTANDO-SE AO MODULO RFID

1) Faça o download do arquivo Rfid_master em https://onedrive.live.com/?cid=F877FDED1DE63FAA&id=F877FDED1DE63FAA%21122;

2) Após feito o download, descompacte o arquivo;

3) Com o arquivo descompactado, vá em (C:) e na pasta Arquivos de Programas, Arduino, libraries, copie o arquivo descompactado nesta pasta e pronto.

CONECTANDO OS COMPONENTES

SEGUE O ESQUEMÁTICO ABAIXO PARA MELHOR ENTENDIMENTO:

Pinos do módulo RFID-RC522          Pinos do Arduino
RST                                                          9
SDA                                                         10
MOSI                                                       11
MISO                                                       12
SCK                                                         13
GND                                                        GND
VCC                                                         3.3V

AGORA VAMOS AO CÓDIGO

// Biblioteca do Serial Peripheral Interface (Interface Periférica Serial).
#include <SPI.h>
#include <MFRC522.h> // Biblioteca do módulo RFID-RC522.

#define SS_PIN 10 // Define o pino 10 como Escravo.
#define RST_PIN 9 // Define o pino 9 como Reset.
MFRC522 mfrc522(SS_PIN, RST_PIN); // Cria um objeto MFRC522.

void setup() {
Serial.begin(9600); // Inicializa a comunicação serial com o PC.
SPI.begin(); // Inicializa o SPI bus.
mfrc522.PCD_Init(); // Inicializa o cartão MFRC522.
// Imprime no Serial Monitor a frase entre " ".
Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
}

void loop() {
// Procura por novos cartões.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}

// Seleciona um dos cartões.
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Agora um cartão é selecionado. O UID e SAK estão no mfrc522.unis.

// Descarrega UID.
Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();

// Descarrega PICC.
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.print("PICC type: ");
Serial.println(mfrc522.PICC_GetTypeName(piccType));
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println("This sample only works with MIFARE Classic cards.");
return;
}

// Orepara a chave - todas as chaves estão configuradas para...
//FFFFFFFFFFFFh (Padrão de fábrica).
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}

// Nesta amostra nos usamos o segundo setor (bloco 4-7).
byte sector = 1;
byte valueBlockA = 5;
byte valueBlockB = 6;
byte trailerBlock = 7;
// Valida usando a chave A.
Serial.println("Authenticating using key A..."); // Imprime no Serial Monitor.
byte status;
status = mfrc522.PCD_Authenticate
(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Precisamos de um setor que define blocos 5 e 6 com valores de blocos...
//e que permita a chave B.
byte trailerBuffer[] = { 255,255,255,255,255,255, 0,0,0,
0, 255,255,255,255,255,255}; // Mantem por teclas padrão.

mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3);
// Agora nos lemos o setor e vemos se isto ista como nos queremos...
//que seja.
Serial.println("Reading sector trailer...");
byte buffer[18];
byte size = sizeof(buffer);
status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
if ( buffer[6] != trailerBuffer[6]
&& buffer[7] != trailerBuffer[7]
&& buffer[8] != trailerBuffer[8]) {
Serial.println("Writing new sector trailer..."); // Imprime no Serial Monitor.
status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
}
// Valida usando chave B.
// Imprime no Serial Monitor.
Serial.println("Authenticating again using key B...");
status = mfrc522.PCD_Authenticate
(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
// Se haver falha na autenticação uma mensagem será exibida no...
//Serial Monitor.
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

/*OBS.: O valor dos blocos tem um valor com sinal de 32 bit armazenado
três vezes e um endereço de 8 bits armazenados 4 vezes. Certifique-se
de blocos valueBlockA e valueBlockB tem esse formato: formatBlock
(alueBlockA); */

// Adciona 1 no valor de valueBlockA e armazena o resultado em...
//valueBlockA.
Serial.print("Adding 1 to value of block "); Serial.println(valueBlockA);
status = mfrc522.MIFARE_Increment(valueBlockA, 1);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Increment() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Transfer(valueBlockA);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Transfer() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

// Descarrega o resultado.
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid),
&key, sector);

// Pausa PICC
mfrc522.PICC_HaltA();

// Para a criptografia em PCD.
mfrc522.PCD_StopCrypto1();
}

void formatBlock(byte blockAddr) {
Serial.print("Reading block "); Serial.println(blockAddr);
byte buffer[18];
byte size = sizeof(buffer);
byte status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

if ( (buffer[0] == (byte)~buffer[4])
&& (buffer[1] == (byte)~buffer[5])
&& (buffer[2] == (byte)~buffer[6])
&& (buffer[3] == (byte)~buffer[7])
&& (buffer[0] == buffer[8])
&& (buffer[1] == buffer[9])
&& (buffer[2] == buffer[10])
&& (buffer[3] == buffer[11])
&& (buffer[12] == (byte)~buffer[13])
&& (buffer[12] == buffer[14])
&& (buffer[12] == (byte)~buffer[15])) {
Serial.println("Block has correct Block Value format.");
}
else {
Serial.println("Writing new value block...");
byte valueBlock[] = { 0,0,0,0, 255,255,255,255, 0,0,0,0, blockAddr,~blockAddr,
blockAddr,~blockAddr };
status = mfrc522.MIFARE_Write(blockAddr, valueBlock, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
}
} // Fim do formatBlock()


Obs.: Para certificar se o código está correto pressione o botão Verify/Compile. Se tudo estiver correto pressione o botão Upload para fazer o upload do código para seu Arduino. Pronto, agora você pode passar o cartão sobre o leitor do módulo e você verá uma imagem igual a descrita abaixo:

 

fonte: facacomarduino.info