Introduction: SmartHome With Raspberry Pi
For this project I made a SmartHome that can be operated by a website and mobile. For this I use the Raspberry PI as a database and webserver.
If you want to start this, you need several things:
- 5 white leds (5mm)
- 1 one wire temperatures sensor
- 1 LDR (light dependent resistor)
- 2 servo motors
- 1 microSD (for Raspberry Pi)
- 1 Breadboard powersupply
- 1 Raspberry Pi 3 Model B+
- 3 Foam plates
- 1 Stepper motor (5V)
- 1 RFID-RC522 reader
- 8 resistors (220 Ohm)
- 1 resistor (10K Ohm)
- 2 Breadboards
- 2 packs of jumperwires
- 1 16x2 LCD display
- 1 PCF8574AN
- 4 small windows (3D printed)
- 1 door (3D printed)
- 2 large windows (3D printed)
- 1 garage door (3D printed)
If you need all of this to buy, the maximum cost will be around €150
Step 1: Wiring
The easiest way to start is with the wiring so that you already have the basics, with this method you can easily check if everything works when you are writing the code.
On this way, you can see if you have enough pins on the Raspberry Pi to connect everything. In this case I used the PCF8574AN to control my LCD with less GPIO pins.
To draw the scheme I used Fritzing. It is a handy program where you can view your cabling in a well-organized way.
As you can see on the second photo there are a lot of cables so you still have to work in an organized way.
Step 2: Housing
For the housing I used foam boards as walls. I used a knife to cut the boards in the desired shapes. The windows, doors and garage door are 3D-printed. Of course I drew the house in advance so I knew what dimensions I had to use.
I used SketchUp to draw the house.
I used a glue gun to keep the walls straight and hold them together, If you can see on the photos, the window and garage door are attached with glue so it would be strong enough.
The black box on the 3th photo is a box I used to transport so that everything stays intact
Step 3: Database
First of all, you need to design the database using Mysql Workbench. If this is succeeded, you need to install the Mysql database on the Raspberry Pi.
The first stap you take is to check if your Pi is updatet. You can use the following command:
sudo apt-get update
sudo apt-get upgrade
Now you can install the Mysql server:
sudo apt-get install mysql-server
If the Mysql server is installed, Install the Mysql client
sudo apt-get install mysql-client
If you now look at the sql server by the command:
You can now import your database code by opening the .mwb file with the sql workbench and forward engineer.
You copy the code and paste this in the mysql from the Raspberry. The database is made.
For the user to get all the permissions, just add your username in the table
grant all privileges on smarthome.* to 'yourname'@'%' identified by 'yourname';
ofcourse you need to refresh the table now
To check this you can simply try:
select * from historiek;
In the user table the names of the users come together with their badge, here you can add new users.
In the devices table you can find all the active sensors with their id.
The historiek table shows everything that is happening like the temperature sensor, badge with the status of the garage door and more.
Step 4: Setup
To set the image on the Raspberry Pi you can use Putty, this is a free program.
You can find the base image file here: https://www.raspberrypi.org/downloads/raspbian/
Of course you need to enable some interfaces on the Pi. First go to the config page.
Now you can go to the categories 1-Wire and Spi and both enable them. You will need these for the temperature sensor.
Follow the next steps to get wifi on the Pi.
First log in as root
Then fill in the name and password of your wifi network
wpa_passphrase="wifiname" "password" >> /etc/wpa_supplicant/wpa_supplicant.conf
Then enter the WPA client
select the interface
Now reload the config
And now you can check if you are connected
The first thing to do is to update the latest versions
sudo apt update
For python we install and make sure that the Pi is choosing the correct version
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1<br>update-alternatives --install /usr/bin/python python /usr/bin/python3 2
For the webserver to run the Site on, we need to install Apache2
sudo apt install apache2 -y
Some python packages need to be installed as well
if there are problems with a package that is not found, just right click on it and let it install.
Step 5: Code
For the backend, we write the code in python and using pycharm to write in. The routes from the backend are possible to check with postman. With this app you can use the POST and GET methods. In the backend I used multithreading so everything is running in the background and can work together. To set the image on the Raspberry Pi you can use Putty, this is a free program.
You can find the code for the frontend and backend at