Blog 2 – Just the UID

So in the last blog I talked about getting the RFID reader working with the Arduino. I also confirmed that the student ID cards were the Mifare type and they work with the reader. I used the Dumpinfo example that came with the Library for the reader to pull all the information stored on the card off. As you can see it can be quite a long process.
Note: I have blanked out the UID as it is private information.

Dumpinfo.gif

Although this could be useful, it is not necessary for my project. I only need the UID of the card when I scan it. To read and print out all the data that is stored on the card takes about 5 to 10 seconds but to get just the UID it is almost instant, as it happens first.

Most of the videos and documents surrounding the MFRC522 that are online just cover the Dumpinfo example. I did find however this website; http://duinos.net/show/?id=220. There is a video and a file on the site of a guy making a door unlock system. For this system he just needed the UID as well. I reviewed his video and the file which he uploaded and figured out how he was just getting the UID to print.

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above

String read_rfid;

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

void setup() {
 Serial.begin(9600); // Initialize serial communications with the PC
 while (!Serial); // Do nothing if no serial port is opened
 SPI.begin(); // Init SPI bus
 mfrc522.PCD_Init(); // Init MFRC522
 mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD
 Serial.println(F("Scan card to see UID"));
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
 read_rfid="";
 for (byte i = 0; i < bufferSize; i++) {
 read_rfid=read_rfid + String(buffer[i], HEX);
 }
}

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

// Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial()) {
 return;
 }
 
 dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
 Serial.println(read_rfid);
}

The above code works great. It reads just the UID and prints it. It is almost instantaneous from when the card is held close enough. There is one small issue though. Because the Arduino works using a loop, once it has read and printed the UID it starts again. This means that it just keeps reading and printing out the UID until the card is removed. This isn’t useful to me so I modified it again and added in a if statement. It reads the UID, prints it and stores it in a variable. When the loop starts again it reads it and then checks the IF statement. If the UID just scans matches the UID that is in the variable then it doesn’t print the UID. The loop then starts again.

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above

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

String read_rfid;
String rfid_uid;

/*
 * Initialize.
 */
void setup() {
 Serial.begin(9600); // Initialize serial communications with the PC
 while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
 SPI.begin(); // Init SPI bus
 mfrc522.PCD_Init(); // Init MFRC522 card
 mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
 Serial.println(F("Scan PICC to see UID"));
 rfid_uid="";
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
 read_rfid="";
 for (byte i = 0; i < bufferSize; i++) {
 read_rfid=read_rfid + String(buffer[i], HEX);
 }
}

void loop() {

 // Look for new cards
 if ( ! mfrc522.PICC_IsNewCardPresent())
 return;

 // Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial())
 return;
 
 dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
 
 if ( read_rfid != rfid_uid) {
 if (read_rfid != rfid_uid) {
 Serial.println(read_rfid);
 Serial.println(F("Scan Next Card"));
 rfid_uid = read_rfid;
 }
 }
}

Next I need to start looking at connecting the Arduino to the cloud, so that I can send the UID to the Database. Before I start that I need to setup a test environment in the cloud. This will be my next blog.

Advertisements

Leave a Reply

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