Etiquetas

jueves, 17 de septiembre de 2015

RFID

RFID




RFID (Radio Frequency Identification acronym in Spanish Radio Frequency Identification) is a system for storing and remotely retrieving data using labels, cards, transponders or devices called RFID tags. The fundamental purpose of RFID technology is transmitting the identity of an object (like a unique serial number) using radio waves. RFID technologies are grouped into so-called Auto ID (automatic identification, or automatic identification).

RFID Tag or Tags are few, similar to a sticker which can be attached or incorporated into a product, animal or person small devices.

They contain antennas to enable them to receive and respond to requests by radio from an RFID transceiver. Passive tags require no internal power supply, while lending itself require. One advantage of using radio frequency (instead, for example, infrared) is not sight between transmitter and receiver is required.

The mode of operation of RFID systems is simple. The RFID tag containing identification data of the object to which it is attached, generating a radio frequency signal with that data. This signal can be detected by an RFID reader, which is responsible for reading the information in digital format and pass it to the specific application using RFID.


An RFID system consists of the following three components:

· RFID tag or transponder: composed of an antenna, a radio and an encapsulated transducer or chip material. The purpose of the antenna is to allow the chip, which contains the information, transmitting the identification information of the label. Several types of labels. The chip has an internal memory with a capacity depends on the model and ranges from a dozen to thousands of bytes. There are several types of memory:

· Read Only: the identification code it contains is unique and is customized for the manufacture of the label.

· Read and Write: identification information can be changed by the reader.

· Collision. These are special tags that allow a reader to identify several at once (usually labels must enter one by one in the coverage area of ​​the reader).

· RFID reader or transceiver: composed of an antenna, a transceiver and decoder. The reader sends signals periodically to see if any tags in its vicinity. When picking up a signal from a label (which contains the identification information of this), it extracts the information and passes the data processing subsystem.

· Data processing subsystem or RFID Middleware provides the means of data processing and storage.

· Electromagnetic induction is the phenomenon that causes the production of an electromotive force (emf or voltage) in a medium or body exposed to a variable magnetic field or a moving means relative to a static magnetic field. Thus, when said body is a conductor, an induced current is produced. This phenomenon was discovered in 1831 porMichael Faraday, who expressed indicating that the magnitude of the induced voltage is proportional to the change in magnetic flux (Faraday's Law).


Practice with RFID


The following practice we have done is to schedule an RFID card with a code that allows via a RFID reader validate the code and allow access.
The materials needed are an Arduino UNO card, a reader and RFID cards and some colored LEDs to indicate and the code is right or wrong.



For this practice will write two codes, one for writing to write the code on the cards that will use and other code reading in which we will read the code on the card introduced by the RFID reader.

Writing Code


/*
  Autor: Ángel Barquín                         Fecha: 8-5-2015
  Programa: rfid                               Versión: 1.0
  Dispositivo: ATMEL 328                       Compilador: AVR
  Entorno ID: 1.5.5-r2                         Simulador: VSM
  Tarjeta de aplicación: Arduino
 
  The program writes a particular block of a sector, and displays the values written.
  RFID used is RFID-RC522

   The pins are connected as follows 
  

  Signal     Pin              Pin
              Arduino Uno     MFRC522 board
  ------------------------------------------------
  Reset      9                RST
  SPI SS     10               SDA
  SPI MOSI   11               MOSI
  SPI MISO   12               MISO
  SPI SCK    13               SCK
 */
 
  #include <SPI.h>
  #include <MFRC522.h>
 
  #define SS_PIN 10   // definition of slave select pin.
  #define RST_PIN 9   / /reset pin definition.
  MFRC522 mfrc522(SS_PIN, RST_PIN);        //522. RSFF object creation must introduce SS pins and    RST defined above.
 
  byte datosEscribir[] = { 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4 };    //data we will write
  byte sector = 5;           //choose the sector in which we will write (0-16)
  byte numeroBloque= 22;      //block number that we will read
  byte bloqueContrasena= 23;  //block dedicated to the password
 
  void setup()
  {
    Serial.begin(9600);        // initialize the serial
    SPI.begin();               // initialize the spi
    mfrc522.PCD_Init();        // initialize the device MFR522
  }
 
  void loop()
  {
    MFRC522::MIFARE_Key key;
    for (byte i = 0; i < 6; i++) // we define the key, which defaults to FFFFFFFFFF, it is necessary that this written in the last block of each sector.
    {                            // if we change the value of the key, the card stops working
      key.keyByte[i] = 0xFF;
    }
    //The following functions are necessarily required because it makes a number of checks needed
    //learn to identify the card type and check if the connection is correct
    if ( ! mfrc522.PICC_IsNewCardPresent()) //checks for a new card
    {
      return;//if no new card, leaves and recheck if all
    }
    if ( !mfrc522.PICC_ReadCardSerial()) //with a card that read, we get avoid collioson between two or more cards present.
    {
      return;//if you can not read the card, if leaves and double check all
    }
   
   
    Serial.print("UID of the card: ");//reed the UID (Unique Identification Number) of the card and we show by serial.
    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();
 
         
    byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);// reads and returns by serial card type
    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)
    {
      return;//if the card type is wrong, out of the if and double check all
    }

    byte status;
   
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueContrasena, &key, &(mfrc522.uid));//Comprobamos la key A
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
   
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, bloqueContrasena, &key, &(mfrc522.uid));//check the key B
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
   
    Serial.println("writing.. ");
   
    status = mfrc522.MIFARE_Write(numeroBloque, datosEscribir, 16);  //Write in the specified block, the specified data and 16 bytes
                 
    Serial.println("written values: ");
   
      // for,is used to repeat a block of statements enclosed in braces, there are three parts for (initialization; condition; increment)
      // It is useful for any repetitive operation. The initialization happens first and exactly once. Each time through the loop,
       // The condition is tested; if true, the statement block, and the increase is executed, then the condition is tested again.
       // When the condition becomes false, the loop exits. 
    for(int i=0;i<16;i++)
    { 
      Serial.print(datosEscribir[i]); //serial show by the values we have written
      Serial.print(" ");
    }
     // if,It used in conjunction with a comparison operator, if the statement in parentheses is true
      // Your statements bracketed run. If not, the program skips over the code.        
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("MIFARE_Write() failed: ");    //warns if the writing has failed
      Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.println();
    Serial.println("finalized");
    Serial.println();
    Serial.println("waiting card");
           
    mfrc522.PICC_HaltA();//It is terminated reading
    mfrc522.PCD_StopCrypto1();//The process ends
        
  }






