- Introduction
- Demonstration
- System Architecture
- Things used in this project
- Pin Wiring between PHPoC and Adafruit PN532
- Source Codes
- Main code
- Libraries
- Mysql server installation and create databases
Introduction
Nowadays, many companies use RFID tag as an employee card which contains employee’s identification information. This tag can be used to lock/unlock the door or to monitor working time (starting/finishing time).
In this article, I am going to show how to use RFID tag to monitor working time of employees. When an employee taps his/her card (RFID tag) to a RFID reader, employee’s identification information from the card is sent to a remote mysql database. Identification information of the employee can be the tag’s UID or the designed ID which stored in user memory of tag. Refer to https://forum.phpoc.com/blogs/khanh-...-reader-writer to see how to read UID, read/write user memory.
Demonstration
System Architecture
Things Used In This Project
- PHPoC Blue or Black
- PN532 NFC/RFID controller (in my case, I used Adafruit PN532 RFID/NFC Breakout)
- Some NFC/RFID tags
- PHPoC bread board (Optional)
- Jumper wires
Pin Wiring between PHPoC and Adafruit PN532
- PHPoC------------- Adafruit PN532
- 3V----------------3.3V (Red wire)
- SCK ----------------SCK (Yellow wire)
- MISO---------------MISO (Green wire)
- MOSI---------------MOSI (Blue wire)
- 4 ------------------SSEL (White wire)
- GND---------------GND (Gray wire)
Source Codes
Main code
<task0.php>
PHP Code:
<?php
if(_SERVER("REQUEST_METHOD"))
exit; // avoid php execution via http request
include_once "/lib/sd_340.php";
include_once "/lib/sn_dns.php";
include_once "/lib/sn_mysql.php";
include_once "vd_pcd_pn532.php";
include_once "vd_mifare_classic.php";
//Check and print error messages from DB server
function chk_error($result)
{
if($result !== false)
{
$error = mysql_error($result);
if(strlen($error) != 0)
echo "Error: $error\r\n";
}
}
//Enter your DB Server's hostname or IP address!
$server_addr = "192.168.0.3";
//Enter your account information!
$user_name = "your_username";
$password = "your_password";
$uid = "";
$pre_uid = "";
reader_init(); //RFID reader init
//Connect to DB Server
if(mysql_connect($server_addr, $user_name, $password))
{
//Create a database named employee
//$result = mysql_query("CREATE DATABASE employee;");
//chk_error($result);
$result = mysql_select_db("employee");
chk_error($result);
//Create a table named working_time
//$result = mysql_query("CREATE TABLE tbl_working_time (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, employee_id VARCHAR(20) NOT NULL, date_time DATETIME(0) NOT NULL);");
//chk_error($result);
//mysql_close();
}
while(1)
{
if(reader_ISO14443A_is_present($uid))
{
if($pre_uid == $uid)
continue;
$pre_uid = $uid;
$uid = bin2hex($uid);
/**
Note: In this code, I use Tag's UID as employee ID.
To make it more flexible, You can wire your own design employee ID to user memory.
And then read it to update to database as employee ID.
**/
//Insert a record
$result = mysql_query("INSERT INTO tbl_working_time (employee_id, date_time) VALUES ('$uid', NOW());");
chk_error($result);
//Inquiry the last record
$result = mysql_query("SELECT * FROM tbl_working_time WHERE employee_id='$uid' ORDER BY date_time DESC LIMIT 1;");
chk_error($result);
//show result of the inquiry
$result_arr = mysql_fetch_row($result);
//Print the result
printf("%s -> %s -> %s\r\n", $result_arr[0], $result_arr[1], $result_arr[2]);
}
}
mysql_close();
?>
Libraries
- Mysql library: Official Library of PHPoC at https://github.com/phpoc/psp
- PN532 library at: https://forum.phpoc.com/blogs/khanh-...-reader-writer
Mysql server installation and create databases
- Install mysql server: you can install only mysql server or XAMPP packet on your computer (XAMPP download link https://www.apachefriends.org/index.html)
- Create an username and password:
- Open cmd.exe
- Type following command:
Code:cd c:\xampp\mysql\bin mysql.exe -u root -p
- Input your root password
- Create a new username and password (which is used in the source code)
Code:CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost' WITH GRANT OPTION; CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;
- Create data base and table:
Code:CREATE DATABASE employee; USE employee; CREATE TABLE tbl_working_time (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, employee_id VARCHAR(20) NOT NULL, date_time DATETIME(0) NOT NULL);
You can combine with other tables to make this system more complete.
If you have any questions or something to discuss, leave a comment below.