RFID/NFC Door Control

RFID and NFC are similar technologies. Both type of cards can transmit 13.56 MHz radio signals without having an inside power source, so employees can enter with a card or packages can be tagged. The supply power is generated in the RFID/NFC cards from radio waves transmitted by the reader device. The reader sends an interrogating signal to the tag via the antenna, and the tag responds with the data stored in it's EEPROM, which is basicly the ID of the tag, so Ozeki 10 can identify a person or package.

The difference between RFID and NFC is that the RFID cannot rewrite tags. NFC technology is good for electronic payments too, because it can rewrite EEPROM data. Ozeki supports both RC522 or PN532 RFID/NFC readers, so you can program Ozeki 10 to permit entrance for people or find a package ID on your SQL server. This guide will show you how to open a relay controlled door.

Section 1 - Build a relay controlled door opener with an NFC reader

STEP 1: Acquire relay, NFC reader and Arduino

Get required components for an RFID door controller:

  • Raspberry Pi 2/3 (or Asus Tinker Board)
  • Arduino Nano, Uno or Mega
  • Relay board with a single relay
  • PN532 (in this guide) or RC522 NFC reader
  • At least one RFID card
  • breadboard (to build your test circuit on)
  • Jumper wires plus a USB cable
  • A door controller magnet wired to the relay

the required hardware
Figure 1 - The required hardware

STEP 2: Wire the circuit the following way

This picture shows how easy it is to wire the component, which is quite familiar to most Arduino wiring found in the Ozeki Guide. Although a big difference is that the relay can control any voltage level coming from the power supply. Even 230V can be easily controlled by the relay, which is the light blue device in the circuit. In this example the relay is connected to a door magnet, which opens the door, but you can support power to any other electronic device. For example barriers or access to highly secured servers running in a cold room. The supplied power doesn't just powers devices, it moves the relay as well. The red PN532 NFC model below, uses I²C wiring, but the RC522 model's SPI communication can be used too, if you wire it.

wire the circuit
Figure 2 - Wire the circuit

STEP 3: Download and install Ozeki to your Pi

Since RFID tag readers are frequently placed next to the entrance It is highly advised to place a Raspberry Pi in a small box beneath the door, since it is directly wired to the reader. Download and install Ozeki 10 for your Pi from http://www.ozeki.hu/index.php?owpn=231 If you have problem during installation, please do page 5986 from this step by step guide.

install ozeki to your pi
Figure 3 - Install Ozeki to your Pi

STEP 4: Install Arduino development package

If you are familiar with Arduino technology, please make sure that the development package is downloaded to an OS you are familiar with. Make sure that Ozeki 10 is downloaded to the OS too, you do not have to activate license to use the Arduino libraries, but should get a license for your Pi if you haven't done it yet. The link to Arduino ide is: https://www.arduino.cc

download arduino ide
Figure 4 - Download the arduino IDE

STEP 5: Please format the EEPROM

Now plug the Arduino to the machine with the installed Arduino IDE. Without a clear EEPROM, the Ozeki 10 devices are not guaranteed to work properly, so you can try a format. An empty EEPROM can leave plenty of space for all required Ozeki 10 device ID -s. All Ozeki 10 device has it's own randomly generated ID, which makes them easily identified by any Ozeki server you connect the Arduino to. A clearly understandable guide for wiping the EEPROM is at your fingertips: http://www.ozeki.hu/index.php?owpn=3110. Look below to check if it has successfully done the job.

clear the eeprom
Figure 5 - Clear the EEPROM

www.ozeki.hu/attachments/5968/RFID_DoorController_code.txt (copy)
The main code above works with the Ozeki 10 libraries and is a combination of the switch controller and the NFC reader device. You can see that D13 is the pin that moves the relay, which opens the door and the I²C communication runs from the factory designed SCL and SDA pins of the Arduino. If you use I²C NFC communication instead of SPI, you can remove 'SPI.begin();' from the code. Although if there is enough space in the flash memory, you can leave it there. SPI technology is for 'MODE::RC522'