Code read


/*
  Autor: Ángel Barquín                         Fecha: 8-5-2015
  Programa: ethernet mas temperatura           Version: 1.0
  Dispositivo: ATMEL 328                       Compilador: AVR
  Entorno ID: 1.5.5-r2                         Simulador: VSM
  Tarjeta de aplicacion: Arduino
 
 
The program reads a particular block of a sector serial sample values check the read are the same as the password
   RFID used is RFID-RC522

   The pins are connected as follows

  Signal     Pin              Pin
             Arduino Uno      MFRC522 board
  --------------------------------------------
  Reset      9                RST
  SPI SS     10               SDA
  SPI MOSI   11               MOSI
  SPI MISO   12               MISO
  SPI SCK    13               SCK
 */
 

  #include <SPI.h>   //SPI include the library
  #include <MFRC522.h>  //MFRC522 include the library
  #define SS_PIN 10  //We define the slave select pin.
  #define RST_PIN 9  //We define the reset pin.
  #define led_rojo 6  //We define the red LED
  #define led_verde 7  //We define the green LED
  MFRC522 mfrc522(SS_PIN, RST_PIN);   //Mfrc522 creation of the object. We must introduce the SS pin and RST defined above.
 
  byte clave[]={1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4}; //Password user-defined

 
  byte buffer[18];             //We define the buffer size has to be 18
  byte sector= 5;              //You choose the area you are going to read (0-16)
  byte numeroBloque= 22;        //block number that we will read
  byte bloqueContrasena= 23;    //dedicated to block password (multiple of 4)
 
  void setup()
  {
    pinMode(led_rojo, OUTPUT);
    pinMode(led_verde, OUTPUT);
    Serial.begin(9600);
    SPI.begin();                  // Start the SPI protocol
    mfrc522.PCD_Init();                             // Start the device MFR522
    Serial.println("Waiting card reading"); // alerts the customer is waiting for a card approach
  }
 
  void loop()
  {
    boolean b=true;
    MFRC522::MIFARE_Key key;
    for (byte i = 0; i < 6; i++)          //We define the key, which defaults to FFFFFFFFFF, it is necessary that this written in the last block of each sector.
    {                                     //if we change the value of the key, the card stops working
      key.keyByte[i] = 0xFF;
    }
         
    if ( !mfrc522.PICC_IsNewCardPresent()) //checks for a new card
    {
      return;//if there is no card back up the loop until it finds card
    }     
    if ( !mfrc522.PICC_ReadCardSerial()) //With this card reads only we manage to avoid the collision between two or more cards present.
    {     
      return;//if the card does not read back to the beginning
    }
         
    byte status;
         
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueContrasena, &key, &(mfrc522.uid));//Comprobamos la key A
   
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
   
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, bloqueContrasena, &key, &(mfrc522.uid));//check the key B

    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
                 
    byte tamanoBuffer = sizeof(buffer);     
    status = mfrc522.MIFARE_Read(numeroBloque,buffer,&tamanoBuffer);//We read the data and store in buffer
    Serial.println("Leyendo...");//We indicated that we are reading
    
    for (int i = 0; i< tamanoBuffer-2 ; i++)
    {
      Serial.print(buffer[i], HEX);  //We write in the serial data read
      Serial.print(" ");
    }
    Serial.println();     
          
    for (int i=0; i<tamanoBuffer-2 ;i++) //We check if the read value is equal to the password
    {
      if (clave[i]!=buffer[i])// si la clave no es igual al valor
      {
        //Serial.println("1 good value");           
        b=false;
        Serial.println("Access denied");
        digitalWrite(led_rojo, HIGH);  // turn on the red light
        digitalWrite(led_verde, LOW);  // turn off the green LED
        delay(2000);
        digitalWrite(led_rojo, LOW);  // We turn off the red light after 2 seconds
        break;
      }
    }
    if(b == true) //si b=true, if the values read are equal to those of the serial password show that applies comprobración
    {
      Serial.println("Access granted");
      digitalWrite(led_rojo, LOW);  // turn off the green LED
      digitalWrite(led_verde, HIGH);  // turn on the green LED
      delay(2000);
      digitalWrite(led_verde, LOW); //the green LED turn off after 2 seconds
    }
   
    Serial.println();
    Serial.println("Waiting for Next Card");

    mfrc522.PICC_HaltA();// It is terminated reading
    mfrc522.PCD_StopCrypto1();//The process ends
  }







No hay comentarios:

Publicar un comentario