IoT_lover
Published © GPL3+

An “IoT Lock” System for Multiple Doors

A door system with RFID-based lock can know access permission level of users and can save the accessed history into database.

BeginnerFull instructions provided2,242
An “IoT Lock” System for Multiple Doors

Things used in this project

Hardware components

PHPoC Black
PHPoC Black
Or PHPoC Blue
×1
Adafruit PN532 NFC/RFID controller breakout board
×1
13.56MHz RFID/NFC Card
×1
PHPoC Bread Board
PHPoC Bread Board
×1
Jumper Wires
DIYables Jumper Wires
×1
Relay Module 5V 1-Channel
DIYables Relay Module 5V 1-Channel
×1
Solenoid Lock 12V DC
DIYables Solenoid Lock 12V DC
×1

Story

Read more

Schematics

Things and connection

Code

Normal RFID Lock

PHP
This code do the basic task of a RFID lock. Compare with RFID IoT lock code to see the difference
<?php

if(_SERVER("REQUEST_METHOD"))
    exit; // avoid php execution via http request

include_once "/lib/sd_340.php";
include_once "vd_pcd_pn532.php";
include_once "vd_mifare_classic.php";

$uid = "";
$pre_uid = "";

$access_list = array("FE65B0AB"); //add more Key here

function access_list_look_up($uid)
{
    global $access_list;
    
    $count = count($access_list);
    
    for($i = 0; $i < $count; $i++)
        if($access_list[$i] == $uid)
            return true;

    return false;
}

reader_init(); //RFID reader init

while(1)
{
    if(reader_ISO14443A_is_present($uid))
    {
        if($pre_uid == $uid)
            continue;
        
        $pre_uid = $uid;    
        $uid = bin2hex($uid);
        
        //lookup in access list
        $result = access_list_look_up($uid);
        
        if($result == false) //Not found in access list
        {
            echo "IoT Lock: Access is denied\r\n";
            // TODO: add alert function here
        }
        else
        {
            echo "IoT Lock: Access is authorized, door is openning\r\n";
            //TODO: open the door.
        }        
    }
}
?>

RFID IoT lock

PHP
This is code of RFID IoT lock, which is evolution of RFID lock
<?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";

define("PRIVILEGE_UNAUTHORIZED",   0);
define("PRIVILEGE_EMPLOYEE",   1);
define("PRIVILEGE_MANAGER",   2);

//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 = "";

$door_id = "00000001"; // Change door id here.
$allow_privilege = PRIVILEGE_MANAGER; // mininum privilege level is required to open door. Change it according to each access permision of each door.

reader_init(); //RFID reader init

//Connect to DB Server
if(mysql_connect($server_addr, $user_name, $password))
{
    $result = mysql_select_db("iot_lock");
    chk_error($result);
}

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 key ID. 
        To make it more flexible, You can wire your own design key ID to user memory. 
        And then read it to update to database as key ID.
        **/
        
        //Inquiry the last record
        $result = mysql_query("SELECT * FROM tbl_key WHERE key_id='$uid';");
        chk_error($result);
        
        //get result of the inquiry
        $result_arr = mysql_fetch_row($result);
        
        if($result_arr === false) //Not found in database
        {
            echo "IoT Lock: Unknown key\r\n";
            // TODO: add alert function here
            continue;
        }
        
        $user_privilege = (int)$result_arr[1];
        
        if($user_privilege < $allow_privilege)    
            echo "IoT Lock: Access is denied. The key doesn't have privilege\r\n";
        else
        {
            //TODO: open the door.
            echo "IoT Lock: Access is authorized, door is openning\r\n";
            //create a history record
            $result = mysql_query("INSERT INTO tbl_history(key_id, door_id, date_time) VALUES ('$uid', '$door_id', NOW());");
            chk_error($result);
        }        
    }
}

mysql_close();
?>

Credits

IoT_lover

IoT_lover

10 projects • 189 followers

Comments