Introduction: SmartHome With Raspberry Pi

About: student MCT Kortrijk

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.

Supplies

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

and

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:

sudo mysql

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

FLUSH PRIVILEGES;

To check this you can simply try:

use smarthome;
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/

Interfaces

Of course you need to enable some interfaces on the Pi. First go to the config page.

sudo raspi-config

Now you can go to the categories 1-Wire and Spi and both enable them. You will need these for the temperature sensor.

Wifi

Follow the next steps to get wifi on the Pi.

First log in as root

sudo-i<br>

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

wpa_cli

select the interface

interface wlan0

Now reload the config

reconfigure

And now you can check if you are connected

ip a

Packages

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

  • Flask
  • Flask-Cors
  • Flask-MySQL
  • Flask-SocketIO
  • PyMySQL
  • Python-socketIO
  • requests
  • pip
  • gpio
  • Gevent
  • Gevent-websocket

if there are problems with a package that is not found, just right click on it and let it install.

Step 5: Code

backend

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.

frontend

On the frontend there are a few buttons who can turn on lights, opens the garage port and door. By using javascript and CSS the style from the buttons changes when they are active. There is also a live temperature and a chart with the past temperatures. At the userpage you can see the various users, you can also add a user to the database and there is a user history where you can see who opened or closed the garage door as last.

You can find the code for the frontend and backend at

https://github.com/NMCT-S2-Project-1/nmct-s2-proje...