upload the rfid door control
Figure 6 - Upload the RFID door control

Section 2 - Can you read some tags with the NFC reader? Can your relay make a quite click?

Before making a real time test, it is required to plug your Arduino to the same Ozeki 10 server machine that will control the relay controlled entrance of your house or company. The server can only detect the Arduino's ATmega microcontroller if it is directly plugged to the USB socket. Ozeki 10 can realize it as a COM port and autodetect the uploaded microcontroller devices.

STEP 7: Start the Control Panel

If the Ozeki server is running, you can reach it through a remote webbrowser. Use the Raspberry Pi IP and port 9505. Log in to Ozeki 10 and on Ozeki 10's desktop, please click the small red Napoleon icon to start the Control Panel which is the best application to add your new devices. It also manages and logs the message flow between each connection.

start the control panel
Figure 7 - Start the control panel

STEP 8: NFC and Relay on connection list

The first thing you should see is the PN532 and relay on the connection list. The same devices should be visible to you as you can see it on the picture below. If they do not appear, try to connect them through the 'Serial ports' tabpage on the left side vertical toolbar. Find a grey switch right to the Arduinos COM port, if you click on it, the serial port will connect to Ozeki 10.

the connection list
Figure 8 - The connection list

STEP 9: Test NFC reader by viewing events

The GUI of each Arduino device is user friendly and easily manageable. After clicking on the 'Details' button next to the NFC connection, you can try detecting a few RFID tags. On this screenshot you can see 4 different tag ID -s detected on the 'Events' tabpage. Later on you will need the detected card ID numbers to store them in your SQL table. This way you can let people in with RFID cards, so please save them. It doesn't matter which reader is used, it will send events automatically to Ozeki 10. It is good to know that the reader detects a single card multiple times, if it is held over the module. Examine the 'Events' tabpage below and see all detection.

test the nfc
Figure 9 - Test the NFC

STEP 10: Test the relay that will open doors

Click on the small house icon on the top toolbar. It is called 'Home'. You will return to the connection list of your Ozeki 10, where you should click on the Switch controller and try it out. The first tabpage you will land on is called 'Test'. Here you can control your relay with the same On/Off switch you can see below. Please listen to the relay and hear the tick after pressing the button, this is really important. It is advised to connect the power supply and your door control magnet as well to test the circuit in real time. From now on you can open your entrance from the GUI too, which makes the device manageable globally through any webbrowser you can access Ozeki 10.

test the relay
Figure 10 - Test the relay

Section 3 - INSERT tag records INTO a table and authorize access

In this section you will see how to save your RFID tags in an SQL table and access these records through Ozeki's database messaging connection. You are going to find an example code that can opens the relay if the record is found. Your Ozeki server has an application called 'Robot Controller'. So you can use the browser to program the most intelligent ideas.

STEP 11: CREATE NEW TABLE for RFID tags

www.ozeki.hu/attachments/5968/create_table_RFID.txt (copy-paste)
Please open your database server and create a test table that will contain all the RFID tags that are authorized to access. It is suggested to use the CREATE TABLE statement above, in the same way you can see it in the screenshot. The example table contains the name of the owner for each RFID tag. To build the database for this guide, XAMPP was installed and MariaDB was used. It can be easily managed with MySQL statements.

create new table for rfid tags
Figure 11 - Create new table for RFID tags

STEP 12: INSERT RFID tags INTO table

www.ozeki.hu/attachments/5968/insert_into_RFID_table.txt (copy-paste)
The SQL INSERT statements in the file above are for example purpose, please think back what RFID signals you have caught. Hopefully the signals are saved, so you can re-use them. If not, you can repeat STEP 9 to get the tags. You have to modify the example above and paste in your own tag IDs and write in the name of each tag ID owner. Finally check the records with 'SELECT * FROM rfid_tags;' wich will show the rfid_tags table content.

insert rfid tags into table
Figure 12 - Insert RFID tags into table

STEP 13: Open Databases application

