Microcontroller Tutorials

Arduino, Raspberry Pi, PIC, Beaglebone Resources

RFID Login System with Raspberry Pi

Wouldn't it be cool if you build your own card login system just like in hotels? Because RFID modules are now easy to get, this is now very possible. This Raspberry Pi RFID project of mine is a simple login system that reads cards and tags. Every time a user taps his or her card, his name and current date and time is saved to a SQL database. This project could be useful for login systems in establishments. And by using a Raspberry Pi, a lot of possible features could be added. 

Materials

I will be using the popular MFRC522 RFID module:

My module comes with a card and a tag plus some male headers. This module can be interfaced using UART, I2C or SPI. I will be using SPI with Python for this project following this RFID Raspberry Pi tutorial.

Powering the project is a Raspberry Pi 2 B+. However, it's compatible with older and newer Pi's as long as they are running the latest Raspbian Stretch (March 2018).  

Other necessary things needed for this project are female-to-female jumper wires and an internet connection.

Building Instructions

Enable SPI
As mentioned, I communicated with the RFID module through SPI. Since SPI is not enabled by default on the RPi, I needed to turn it on via raspi-config:

In the raspi-config interface, I selected Interfacing Options and then selected SPI. After answering yes to the prompt, I rebooted the RPi to save changes.

I needed to check if SPI is indeed enabled. This is done by opening config.txt

Then I searched for the line that says:

If this line is commented-out, then SPI is now enabled. 

SPI for Python

There are a number of handy python library for using the SPI interface.  I used SPI-Py. To download this library, just enter the commands:

Once downloaded, cd to the folder and run the included setup.py script

This installs the SPI-Py library and is now usable in Python environment.

Installing the RFID Library

My Raspberry Pi is now SPI ready! Next, I installed the RFID library by Mario Gomez.

This library uses the following connection:

I've created a Fritzing diagram for those who prefer it:

To test if everything's working as expected, I ran the included Read.py script:

Tapping the included RFID card shows the UID, size and data for a specific sector for that card. It's looks like this:

This library allows me to view sector 8 which has 16 bytes of free space. I can use this space to save data but for now, I will be only using the UID as a unique identifier for the card.

Installing MySQL to Save Data

To complete my login system, I need to install mySQL and modify the Read.py code so that the time and name of the user is saved to a database every time the card is tapped into the module.

Here's how I installed mySQL server:

Once installed, I needed to create a database with two tables: user and log. The user table records all RFID card uid and the corresponding user names. The log table records the time and date when a user taps a card. 

After the database and tables are created, I inserted dummy data to the user table.

[/crayon]

To be able to access the mySQL database through Python, I installed python-mysqldb:

That's about it for the software prerequisites.  Since the system logs current date and time, my Raspberry Pi must be synced with my local time. This is done automatically in newer Raspbian distributions. I followed these steps:

1. sudo raspi-config

2. Internationalization options

3. Change Time Zone

4. Select geographical area

5. Select city or region.

6. Reboot your pi.

Now, i just needed to edit Read.py to this:

There are other things printed on the terminal that are not useful for this project. Thus, I decided to edit the MFRC522.py script. You can download my version of this script on my repository.

If you want to replicate this project, all the files are found here. Just follow all the steps here for the software requirements. Note that the edited Read.py is now named Login.Py. To run, just cd to the folder I linked to and run this command:

I've created a video showing the steps for running the project. Note that it's assumed that you have already installed the software prerequisites as presented above.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.