I soldered the hardware and made a Python script to control the hardware and send requests to the API. I'm also still converting certain pages from plain PHP to the Silex app.
Raspberry Pi hardware
This week the Raspberry Pi hardware arrived and I immediately started soldering. I did my research so I knew where everything had to be connected.
A list of the hardware:
- LCD screen kit
- NFC/RFID breakout board
- Pi Cobbler kit
I started with the LCD screen. This is a 16x2 inverse RGB display that comes with a PCB Pi plate to solder your components on. It's an RGB display, so you can (programmatically) choose the color of the backlight. Fancy! It also features some buttons (keypad) but I won't need this.
It uses the I2C (SDA/SCL) pins of the Raspberry Pi, which is thanks to the I2C port expander chip MCP23017 (to be soldered onto the PCB) because the LCD needs 3 backlight pins (RGB) and 5 keypad pins. With the expander, we only use two I2C pins. And the best part is that we don't really lose those pins because you can use the stacking header pins to connect other elektronics and let them share the I2C bus.
I started by soldering the three resistors, as they are needed for the RGB backlight colors, then I soldered all pins of the the I2C Port Expander chip, the pins of the 'extra tall' stacking header, the keypad buttons and the contrast potentiometer and finally all the pins of the LCD.
To set up I2C on the Raspbian OS, I needed to edit /etc/modules and add:
And just reboot your RPi to enable the I2C drivers. To add SMBus (includes I2C) support to Python, I entered these commands:
sudo apt-get install python-smbus sudo apt-get install i2c-tools
With the last tool (I2C-tools), you can scan for I2C devices connected to your board. It can help you find the 7-bit I2C address or just troubleshoot if the LCD is recognized en properly connected.
sudo i2cdetect -y 1
This is the I2C address output you'll receive if the LCD plate is connected 0x20
Adafruit provides a very easy-to-use Python library. Just clone the repo and execute the Adafruit_CharLCDPlate.py example to test everything. You can set the backlight color, print a string on the LCD or turn the LCD off with some methods from the library, and even check for button presses on the keypad.
NFC/RFID breakout board + Pi Cobbler
To read NFC or RFID cards and tags, I needed an NFC/RFID breakout board. Originally designed for the arduino, but it also works with the Raspberry Pi. I read that the NFC/RFID breakout board is more appropriate for the Raspberry Pi, because the NFC/RFID shield did have 5V level shifting which could damage the Raspberry Pi.
You can see the NFC/RFID shield on the left, and the NFC/RFID breakout board on the right (the one I ordered). I needed to solder the pins into the two selectors. With the selectors, you can choose the desired bus. I chose UART, so I had to put both selectors in the "off" position.
I also soldered the pins needed to connect the NFC board to the Raspberry Pi:
I also needed a Pi Cobbler to connect the pins of the Raspberry Pi to a breadboard using a ribbon cable. I soldered all 52 pins and then I assembled the NFC board with the Pi Cobbler and the Raspberry Pi.
The easiest way to use the famous libnfc library is with the UART bus. We need to free the UART bus on the Raspberry Pi first, as it is already dedicated for other purposes by default.
sudo nano /boot/cmdline.txt Change: dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 $ to dwc_otg.lpm_enable=0 console=tty1 $
Then, we need to comment out the next line:
sudo nano /etc/inittab Comment this line: #Spawn a getty on Raspberry Pi serial line #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
We can download the libnfc library from http://libnfc.googlecode.com/files/libnfc-1.6.0-rc1.tar.gz. I followed a tutorial to build the libnfc library and configure it on the Raspberry Pi.
Once the hardware was fully assembled, I could test it by executing the nfc poll bash script. Once I held my smartcard above the NFC sensor, all the data on the card got immediately printed on the screen. Great!
It was the first time I actually programmed Python, but it was very intuïtive. I first made a python app that would save the user id's and their check-in status into an array, and then start to poll to see if there were changes. If the user checked-out, the LCD screen would instantly light up in a bright red color and display a notice that "User X has checked-out". The same happens when a user checks in using the webinterface: the LCD screen lights up with a green backlight color and displays the message that "User X has checked-in". I then modified the script to use a queue when multiple users checked in/out at the same time.
Converting the admin panel
I was still supposed to re-implement my admin panel in the Silex application. I wrote the admin check-in and profile manager pages in the first month of my internship. Now that I'm working with Silex, and therefore using a better structure and improved code quality using controllers, data repositories and Twig views, I still needed to convert my PHP code to the Silex form's, queries into my repositories and the functionality inside my controllers. I'm now also using strict form validation and checking for parameter tampering and taking other security measures.
I also wrote a CRON script that could automatically take a backup of the Raspberry Pi's MySQL database onto my laptop on a certain time interval. This was needed because we're already using the check-in webapplication to check-in and check-out every day to "test" and find ideas for improvement.