Introduction: Electromagnetic Bikeshed Lock

Project Requirements:

  1. Raspberry Pi
  2. Python 2 and/or Python 3
  3. RFID-RC522
  4. EM Lock
  5. 3v3-12v Step up Boost converter
  6. 2 leds ( red & green )
  7. SPI-Py library
  8. Flask
  9. MySQL & MySQL Workbench (latest version)
  10. pycharm
  11. 5v Power Supply
  12. Transistor

Explanation

When everything is running and powered on, you take the rfid card and scan it on the rfid reader. The lock is automaticly open and upon card scanning, the lock will close. when you meanwhile try to scan with another badge, this will not work because it is another than the one that last checked in to store his bike.
When you scan with the card again that was used the first time, the lock will open again, and you are free to take your bike back home.

On the website, you can see on the homepage which locks are opened/locked/broken or reserved.
When you navigate to the page Analytics, you can there see data being displayed from the database
The 2 kinds of data are: the amount of open locks/week and the amount of broken locks/week.

Raspberry Pi

Use following code to set up your pi.

me@my-rpi:~ $ sudo apt update<br>me@my-rpi:~ $ sudo apt install -y python3-venv python3-pip python3-mysqldb mariadb-server uwsgi nginx uwsgi-plugin-python3<br>me@my-rpi:~ $ python3 -m pip install --upgrade pip setuptools wheel virtualenv
me@my-rpi:~ $ mkdir project1 && cd project1
me@my-rpi:~/project1 $ python3 -m venv --system-site-packages env
me@my-rpi:~/project1 $ source env/bin/activate
(env)me@my-rpi:~/project1 $ python -m pip install mysql-connector-python argon2-cffi Flask Flask-HTTPAuth Flask-MySQL mysql-connector-python passlib

Clone this github project: https://github.com/NMCT-S2-Project-I/Project-I.git or in case this doesn't work: https://github.com/NMCT-S2-Project-I/project-i-Def...

navigate to the files in /conf that you got from the above github link and correct the path & user to the one of your own.

Database

Use following code to set up the database.

sudo mariadb

CREATE USER 'project1-admin'@'localhost' IDENTIFIED BY 'adminpassword';<br>CREATE USER 'project1-web'@'localhost' IDENTIFIED BY 'webpassword';
CREATE USER 'project1-sensor'@'localhost' IDENTIFIED BY 'sensorpassword';
CREATE DATABASE project1;
GRANT ALL PRIVILEGES ON project1.* to 'project1-admin'@'localhost' WITH GRANT OPTION;
GRANT SELECT, INSERT, UPDATE, DELETE ON project1.* TO 'project1-web'@'localhost';
GRANT SELECT, INSERT, UPDATE, DELETE ON project1.* TO 'project1-sensor'@'localhost';
FLUSH PRIVILEGES;

Pycharm

connect your pycharm with the MySQL or MariaDB datbase.
Use datascource > MariaDB
go to ssh, there you fill in the credentials to your pi
in the general page, you fill in the credentials to your database on the pi

Raspberry Pi

me@my-rpi:~/project1 $ sudo cp conf/project1-*.service /etc/systemd/system/<br>me@my-rpi:~/project1 $ sudo systemctl daemon-reload
me@my-rpi:~/project1 $ sudo systemctl start project1-*
me@my-rpi:~/project1 $ sudo systemctl status project1-*
me@my-rpi:~/project1 $ ls -l /etc/nginx/sites-*
me@my-rpi:~/project1 $ sudo cp conf/nginx /etc/nginx/sites-available/project1<br>me@my-rpi:~/project1 $ sudo rm /etc/nginx/sites-enabled/default
me@my-rpi:~/project1 $ sudo ln -s /etc/nginx/sites-available/project1 /etc/nginx/sites-enabled/project1
me@my-rpi:~/project1 $ sudo systemctl restart nginx.service
me@my-rpi:~/project1 $ sudo systemctl status nginx.service

Then do: wget -qO- localhost
You should get a return saying "Hello World"

Then, to automaticly start my scripts:

Go to /etc/rc.local
Scroll down to the very last bottom line and add there:

sudo systemctl start project1-sensor.service<br>sudo systemctl start project1-flask.service

Website

This link contains the full website already: https://github.com/NMCT-S2-Project-I/project-i-Def...
The templates have the needed HTML files for display.
the index.py file is the one where all the routes are made and where the MySQL connector is.
the Static folder contains all js, css, img's & svg's.

The templates folder contains the files 'index.html' & 'analytics.html', both extended on the 'template.html'.
Analytics.html contains a single section where you run the chart.js in with a footer.
using the jinja macro in the chartjs script can be tricky, so be sure to watch that part extra carefully!

Index.html contains all the sections, made to display all the locks + the legend and footer.
In here, I chose to do every row in a seperate section, because this way, you could do alot more, and make a better responsivity. In a table, there was quite some trouble and it just didn't look good.

Sensor Code

this link contains all code needed to run the lock, leds & RC522 RFID reader:
https://github.com/NMCT-S2-Project-I/project-i-Def...

A user ID is always displayed as 4x 1byte
The Read.py file is only able to read the badges, and will scan for them with check if the current scanned badge is matched with the previous one. There is also logging added for support.
The Write.py file can write items on the file, only 8x 1byte can be written there.
The MFRC522 file contains all code in order to check for the RFID NFC protocols, authentication and etc.

The code is explained inside the files on github (in english) for your ease :)

The end result

As extra addition, the lock can also be opened through the website.