If you look back to your Ozeki 10 GUI, you have left it at the sensor testing phase. From this point you will see how simply you can connect Ozeki 10 with your database. Click through the following route to find the application:

Ozeki's Start menu/Programs/Administrative Tools/Databases

open the database application
Figure 13 - Open the Database application

STEP 14: Select SQL database server type

The 'Databases' application can easily connect to most common SQL database servers. Click the wide blue 'Create new Database connection' and select the type of your database server. For this guide MariaDB was used. It works like a MySQL database, although ODBC, Oracle, Postgre SQL, Db2, SQL Server Express, MS Access, OLEDB, Informix and SQL Anywhere is supported as well. You will love automatic table management.

select the sql database server type
Figure 15 - Select the database type

STEP 15: Fill the SQL database connection details

A database login form will open, where you can use a database user that has permissions granted to read the table for rfid tags. The IP textbox should contain the IP address of your SQL database server and the port number is 3306 by default. In this guide the database is called 'ozeki' and the root user was used. If you do the same, you have to grant global access rights to use the root with %. Click 'Ok' and if you see a green dot next to the database, you can control it from Ozeki 10. Please switch back to the Control Panel and see how to create an SQL messaging connection.

connect to database
Figure 16 - Connect to a database

STEP 16: Click 'SQL' icon to make a database messaging connection

Hopefully your Arduino is still plugged in, so the device connections are still there. But now you have to click 'Create new Connection' and select the 'Application' icon and then the 'SQL' button as below. The SQL messaging connection is required to interact with the database by sending automatic SQL statements in real time. It must be paired with the database server connection and in this guide the rfid tags will be polled every 20 seconds. The SQL messaging connection will be the one that you have to subscribe to from the short program code that controls the relay and is made for you.

start creating sql messaging conncetion
Figure 17 - Click the SQL icon

STEP 17: Add periodic send to messaging connection

This is an important part, not to mistype the SQL SELECT statements. Go over the 'Send' tabpage and type the following statement in the textbox:

SELECT rfid, rfid, rfid, rfid FROM rfid_tags
At the current development stage of Ozeki 10, the rfid has to be selected four times in a record. You also have to set a delay between each SQL request. Click 'Ok' to see it appear and look at the SQL events if it works. (Do not forget to write a random SELECT statement in the 'SQL UPDATE used if the polling' textbox. At the moment Ozeki 10 requires it. You can leave the other 4 textboxes empty. Find them by scrolling down to 'Ok'.)

configure periodic send
Figure 18 - Configure periodic send

STEP 18: Open Robot Controller

If the SELECT works, you can subscribe to it in Snap or Robot Controller. For this guide the Ozeki Robot Controller application was selected from:

Ozeki's Start menu/Programs/Education/Robot Controller
Ozeki Robot Controller is an integrated C# IDE capable to manage complex message flow between each connection, in case you would like to connect them to do the job for you. It can solve complex message routing problems. Like in this case it routes the polled SQL records to a C# list. This not direct routing between two connection, if it was, you wouldn't need a C# code. In the near future... you will create simple algorithms with Ozeki Snap blocks.

open robot controller
Figure 19 - Open robot controller

STEP 19: Download and run the example code

www.ozeki.hu/attachments/5968/RFID_door_code.txt (copy-paste)
A simple code has been made for you to copy and paste into your Robot Controller. Press 'Run' to start it from the top toolbar as you can see it below. It subscribes to your Arduino NFC device and the SQL messaging connection. It can open a relay if the keycard ID is found between the SQL records. The records polled from the SQL server are appended to a list.

run the example code
Figure 20 - Run the example code

RFID doors are mainstream

The technology is getting so mainstraim that it is leaping far beyond imagination... The main entrance door of most hallways are only the beginning. The technology expands to hotel rooms and even further. You can power the room with electricity by putting the card in a slot. The RFID keycard powers the room lamps the same way you have left them. You can also load money on NFC bracelets to drink cocktails in the swimming pool.

rfid card in working
Figure 21 - RFID card in working

More information