Rfid Programado para acender Led, Arduino, Leitor Rfid MFRC522
Autor: Vilmar Junior | Publicado: 2016-10-23T10:38:00-07:00







Depois de um tempo de pesquisa finalmente achei um código para o leitor Rfid que eu pudesse transformar em uma fechadura eletrônica, por enquanto ele apenas acende um LED verde para o tag conhecido e um Led vermelho para o tag desconhecido.

aqui está o código completo, inclusive com um monitor OLED, que no meu caso não usei, usei o monitor serial da janela do arduino para visualizar os códigos dos tags.

#include &lt;MFRC522.h&gt;
#include &lt;Adafruit_GFX.h&gt;
#include &lt;Adafruit_SSD1306.h&gt;
#include &lt;SPI.h&gt;


#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define SS_PIN 10
#define RST_PIN 9


MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code[]= {53,66,217,101};
int codeRead = 0;
String uidString;
void setup() {

&nbsp;
&nbsp; Serial.begin(9600);
&nbsp; SPI.begin(); // Init SPI bus
&nbsp; rfid.PCD_Init(); // Init MFRC522
&nbsp;
&nbsp; display.begin(SSD1306_SWITCHCAPVCC, 0x3C); &nbsp;// initialize with the I2C addr 0x3D (for the 128x64)

pinMode(7,OUTPUT);
pinMode(8,OUTPUT);

&nbsp; // Clear the buffer.
&nbsp; display.clearDisplay();
&nbsp; display.display();
&nbsp; display.setTextColor(WHITE); // or BLACK);
&nbsp; display.setTextSize(2);
&nbsp; display.setCursor(10,0);
&nbsp; display.print("RFID Lock");
&nbsp; display.display();
&nbsp;


}

void loop() {
&nbsp; if( &nbsp;rfid.PICC_IsNewCardPresent())
&nbsp; {
&nbsp; &nbsp; &nbsp; readRFID();
&nbsp; }
&nbsp; delay(100);

}

void readRFID()
{
&nbsp;
&nbsp; rfid.PICC_ReadCardSerial();
&nbsp; Serial.print(F("\nPICC type: "));
&nbsp; MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
&nbsp; Serial.println(rfid.PICC_GetTypeName(piccType));

&nbsp; // Check is the PICC of Classic MIFARE type
&nbsp; if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &amp;&amp; 
&nbsp; &nbsp; piccType != MFRC522::PICC_TYPE_MIFARE_1K &amp;&amp;
&nbsp; &nbsp; piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
&nbsp; &nbsp; Serial.println(F("Your tag is not of type MIFARE Classic."));
&nbsp; &nbsp; return;
&nbsp; }

&nbsp; &nbsp; clearUID();
&nbsp; 
&nbsp; &nbsp; Serial.println("Scanned PICC's UID:");
&nbsp; &nbsp; printDec(rfid.uid.uidByte, rfid.uid.size);

&nbsp; &nbsp; uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);
&nbsp; &nbsp;
&nbsp; &nbsp; printUID();

&nbsp; &nbsp; int i = 0;
&nbsp; &nbsp; boolean match = true;
&nbsp; &nbsp; while(i&lt;rfid.uid.size)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; if(!(rfid.uid.uidByte[i] == code[i]))
&nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;match = false;
&nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; i++;
&nbsp; &nbsp; }

&nbsp; &nbsp; if(match)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; Serial.println("\nI know this card!");
&nbsp; &nbsp; &nbsp; printUnlockMessage();
&nbsp; &nbsp; &nbsp; digitalWrite (7,HIGH);
&nbsp; &nbsp; &nbsp; delay (1500);
&nbsp; &nbsp; &nbsp; digitalWrite (7,LOW);
&nbsp; &nbsp; }else
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; Serial.println("\nUnknown Card");
&nbsp; &nbsp; &nbsp; digitalWrite (8,HIGH);
&nbsp; &nbsp; &nbsp; delay (1500);
&nbsp; &nbsp; &nbsp; digitalWrite (8,LOW); 

&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; }


&nbsp; &nbsp; // Halt PICC
&nbsp; rfid.PICC_HaltA();

&nbsp; // Stop encryption on PCD
&nbsp; rfid.PCD_StopCrypto1();
}

void printDec(byte *buffer, byte bufferSize) {
&nbsp; for (byte i = 0; i &lt; bufferSize; i++) {
&nbsp; &nbsp; Serial.print(buffer[i] &lt; 0x10 ? " 0" : " ");
&nbsp; &nbsp; Serial.print(buffer[i], DEC);
&nbsp; }
}

&nbsp; void clearUID()
&nbsp; {
&nbsp; &nbsp; display.setTextColor(BLACK); // or BLACK);
&nbsp; &nbsp; display.setTextSize(1);
&nbsp; &nbsp; display.setCursor(30,20);
&nbsp; &nbsp; display.print(uidString);
&nbsp; &nbsp; display.display();
&nbsp; }

&nbsp; void printUID()
&nbsp; {
&nbsp; &nbsp; display.setTextColor(WHITE); // or BLACK);
&nbsp; &nbsp; display.setTextSize(1);
&nbsp; &nbsp; display.setCursor(0,20);
&nbsp; &nbsp; display.print("UID: ");
&nbsp; &nbsp; display.setCursor(30,20);
&nbsp; &nbsp; display.print(uidString);
&nbsp; &nbsp; display.display();
&nbsp; }

&nbsp; void printUnlockMessage()
&nbsp; {
&nbsp; &nbsp; display.display();
&nbsp; &nbsp; display.setTextColor(BLACK); // or BLACK);
&nbsp; &nbsp; display.setTextSize(2);
&nbsp; &nbsp; display.setCursor(10,0);
&nbsp; &nbsp; display.print("RFID Lock");
&nbsp; &nbsp; display.display();
&nbsp; &nbsp;
&nbsp; &nbsp; display.setTextColor(WHITE); // or BLACK);
&nbsp; &nbsp; display.setTextSize(2);
&nbsp; &nbsp; display.setCursor(10,0);
&nbsp; &nbsp; display.print("Unlocked");
&nbsp; &nbsp; display.display();
&nbsp; &nbsp;
&nbsp; &nbsp; delay(2000);
&nbsp; &nbsp;
&nbsp; &nbsp; display.setTextColor(BLACK); // or BLACK);
&nbsp; &nbsp; display.setTextSize(2);
&nbsp; &nbsp; display.setCursor(10,0);
&nbsp; &nbsp; display.print("Unlocked");

&nbsp; &nbsp; display.setTextColor(WHITE); // or BLACK);
&nbsp; &nbsp; display.setTextSize(2);
&nbsp; &nbsp; display.setCursor(10,0);
&nbsp; &nbsp; display.print("RFID Lock");
&nbsp; &nbsp; display.display();
&nbsp; }