Introduction: Smart PC Case

About: Student MCT at Howest (Kortrijk, Belgium)

A few years ago I built my first desktop computer, back then there wasn't such a hype about RGB and monitoring/controlling your PC. A while ago I already started working on my own smart pc case a few years ago but never made something that I really used.

Since September 2020, I started my bachelors degree Multimedia & Creative Technologies (MCT) at Howest Kortrijk (Belgium). After the first 2 semesters, we already have to built our own project with everything we learned already.

This is how a dream/side-project became reality as my first project in MCT.

Which features does my project have?

  • Turn on the computer from the web app
  • Monitoring the power usage & temperature
  • Controlling the fan speeds
  • And of course: controlling some led strips

Step 1: What You Will Need

Parts needed:

1x Raspberry Pi 4 (4GB RAM recommended)

1x Raspberry Pi USB-C power supply

1x Micro SD card (32GB recommended)

1x 12V power supply

1x DS18B20 One-Wire temperature sensor

1x ACS712 20A Current measuring module

1x ZMPT101B AC Voltage sensor

1x I2C LCD display (16 characters x 2 rows)

3x MOSFETs (or more depending on how many led strips and fans you want)

1x led strip (or more if you want)

1x case fan (or more if you want)

1x 230V power connector

1x 12V power connector

2x prototyping PCB's

Some nylon bolts en nuts

Some wires rated for 12V and 230V

Tools needed:

- Soldering iron

- Soldering tin - ...

BOM:

All parts can also be found in my Bill Of Materials, per part I listed 2 online shops where you can buy and the price I paid for it. As you can see, you will need a budget of around €220 to recreate this project.

Step 2: The Circuit Diagram

This is the full circuit diagram for the project like a created it. You can of course add as many outputs as you want depending on your needs.

You can make this on a breadboard, but I prefer soldering it to a prototyping PCB since you end up with a bunch of connections.

Warning:

Also, be sure to use the correct voltage sources, some components (like the LCD and the Arduino) need 5V, but the one-wire temperature sensor needs 3.3V, so be sure to double-check before powering up.

When connecting the 230V AC wires to the sensors, be really careful so that no copper is exposed and you don't make any mistake, since 230V can kill you when not handling it with the proper caution.

Some information about the connection with the Arduino:

Since the Arduino operates on 5V and the Raspberry Pi on 3.3V, I also added a voltage divider to prevent the Raspberry Pi from getting too much voltage on its RX pin.

Step 3: Wiring Everything Up

Since my project has a lot of electronics, I decided to solder everything on 2 prototyping PCB's as you can see in the images.

On the first (smaller) print, I mounted the 2 AC sensors and hooked them up to an Arduino. To easily connect the needed wires to the Raspberry PI, I used JST connectors that can't be plugged in in the wrong direction.

The second print then contains all MOSFETs, a 5V input from the Raspberry Pi, and again some connectors to external components like the temperature sensor and the I2C LCD.

To make things easier to mount in the case, I made some holes to mount it on an HDD tray which can then slide into the PC case. Also, an advantage of this is that your electronics won't touch any metal parts of your case when mounted on plastic brackets.

Step 4: Installing the Raspberry Pi

Before we can run the code on the PI, we need to install an operating system on it and install all the necessary dependencies.

1) Download the Raspberry Pi OS image

You can download the official Raspberry PI image from there website: https://www.raspberrypi.org/software/operating-sy...

I recommend downloading 'Raspberry Pi OS with desktop'

2) Burn the image on the SD card

After you downloaded the image, you can burn it onto an MicroSD card with at least 8GB of memory.

Handy tools for this are Win32DiskImager and Etcher.

3) Some important steps before booting

When the writing process is done, you should see a 'boot' disk entry in your file explorer.

- Open this disk with a file explorer on your computer

- Create an empty file named 'ssh' (without extension) and put in the root folder of the 'boot' disk

- Open the cmdline.txt file with notepadd++ (or another basic text editor) and add 'ip=169.254.10.1' to the end of the file

- Save the file and eject the SD card

- Now you can put the SD card in the Raspberry PI

4) First boot

- Before booting the Pi, connect an ethernet cable directly from your computer to the Pi

- Now you can boot the Raspberry Pi for the first time

- Open a new terminal window and connect to ssh with the following command:

ssh pi@169.254.10.1

- If you get a warning about the certificate, you can accept by entering 'Y' or 'YES'

- Now you are prompted to enter the password: the default pi password is 'raspberry'

5) Some basic configuration

- Enable the Serial port, I2C and 1-Wire bus with the following command:

sudo raspi-config

- Once in the configuration tool, you need to select option 3: Interface Options

- There you can enable the needed ports.

- After doing these changes, you will be prompted to reboot the Pi.

While the pi is rebooting, you can drink a coffee ;)

