Attendance System Using Raspberry Pi and NFC Tag Reader

264K17943

Intro: Attendance System Using Raspberry Pi and NFC Tag Reader

With my new toy Raspberry pi I've done several testing projects from
instructables, adafruit etc. But after a while it became boring and I've started looking for something actually useful for me. My previous playground was my new phone several weeks ago which came with new feature NFC reading. Thought and thought together gave me idea for attendance system for our small (family) company using NFC tags. The additional kick was my interest in those systems before, but discovering the cheapest are for around $750 I decided it is too much for such small company as 6 employees.
As I don't have any experience with "mature" attendance systems, I've decided only to implement basic features. These consist of logging incoming people, outcoming people, start and end of a break and deleting last inserted action (in case of mistype during logging).
All these actions are logged into local MySQL database from where I can display it and manipulate with my front-end application. Because the SD card is not such safe data medium, especially when loosing power unexpectedly, I'm uploading all data daily to my local server, where I keep backup in case of corruption of the SD card.
During normal operation of the logging station, display shows current date and time and calls for action selection. When you choose appropriate action on the keyboard, display shows selected action and calls for attaching the TAG to the reader. Also the LED under display also turns on.
When the TAG is read, the LED turns off and beep signal comes out the speaker. For a brief moment display shows action and name of the owner of the card. Then everything returns to the default state waiting for another entrance.

For foot notice, this whole project including source codes is licensed under Beer-ware licence as follows:
Jakub Dvorak wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return.

STEP 1: Materials List

Everything you need for this project can be bought on E-Bay or in many Chinese e-shops like dealextreme etc.
For this project you'll need following items:
  1. Raspberry Pi model B
  2. 16x2 display
  3. Mifare RFID Reader
  4. USB numeric keypad
  5. LED diode holder
  6. Red LED diode
  7. Active 3V buzzer
  8. Small breadboard and various F-F and M-F jumper cables
  9. Box
  10. Various screws and/or glue gun, LED resistor, display 10k potentiometer

I recommend Raspberry Pi model B, because of the integrated LAN and two USB ports. That way you can use either LAN+USB keyboard or USB keyboard and USB WiFi dongle.

You can use any display, but I used HD44780 (the same display as found on adafruit), because it has well documented usage and uses only few pins from Raspberry.

I've chosen 13.56 MHz RFID reader and tags, because I've already got few tags with this technology from my experimenting with my cellphone. I've also found good How To (http://fuenteabierta.teubi.co/2013/07/utilizando-el-lector-nfc-rc522-en-la.html) and with a little help of Google translator modified given source to my needs.

Any numeric keypad can be used. Those with high buttons might be better due to thickness of the box. For installation just remove any extra buttons with small knife or screwdriver.

LED and LED holder are pure extra which are not needed for the device to be operational, but are good as extra indicator of actions.

The same is with the buzzer. I felt such a device should have it's own voice for letting people know it is working.

Breadboard and jumpers are for interconnection between each device because several pins (3.3V, GND,...) are used for more than one part. You can of course use soldered board, but I wanted to be able to replace each part easily and just glued it together with glue gun.

You'll also need few screws and/or glue gun for attaching all the pieces together. For LED you'll need small resistor. For my red LED I've used 68 ohm one which should limit current to around 19 mA. Depending on your LED you can calculate value for the resistor for example on this address. Maximum GPIO current is discussed on several forums but I wouldn't go over 20mA. Potentiometer is for contrast adjustment of the display.

STEP 2: Wiring It Together

On the picture from Fritzing above you can see connections between each element. You can find the fzz file as download link down the page.

For troubleshooting of display connection you can refer to nice manual on adafruit website.

Connection is pretty straightforward so there shouldn't be any trouble.

I've cut holes in the lid of the box for display, LED and keypad, NFC reader remains under the lid for safety and it has strong enough signal to read tags through the plastic.

I used glue gun for the alignments in the box and it holds every component firm enough. And after testing even the wires are glued to the connectors to prevent accidental disconnecting during operation.

Also don't forget to drill holes for power and network cable (or just one hole, if you use WiFi dongle or PoE).

As final touch, mark spot on the outside of the lid where NFC reader is for easier reading.

STEP 3: The Software

