jueves, 30 de abril de 2015

Write and Read RFID

The practice is to schedule an RFID card with a certain code that is common between cards.
To do this we must first program the cards and we need an RFID reader. We must first understand how these cards work, is an issue of a certain frequency, this frequency emission of the card is driven by a magnetic field generated by the RFID reader so that when a certain distance card reader, the card can emit that frequency and the reader depending on what we indicate now may accept or reject the card.
The materials need is the Arduino UNO card, RFID reader and cards.

Then we will have a brief description of the practice.
Read a card that works for RFID protocol read the identifier key A, read the result identifier Key B, which is the password that comes from the trailer factory located in each sector.
Once that is done to modify prompted the sector number to write and the block number. You can write in several blocks at a time but this example is done with only one block. This will block the password you want in numbers 0-255.
In our memory it is displayed in hexadecimal format. This practice is done because the UID (ID factory) can not be changed by this method. The card used is MFRC522 which has a 1K memory.
It has 64 blocks of 16 bytes. For each sector we have 4 blocks, block most weight is the one with the identifier and the other blocks serve as increment and decrement purse.
In block 0 sector 0 we have the ID card is unique and provided by the manufacturer. We can also see that there is a password on each sector located in block 3 of each sector, which can be modified, although it is not recommended as it can cancel the card and not to modify it while we used to compare the UID.
The protocol for this practice is SPI.
First we begin to explain how to program the cards. I connection.
SPI include the library and the MFRC522 card is used. We define the pin 10 as a slave pin and pin 09 as a reset, and declare a function to associate MFRC522 library what the SS and RST pins are.
Now for the setup, we started to see the serial data, the SPI protocol, started the MFRC522 reader and print as a control point.
Now is the loop. We created the object key (key that is inserted at the factory and is in every trailer block) the class is MFRC522 factory. For we identify with begins at byte 0 to 5 and are 0xFF. The IsNewCardPresent this serves only read a card because if we put two data pile up.
ReadCardSerial this serves so that if he was present and was reading not bunch up.
Then you choose the field of 16 there. The block number you put a name in this case valueBlockA and dedicated to block password block.
imageNow the reader must identify the data of the default password. First start with the key A. Status gives a value of 1 or 0, which makes authentication is the first thing that comes out of the class in this case is the key A, you say in that block is the key and the MFR. UID is the bookstore. It returns a value 0 if it goes below the authentication communicating, in the same way about the factory key B failed.
Now we will indicate what kind of key you want. Serial.print indicating create a text that shows us that will start recording our new key card. We create a byte constant size and is an array with the data we record we can write in the 16 fields in the array. The status returns 1 if written with this function, the first value we place the block number, followed by the constant and the number of data recording.
We record the effect of a Serial.print card.
imageNow we see a confirmation of having successfully recording the key. If you are not well recorded the message "La has cagado grabando" if we recorded well as leave the message "Has grabado Flaman". Then to display the data by the pour Serial data by serial to confirm that I recorded, the first thing is the UID, followed by the key and the entire sector, there are functions inside the library to display the block that we have recorded, or the whole card. Halta puncture this feature sends to terminate high transfer and would request if StopCrysptol card and is a function that the process ends.
Well we finished recording the card now is that we can read the card and give us the way to such a gateway.
The connections are the same what changes is the Sketch.
As explained below is performed for reading. Include the SPI libraries and MFRC522 as explained above, the SS and RST pin function library to determine the SS and RST pins, our key is an array and must be equal to that put to record cards otherwise it will not give way. I put two LEDs green and one red or refuse to make way for determining which is digital five green and red el`pin digital pin 4.
image We started with the setup, we start the Serial, SPI protocol, the MFRC522 card, print the serial we are waiting for a card, determine output LEDs and are therefore in low off.
Now for the loop. In the same way it is explained above is the protocol, first compares the key factory, if one is present card and if you are reading a card that does not bunch up.
We say that buffer is byte data type and is 18 bytes. Choose the sector, you have 16 sectors. The block number you put a name in this case dedicated to block valueBlockA password block.
We identified as explained above.
Now let's read in block 4 of the sector 1, determine the size for what we have in the buffer. We print the serial we are reading the card. Status returns Mifare_Read which determines the function returns 1, the first value put the block number, followed by the buffer, and the number of data to read.image 
With this for what we do is determine which index 0-16 go one by one. Reading print buffer to create the MIFARE_Read function and will put a 0 in front. Pass the index variable data to hexadecimal. We print the index in managed print the serial block read.

Compares the first and second byte and card reader. If it is all right because it indicates a text of "Pase hacia delante" and the green LED lights up intermittently.
image Otherwise shows "Prohibido el paso" and flashes the red LED.
image If status returns 0 determines you have not read well and put the following "The've screwed up reading", and instead as  “lectura correcta”
image And the end is the same as in the recording.
imageI attached two videos so you can see how it works by reading serial.

To download the files from all the practice you must click on the picture below.

No hay comentarios:

Publicar un comentario