Introduction: How to Build Raspberry Pi RFID Attendance System

In this tutorial, I will show you How to build Raspberry pi RFID attendance system complete process, Below are some of the things you need to know to complete this project

How to Setup MYSQL Database and PHPMyAdmin on the Raspberry Pi and I will try to give you an idea on this subject after which I will go to see the main project

For Raspberry pi RFID attendance system project, you have to need flowing Equipment

    1. Raspberry Pi

    1. Micro SD Card

    1. Power Supply

    1. RC522 RFID Reader

    1. Breadboard

    1. Breadboard Wire

This book will help you to gain more knowledge about Raspberry Pi

Setting up MYSQL on a Raspberry pi RFID attendance system Project:

Before we get started with installing MySQL to our Raspberry Pi, we must first update our package list and all installed packages. running the following two commands.

sudo apt update
sudo apt upgrade
The next step is to install the MySQL server software for your Raspberry Pi.
running the following commands Installing MySQL to the Raspberry Pi is
sudo apt install mariadb-server

Now you have to need to secure it by setting a password for the “root” user.

By default, MySQL is installed without any password set up meaning you can access the MySQL server without any authentication.

Run the following command to begin the MySQL securing process.

sudo mysql_secure_installation
Just follow the prompts to set a password for the root user and to secure your MySQL installation. For a more secure installation, you should answer “Y” to all prompts if you want to access your Raspberry Pi’s MySQL server and start making changes or something work your databases, you can enter the following command.
sudo mysql -u root -p

You can now enter MYSQL commands to create, alter, and delete databases. Through this interface, you can also create or delete users and assign them the right to manage any database. There are two different ways you can quit out of the MYSQL command line, the first of those is to type “quit” into the MySQL interface. The other way of quitting out of the MYSQL command line is to press Ctrl + D. Ok great, you will now have successfully set up MySQL on your Raspberry Pi.

Now time to Install PHPMyAdmin on the Raspberry PiTo install the PHPMyAdmin package to our Raspberry Pi, we need to run the command below.
<span style="font-size: small; font-weight: normal;">sudo apt install phpmyadmin</span>

You have to need to configure PHPMyAdmin to connect to our MYSQL server. We will also need to set up some details so that we can log in to the PHPMyAdmin software.

To do this select “<Yes>” at them all next prompt.

It will now ask you to set a password for PHPMyAdmin itself. It is best to set this password to something different to your root SQL password. Doing this will help secure the server. This password is what PHPMyAdmin will use to connect to the MySQL server.

To do this, we will need to first login to the MySQL command-line interface using the “root” user with the password you set up.
sudo mysql -u root -p

Now time to run the command below to create a user and permit it to access all databases on the MySQL server.

Make sure you replace “username” with the username of your choice.

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
You can exit out of the MySQL command-line interface by typing “quit” in the terminal. Configuring Apache for PHPMyAdmin Before you can load the PHPMyAdmin interface on our Raspberry Pi, we will need to make some configuration changes to Apache. entering This the following into the terminal.
sudo nano /etc/apache2/apache2.conf
Now we need to add the following line to the bottom of this file.
Include /etc/phpmyadmin/apache.conf
Once done we can save and exit by pressing CTRL + X and then pressing Y then ENTER. Now we need to restart the Apache service on our Raspberry Pi by running the command below.
sudo service apache2 restart
Configuring PHPMyAdminTo do this, we need to run the following command on our Raspberry Pi.
<span style="font-weight: normal;">sudo ln -s /usr/share/phpmyadmin /var/www/html</span>

now you able to access your Raspberry Pi’s PHPMyAdmin interface from a web browser using your IP address.

In my case http://169.254.219.186/phpmyadmin

Now time to build your Raspberry Pi RFID Attendance System

Notice

You have to need read this tutorial for how to Building the RFID RC522 Reader Circuit and how to Enabling the SPI interface go to

How to interface RFID-RC522 with Raspberry Pi

The first thing you need to do is move on from the link above and gain an idea about the RFID.