6) Installing the latest updates

sudo apt update
sudo apt upgrade
sudo apt dist-upgrade
sudo apt autoremove

If you are prompted a confirmation, just press 'Y'

This can take some time again, so if you want you can take a second coffee :)

6) Installing the needed software

sudo apt install apache2 mariadb-server mariadb-client

- Secure the installation by running the following command and following all steps:

sudo mysql_secure_installation

- Open a mysql shell on the pi:

sudo mysql

- Create a new user to use for the application:

create user username@localhost identified by 'password_here';

- Give the newly created user all permissions:

grant all privileges on *.* to username@localhost;

- Flush the privileges:

flush privileges;

- Exit the MySQL shell:

exit;

Step 5: The Database

All the data collected by my project will be saved in a SQL database with some relational tables.

You will need to import the database into your MariaDB installation on the Raspberry Pi, in order to do this, you can download the database dump to your computer. After that, you can open MySQLWorkbench and create a remote connection to the database server on the Raspberry Pi. Finally, you can open this connection and click 'Data Import/Restore' under 'Administration' in the left column to import the provided .SQL file.

Here is some information about the different tables I created:

Category:

A category is a group that holds some sensors or outputs, which can then be shown on the dashboard as one card. The slug field is a field that can be used in the backend to easily find a group of items independent of the group's name. It's also possible to set an icon that will then be displayed on the dashboard page.

Please note: the categories provided in the database dump are the minimum required categories, deleting a category can cause errors in the backend since in some places a categories slug is referenced in the backend with a hardcoded string.

Sensor:

This is the representation of a physical sensor or input, for example, a temperature sensor or button. Some extra fields like 'unit' are provided to display in the frontend.

Output:

The output table stores all outputs which need to be driven by the Raspberry Pi. In this way, you can add as many led strips or fans like you want as long as you have enough GPIO. There are again some extra fields to improve the frontend & to know if the output is binary or PWM.

OutputHistory & SensorHistory:

These tables are the tables where you can find the historical data which has been measured or sent out to the corresponding output. In that way, you can create some nice charts in the frontend.

User: (not implemented)

Of course, we want only the authorized persons to control your PC Case, so authentication is needed. Before you can do something, you need an enabled account that is protected with your personal password.

Step 6: Installing the Software

You can clone the code from my GitHub: https://github.com/guillaumedeplancke/SmartPCCase

After you've done that, you can save it somewhere on the raspberry pi (in your home directory for example)

The folder you downloaded consists of 3 important sub-folders:

- backend: the Flask API and Socket.IO server written in Python

- frontend: the frontend written in pure HTML, CSS & Javascript

- arduino: the code needed for the Arduino which reads the current and voltage values

Important step: install the python dependencies

pip3 install -r requirements.txt

You're ready to go now, you can test if everything works by running the app.py file.

/bin/python3 <project_directory>/Code/backend/app.py

Step 7: The Arduino Software

The Arduino which is connected to the voltage and current sensor needs a little bit of code I wrote which you can also find in my GitHub repository under the '/arduino' folder.

Just upload this sketch and connect the voltage sensor to A0 and the current sensor to A2. To send the data over the serial port, you can pull the D13 pin high or connect it to GPIO18 on the Raspberry Pi.

What does the code do?

It constantly takes the analog values and then calculates the RMS values and sends them to the serial bus as long as the D13 print is high.

Step 8: The Frontend

After you installed the software requirements on the Raspberry Pi, you should see the default apache page when you browse to its IP address on your network.

To display the frontend I created for this project, we need to do some little steps.

First, make sure you have root permissions if you don't:

sudo -i

Then you can open the apache2 default configuration file:

nano /etc/apache2/sites-available/000-default.conf

When you are in the configuration file, you should see a line starting with 'DocumentRoot', change that line to the following:

DocumentRoot <project_directory>/frontend

Now we can close and save the file again:

CTRL + X
Y

Lastly, we want to restart the webserver to make the changes active:

service apache2 restart

Step 9: Automating the Software (optional)

If you want the backend to start when you boot the pi, you can create a service that will start after you boot the Pi.

Create the file and put the content from the gist in it:

nano /etc/systemd/system/smartpccase.service

Don't forget to replace the placeholders with the correct directory and username!

Save the file:

CTRL + X
Y

Start the service:

sudo systemctl start smartpccase.service

Enable the service to start with the system:

sudo systemctl enable smartpccase.service

Run python code as a service on linux

[Unit]
Description=SmartPCCase
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u /backend/app.py
WorkingDirectory=/backend/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=
[Install]
WantedBy=multi-user.target

Step 10: Outro

If you have a question about my project or want to say something else, don't hesitate to leave a comment.

Please not that I can't provide full support on this when you have technical issues, but I will do the best to help you further!