The software for this project is mixed from several tutorials and HOWTOs so there might be some leftovers from that, but it works for the time being and I might will fix that later (AKA never ;) )

First of all you need default Raspbian image installed on your raspberry (LINK and HowTo).

After that we need few adjustments to enable correct function:
First of all we install files needed by the NFC reader as follows on previously mentioned page (http://fuenteabierta.teubi.co/2013/07/utilizando-el-lector-nfc-rc522-en-la.html)
1. Enable SPI device by editing file /etc/modprobe.d/raspi-blacklist.conf and comment the line blacklist spi-bcm2708 so it will read as follows:
# blacklist spi and i2c by default (many users don't need them)
# blacklist spi-bcm2708
blacklist i2c-bcm2708
2. Then download SPI-Py package to enable SPI communication for Python
git clone  https://github.com/lthiery/SPI-Py
and install it via
sudo python setup.py install

If you are missing python-dev and the setup script exits with error, install the package first

sudo apt-get install python-dev

We will also need the MFRC522 package from https://github.com/mxgxw/MFRC522-python but I had to edit it a little bit, so it's already in the source folder of this project.


The last think to get it work is installing GPIO for Python (if you wish, update the number in package version to the newest one):
wget https://pypi.python.org/packages/source/R/RPi.GPIO-0.5.4.tar.gz
tar zxf RPi.GPIO-0.5.4.tar.gz cd RPi.GPIO-0.5.4 sudo python setup.py install

After that we can download the source code for this project
git clone  https://github.com/Yimbo/attendance
Create table in your MySQL Database and upload structure to that database from file mysql.sql.
Next update file mysql.py according to your settings (server, username, password and database name).

After this step you should be able to run the program:
sudo python attendance.py

STEP 4: Automatization

Now you should have fully functioning system for reading NFC tags and storing information about them in MySQL database.
For daily usage your should be ready to accept power failures, data errors etc. To avoid power failures you can use UPS, but your raspberry might still get rebooted from time to time. Also you want to use the integrated keyboard instead of the keyboard from console where you run program from.
First of all change your attendance file attributes so it can run without external calling python:
chmod +x attendance.py
In next step enable autologin on TTY1. This will automatically login your first console after reboot as root.
First install mingetty:
apt-get install mingetty
Then edit the file /etc/inittab and find line
1:2345:respawn:/sbin/getty --noclear 38400 tty1
and update it to:
1:2345:respawn:/sbin/mingetty --autologin root --noclear tty1
Then you need NumLock activated everytime system logs onto console tty1, so create file leds.sh with following content (note it probably won't affect the actual LEDs on the keypad, but it will turn Numlock ON anyway):
setleds -D +num < /dev/tty1
and change its permissions to executable:
chmod +x leds.sh
Then you want to create watchdog for your python script so even if it crashes, this watchdog will start it again. Go to your home directory and create file runattendance.sh with following text:
while [ 1=1 ];
do if [ ! "$(pgrep attendance.py)" ]; then /root/leds.sh && cd /root/Attendance/ && /root/Attendance/attendance.py fi sleep 10 done
and change it's permissions to be executable the same way as before.

After that go to your root home folder (/root/) and update file .bash_profile and add following line:
[[ $(tty) == '/dev/tty1' ]] && /root/runattendance.sh
This will run runattendance.sh every time system logs into first console (which is now automatically after booting).

If you finished all those steps, you can now reboot your raspberry and after successful boot, display should light and attendance system should work.

35 Comments

I just spend the last few hours making sure this script could run: Here is how:
//Install raspbian
sudo raspi-config
//Enable auto login for console user pi
sudo rpi-update
//run updates
//install git
sudo apt-get install git
//Clone SPI
git clone https://github.com/lthiery/SPI-Py
Cd /home/pi/SPI-Py/
//Install python
sudo apt-get install python-dev
//get rpi GPIO
wget https://files.pythonhosted.org/packages/f0/19/89e634790eb10d64ca87ce5c01751e784a730cf102262205d815275e883a/RPi.GPIO-0.6.4.tar.gz
tar zxf RPi.GPIO-0.6.4.tar.gz
cd RPi.GPIO-0.6.4/
sudo python setup.py install
//clone rep
git clone https://github.com/Yimbo/attendance
//Install mysql &set password
sudo apt-get install mysql-server && sudo apt-get install mysql-client
//install phpmyadmin, select apache2 & answer yes at dbcommon
apt-get install phpmyadmin
nano /etc/apache2/apache2.conf
//include the folowing line in the conf file
Include /etc/phpmyadmin/apache.conf
//make service
/etc/init.d/apache2 restart
//login to mysql & change root settings
mysql -u root -p
update user set plugin='' where user='root';
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
flush privileges;
//import mysql file into new databas
//install modules
apt-get install python-mysqldb
apt-get install python-unidecode
sudo apt-get install python-pip
Pip install beeper
//run attendance
chmod +x attendance.py
sudo python attendance.py
//create led control
sudo nano /root/led.sh
//insert into led.sh
setleds -D +num < /dev/tty1
//make executable
chmod +x led.sh
//create file to run at startup
sudo nano /home/pi/runattendance.sh
//inset into file, make sure you enter the correct paths
while [ 1=1 ];
do
if [ ! "$(pgrep attendance.py)" ];
then
/root/led.sh && sudo /home/pi/SPI-Py/RPi.GPIO-0.6.4/attendance/attendance.py
fi
sleep 10
done
//make file executable
chmod +x runattendance.sh

All done

Edit: Ran into another few problems ^^. If you get a typerror, simply add a comma (,) behind the tagid statement.
The Mysql delete function was not doing what it was supposed to, needs some tweaking as well
* Mysql delete function needs db.commit() after cur.execute in mysql.py line 57 to work properly, this should be corrected for all MySQLdb calls actually.
* Typo in attendance.py line 185, true has to be in uppercase, True
* PIP install: pip probably not in search path before reboot, use;
sudo /usr/bin/pip install beeper
Correct, some of the code needs some changes. As stated the commit is missing. I fixed all that, simply forgot to mention it....too many fixes :)
Thanks for listing them.

Thanks for taking the time to create these instructions. I help organise a charity run and I've adapted this system to work as a low cost timing system for our races. It works really well!

Hi, how did you solve the import beeper problem?

May I ask how to solve importerror: No Module named onScreen? Please? Looking forward for your response. Thank you

This Instructable is incomplete. A lot of steps are not mentioned by the author.

Hi, can you do this with fingerprint scanner too?

Hello, how did you program the cards?

When I'm trying to install setup.py of SPI-Py folder, it shows an error as: command 'gcc' failed with exit status 1..

Help me to solve this. Please!!

To fix the GCC error you need to run this command first: sudo apt-get install python-dev

Hey! I've got a question for the wiring part of the RFID RC522. I have connected everything to the right pins (Got raspberry pi 2) but no led lights on the rc522 turn on and the chip gets hot as hell. Do you know how I can solve this? Many thanks for your help!

hi i love ur project it is similar to my project can u share idea wit me wasimahmed9415@gmail.com thank you

Hi, it seams that the beeper module is not supplied on the github page

File "/home/pi/src/attendance/attendance.py", line 23, in <module>
import beeper
ImportError: No module named beeper

after commenting this out the only thing i get is something like this

attendance.py:178: RuntimeWarning: No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead!
GPIO.cleanup()
29.10.2014 06:44:43
29.10.2014 06:44:44
29.10.2014 06:44:45
29.10.2014 06:44:46
29.10.2014 06:44:47
29.10.2014 06:44:48
29.10.2014 06:44:49

But no readings, i can confirm that when i use the

MFRC522-python-master/Read.py i get readings so my connections are ok..

please help

The GPIO.cleanup() isn't successfully clearing the channels. I'll see what I can do.

Also I must mention; use the terminal command 'ps aux | grep python' and close running python processes. Compiling the script over and over can throw off your channels, you gotta kill processes before.

hi there, as you go through the code you see the following

if(actions==(specified action))

this actually are unprintable ascii characters and ttys, where 49 is called upon pressing 1 key on the numpad,50 on 2 ,51 on 3 and so on

so the terimnal is showing the timing's as you have stated above, you actually need to press 7 on the numpad in order to read the uid from the rfid reader which corresponds to 55 in unprintable ascii, i believe the author chose this way of accepting choices in order to run the code at boot(this is what i think may be something else, not sure). you can inbox me if need further information regarding this

sirion ,were you ever able to figure this out?

More Comments