Raspberry pi to RFID-RC522 wiring

  1. SDA connects to Pin 24.
  2. SCK connects to Pin 23.
  3. MOSI connects to Pin 19.
  4. MISO connects to Pin 21.
  5. GND connects to Pin 6.
  6. RST connects to Pin 22.
  7. 3.3v connects to Pin 1.

Now your job is to create a database for the RFID attendance system. I will now show you how to create a database.

RFID attendance system

Now it is time to load up into the MYSQL command-line tool by running the following command

sudo mysql -u root -p
Now you need to create a database according to the command line given below

We will be naming this database, “attendancesy_stem“. To create this database, run the following command

CREATE DATABASE attendance_system;
With our database created, let’s now create a user called “attendance_admin” we will utilize this user in our Python scripts to read from our newly created database.

Make sure you set the password for this to something unique and hard to guess. For our example, we will be just using “your_password” as the password

CREATE USER 'attendance_admin'@'localhost' IDENTIFIED BY 'your_password';

Now that we have created our user we need to give it the rights to access our “attendancesy_stem” database.

We can do this by running the following command. This command will give our “attendance_admin” user full privileges on any table within our database.
GRANT ALL PRIVILEGES ON attendance_system.* TO 'attendance_admin'@'localhost';

Before we create our tables, we need to utilize the “use” command so that we are directly interacting with the “attendance_system” database.

Begin interacting with the database by running the following command.

use attendance_system;

Now that we are dealing directly with the database that we want to utilize we can now start creating the tables where all our data will be stored

You can leave the MYSQL tool by entering exit; Recording a User in the Attendance System
Before we get writing our attendance system scripts, we need first to install the Python “MYSQL Connector” using pip.

Install the connector library by running the following command on your Pi.

sudo pip3 install mysql-connector-python

Recording a User in the Attendance System Code


import time
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import mysql.connector


db = mysql.connector.connect(
  host="localhost",
  user="username",
  passwd="your_password",
  database="attendance_system"
)

cursor = db.cursor()
reader = SimpleMFRC522()

try:
  while True:
    print('Place Card to\nregister')
    id, text = reader.read()
    cursor.execute("SELECT id FROM users WHERE rfid_uid="+str(id))
    cursor.fetchone()

    if cursor.rowcount >= 1:
      print("Overwrite\nexisting user?")
      overwrite = input("Overwite (Y/N)? ")
      if overwrite[0] == 'Y' or overwrite[0] == 'y':
        print("Overwriting user.")
        time.sleep(1)
        sql_insert = "UPDATE users SET name = %s WHERE rfid_uid=%s"
      else:
        continue;
    else:
      sql_insert = "INSERT INTO users (name, rfid_uid) VALUES (%s, %s)"


    print('Enter new name')
    new_name = input("Name: ")

    cursor.execute(sql_insert, (new_name, id))

    db.commit()

    print("User " + new_name + "\nSaved")
    time.sleep(2)
finally:
  GPIO.cleanup()

Recording Attendance

#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import mysql.connector

db = mysql.connector.connect(
  host="localhost",
  user="username",
  passwd="your_password",
  database="attendance_system"
)

cursor = db.cursor()
reader = SimpleMFRC522()

try:
  while True:
    print('Place Card to\nrecord attendance')
    id, text = reader.read()

    cursor.execute("Select id, name FROM users WHERE rfid_uid="+str(id))
    result = cursor.fetchone()

    if cursor.rowcount >= 1:
      print("Welcome " + result[1])
      cursor.execute("INSERT INTO attendance (user_id) VALUES (%s)", (result[0],) )
      db.commit()
    else:
        print("User does not exist.")
        time.sleep(2)
finally:
  GPIO.cleanup()

Now you can check your Database for update

My Previous project

  1. PIR Motion Sensor using Raspberry Pi4 | Interfacing Tutorial
  2. PIR Sensor -Email Sending Movement Detector using IFTTT
  3. Controlling a DC Motor with Raspberry Pi4
  4. How to Use the Raspberry Pi4 Camera And PIR Sensor to Send Emails
  5. Raspberry Pi Distance Sensor using the JSN-SR04T
  6. How to connect 16×2 LCD with Raspberry pi
  7. How to interface RFID-RC522 with Raspberry Pi