A Look into the Source Code behind Pi-Lock

Reading the RFID tags

This method opens the Raspberry Pi’s serial port, and loops continuously to read a RFID card/tag. It is the primary method of the Pi-Lock, and starts when the RPi boots up. If a card is found, it calls the findTag() function, which in turn searches in the local SQLite database (table: seguridad_permisos) for the card, and then determines if the scanned card if valid for entry.


Check the configuration.py file for configuring the serial port variables. The .conf signifies a variable inside this file.

Turning on a LED

A simple method to switch the LEDs’ color state. The parameter colorOn directs which light to turn on, thereby switching off the remaining two colors. The conf.GREEN, conf.RED, conf.BLUE variables are set in the configuration.py file, where each value represents a GPIO pin, set as an integer. In this instance, GREEN = 4, RED = 17 and BLUE = 22.

Check if card is permitted to enter


Pi-Lock employs the use of a few object-oriented classes to make the source code more efficient, legible, and comprehensible. The classes are broken up into the following files:

Logs an entry in local SQLite db, and post data in JSON format to server
Logs an error in local SQLite db
With the scanned RFID tag #, looks into local SQLite db for the matching information, and sets object’s instance variables to the record found, else to None

Records and saves the RPi’s CPU temperature, and RAM, CPU and SD-card usage in JSON format

Class – Log an Entry

On initialization, the instance variables of the parameter permiso are passed on to the EntryLog class, and used for two actions: insert into the local SQLite db and post the JSON to a sever via HTTP protocol.

Class – Search in DB for Scanned RFID #

When a card is scanned, the Permission Class is initialized, and the RFID tag # is passed through as a parameter. The RFID tag # is then used to search the local SQLite DB in the table seguridad_permisos using a "SELECT * FROM ..." query. If the query returns null, the permission variables are set to None, otherwise the information matching the tag # is saved to the class’ instance methods.

Class – Log an Error

When an error occurs, it is important to log it. Rather than simply make a .log file, Pi-Lock opts to write each error in the SQLite db. The error log is detailed and records the error type, message, line number, file name and string of the line on which the error occurred.