Project One: Alcohol Lock

For my first project, I will make an alcohol lock with a Raspberry Pi. Short: The alcohol gas sensor will detect the percentage of alcohol in your breath and then decides if you’re safe to drive the car or not. If so, then the starter of the car will be enabled.

The times and percentages will also be kept in a SQL database along with the temperature to see if there is a connection between the temperature and the alcohol consumption.

Project One Alcohol Lock

This is a project which contains Python,Javascript,HTML and CSS. You need a bit of experience to work with this. The code is in the last step. I will explain some code so you can understand how it works. I hope you enjoy it!!

Supplies

– Raspberyy Pi 4B 2GB

– Alcohol gassensor MQ-3

– Temperature Sensor DS18B20

– RFID RC522

– Buttons

– LCD 1602 with I2C

– SPI MCP3008

– Arduino contactor FE-SRly

Underneath you will find the material list with the price and link to the sellers.

Step 1: Get the Pi Ready

Get the Pi Ready

To start I will install some programs, those will help us furhter in the project.

The following programs should be installed on the Pi:

– MariaDB

– Apache2

– Python3

– Chromium

Now we can enable SSH if not enabled yet. If it is enabled we van open Visual Studio Code and connect with remote control. Set up or use your wlan IP, if you connected to wifi you can use that too.

Now I worked with a Github for my school, so you can chose wether you clone one or not.

Step 2: Fritzing and Electric Scheme

Fritzing and Electric Scheme

The second thing to do is making a Fritzing scheme, so it will be easier to connect and keep an overview. I listed up all my components and see how the data should be read or send. For instance, the alcohol/gas sensor has an analog input, the Raspberry Pi can’t read analog values compared to an Arduino. So we will use an SPI to read this. The RFID has a SPI built in so we don’t have to change thatt, we will work with the 2 SPICE/CS ports on the PI.

The temperature sensor can be read with One Wire and the buttons are digital inputs. The contactor is a digital output.

If this is connected we have our test setup.

Step 3: Setting Up the Database

Setting Up the Database

In this second step I will setup my ERD. I connect mySQL with my PI. I connect with my Pi’s IP address. So now I made an ERD like in this picture, I filled the 3 tables with data,the Device table will contain my devices with the info it needs. The Actie tabel is filled with the possible outcomes. The Historiek tabel is now filled with dummy data but will contain the history of timestamps connected to the right device and action.

I had the fault that I couldn’t dump. My dump.exe version was lower than my server version wich was the most recent one. So I changed the path of my dump.exe file under men>preferences>administration, and now I could dump again.

In python we need to make a config file which connects our MariaDB to our database.

Step 4: Code the Site and Connect

Code the Site and Connect

Design a website using HTML and CSS. Then with javascript I fillin some important parameters, like my history and live temperature.

First of all I connected my database, I made a database file where I go get my data from my sql file. I made an datarepository file where I give my sql commands. In the config.py file I put the mysqlconnector info from my database.

Step 5: Setup Socketio

Setup Socketio

I will set up a socketIO. First of all i will import flask and some other libraries you can see in the pictures, on my backend(python). Then in the backend I simply call a function, with a socketio.on(connect). This will check if the socket is connected, if so, the function underneath that will run. There we write a message that tells us if the socket is connected.

On the front end I will catch the connection. We also check if the socket is connected in the console. Also here we can link a function that will run then.

Step 6: Live Temperature

Live Temperature

So I put in my html a section with an svg where there is a text attribute. I give the attribute a class name. Then I will measure my temperature on my backend, I put that code in a function, I call that function in a thread, so it will be running over and over. Now, in my function called Temperature, I send my value to the frontend with socketIO. I measure my temperature with the onewire. So we need to open the onewire file and read it. Make sure one wire is enabled in the raspi-config.

You will see that the code is in a while true. That is because we are going to put that function in a thread. So that the global variable will be updated all the time. and it will be updated in my database every minute due to a global datatimer. The timer can and will be used in other threads.

On my front end I will catch it and fill it in my html through innerHTML and call the class.

Step 7: Splitting the Site in 4

Splitting the Site in 4

In this step I will start a little bit of designing of my site, so I can see where I will put all the other live data.

So I now have 4 html-pages: index, history,alcohol history, users.

The names say the most, but on the index I will show a dashboard with live data, history will show the history of the events. The alcohol history will show the last measurements of the alcoholgas sensor and users will show the users.

Now the site I have is my design, you can design whatever you want. The css is in your hands. The only thing that has to be done is making sure that the attributes that you want to be filled in with live data, that those have an individual, unique class. So we can call them in our Javascript file.

Now if u work with multiple html-pages you have to work with an if function in the js-script. Don’t forget to mention your script in the header of each file.

In the if-loop we chose one main element and check if it is on the page and in that way we will determine wich functions it has to run. The functions are mostly those to show on the page and listening to the socket. Here you see a lot of functions where I will get to when it is needed.

Step 8: Making the Housing

Making the Housing

I made a custom box and printed it in 3D with my 3D-printer. It took 17 hours but is was worth it.

On the front there is a place for my lcd and RFID and also the start button. On the top you have a place for the shutdown button and the alcohol gas sensor. It is a bit thicker than expected but all the components inclusive the pi needs to get in there. If u use a preprinted PCB you can make it smaller

Step 9: Alcohol Gas Sensor

Alcohol Gas Sensor

-The alcohol gas sensor, shorter, the mq-3 is a simple analoge signal. So I will read it with an MCP3008 SPI. In the picture you will find a class that I will use in a different file, you only have to define the bus to 1 because we also have the RFID which is bus 0 and can’t be changed. So now I can call him in my main code. and simply insert read_channel(“channel numbre“).

I will convert the data to mg/l. I also put it in a thread that keeps it in a global variable. Don’t forget to open and close the SPI bus otherwise it will interfere with the RFID.

I will also write this to my database. In the datarepository you add this staticmethod.

This data i will send to the history and the alcohol history database.

Step 10: Latest Alcohol Measurement on Site

Latest Alcohol Measurement on Site

I want to show my latest Alcohol measurement on the site just like my temperature. So I make a new staticmethod in my datarepository and send it through socketio every minute thanks to a thread. In my js-file I again will catch it in the same function listenToSocketTemp which belongs with my index page. That one will send it to the ShowAlcohol function who will show it.

On the dashboard panel there is a learn more link to the infopage. You can add that if u want to explain the regulations in your country. Here this is belgium.

Step 11: LCD Screen for Communication

LCD Screen for Communication

I have an lcd with I2C so we easily init the lcd and write a little function called lcd_string to wriite to the lcd. Now we can just call lcd_string and giva a string and the line to write on the lcd.

Step 12: RFID

RFID

For the alcohol database I need a UserID. But that I only can determine if I have a name. So I need to scan the badge of the user.

I used a library to read my RFID. But before we can read we have to write first. So the first program I will run the riting programming and will hold the badge against the RFID. I have to insert a name. Now there is an ID and a name configured in the RFID. So if we test the reading program we will see that it will work.

Next up: connecting the RFID to the gas sensor. So before I will test for alcohol I will ask to scan the badge. If the scan is alright, I will tell the user and then He can blow. With that info I will write it all to the database.

I do check if the user has permission or if there is a timer running on his name. If so it means he is in a time out. which we will see later.

Step 13: Sequence for Alcohol Measurement

Sequence for Alcohol Measurement

In this step I will measure my alcohol and see if I didn’t drink to much and link it to a user. Now I can save the data. I need the biggest numbre so in a for loop of 5 seconds I will check for the biggest value and save it. I also insert LCD commands for visuals.

The datarepo will be called for acces check or to get the user name. In the next step we’ll check if u are to drunk.

Step 14: Did You Drink to Much?

Did You Drink to Much

In this step you will check if you drank to much. If so, we will set a timer for a timespan you can’t drive anymore or if you are in limits you can drive and the contactor will open. These are the Belgian limits.

Step 15: Alcohol History on Site

Alcohol History on Site

In this step I will explain how I will show my alcohol history on the site. It is not the meaning that I will show a random datadump so in my Datarepository or Java-file I will filter the data and change the names of my data if it is presented in numbers. For example. DeviceID = 2 says nothing about my device or which one it is. So I will change it to Alcohol sensor or the name that is connected to. simply with an if-statement.

First I will make an app route which I will send the jsonified data of my database to. I will look for it with my javascript. because my IP changes constanly I need to use a variable parameter.

Handle Data is a function to extract the data. This I will not explain. You can find it in the code and study it if u want.

In fill table you will see how I get the data and give it another name. I also fill it in my html.

Step 16: Connect and Built!!

Connect and Built!!

Last but not least! Put it all together. I soldered my components on a print PCB. If u have some more time you can get a presoldered PCB online so you can make everything more compact.

Then I placed my items in my box.

Step 17: Drive Safely!!

Drive Safely!!

I hope you enjoyed this Instructable and that you are stimulated enough to get to this. You can find the code underneath here but you do need to fill in the config.py for the sql connection. Thank you for taking the time to read this Instructable and have fun building!!

Download Visual Studio Code and work with an SSH connection (enable in raspi-config). Simply enter a folder on the Pi and clone the repo below.

Download VSC:

https://code.visualstudio.com/?wt.mc_id=vscom_downloads

Repo:

https://github.com/howest-mct/2021-2022-projectone-MaesShankar.git

Shankar Maes


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Scroll to Top