Spis treści:
Smart home systems are becoming increasingly common in our households. Control of appliances, security and monitoring of our four walls is an interesting and convenient solution, but due to high prices of ready-made systems, many people choose to install only modules for controlling appliances at home.
Today, we will build a system that will allow you to control your home over the Internet!
The main assumptions of the project:
- Controlling devices connected to the power grid,
- Temperature measurement,
- Simple and comfortable design,
- Controlling and checking the status of devices from anywhere in the world.
What will we need?
To make the system easy to use via the website, we will be using the Raspberry Pi Zero W – a minicomputer worshipped by the electronics industry – a device that will surprise you with a positive price/quality ratio. The model with the letter “W” is equipped with Wi-Fi and Bluetooth, so we do not have to worry about connecting to the Internet. In addition to the minicomputer, we will have to buy relays for controlling network devices – here we will use ready-made Grove modules, which we will connect to the Raspberry Pi shield. This way we will minimise the problem of wiring. To measure the temperature at home (or outside, the choice is up to you), you need a temperature sensor. We will use the popular DS18B20.
You will find the exact shopping list below:
- Raspberry Pi Zero W
- Memory card
- 5V power supply, microUSB
- Grove Base Hut for Raspberry Pi Zero
- Grove relays – 4 pieces
- Cables connecting relays to the RPi cover – 4 pieces
- DS18B20 temperature sensor
- The cables to connect the DS18B20 with RPi
- 4.7k Ohm resistor
- 2 × 20 male Goldpins
In addition to the electronic components, we need a soldering iron, wire cutters and of course the enthusiasm to create our own system!
Preparing Raspberry Pi for the work
Like PCs or laptops, Raspberry requires an operating system. However, we do not upload the system to the hard disk, but to the microSD card. Let’s start by downloading the system image from the Raspberry Pi website.
We have three versions available here. In the lower left corner is the Lite version, the minimal version without graphical interface. The upper right corner shows the version with graphic interface, and the upper left corner – with graphic interface and recommended software. We download the latter because most of the programs we will need are already installed on the system by default, so we do not waste time downloading and installing additional software. When the package with the system image is downloaded, we can then insert the microSD card into the slot so that it can be seen by the computer to which the package was downloaded.
Now we want to upload the system to the memory card. Again, the operation is very simple. We start by downloading Etcher. Etcher is a software that allows you to seamlessly upload system images to memory cards. Once you have downloaded and installed the program, click on “Select Image” and select the location of the downloaded file with the system. Then click on “Select Drive” and choose our memory card. Finally, click on ” Flash! ” and wait until the system image is uploaded to the memory card.
After uploading the system, start the wireless communication. We open the Boot directory stored on the memory card and create there an empty file without an extension, named ssh. To avoid problems, make sure that the system shows file extensions. We can change it by clicking “View” and then “Options”. Deselect “Hide extensions for known file types”.
Now we can right-click in the folder, choose “New”, then select “Text Document” and name it “ssh” and then delete the .txt extension.
The final step in establishing proper communication is to connect to the Wi-Fi network, i.e. to tell your Raspberry which network it should connect to. This process is also simple – in the Boot folder we create a text file wpa_supplicant.conf. We open it with a text editor (we can do this with the system’s notebook) and enter this content:
country=PL
update_config=1
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid=”Wpisz tutaj swoją nazwę sieci”
psk=”Tutaj wpisz swoje hasło”
key_mgmt=WPA-PSK
}
In “ssid” we enter the name of the network, and in “psk” – the password. Be careful not to make any typos here, because if any occurs, Raspberry Pi will not be able to connect to the network. Then save the file and insert the card into your Raspberry, and connect the power supply first to the socket and then to the Raspberry microUSB connector provided for the power supply. Now the magic begins!
Connecting the Raspberry to your PC
Although you can connect a monitor, keyboard and mouse to your Raspberry Pi, in this project we will control this minicomputer via its bigger brother, i.e. a PC or a laptop.
Let’s check if Raspberry has already connected to the wireless network! To do this, we need to go to the router’s configuration panel (usually at 192.168.1.1) and look at the devices connected via Wi-Fi. If you see a device called “raspberrypi”, the connection is fine. If Raspberry did not connect, check if you did not mistype anything in the wpa_supplicant.conf file – this is usually the cause of the connection problem.
Let’s install Putty now. It is a client of the Telnet and SSH services, which allows us to connect to Raspberry and control it using a terminal. The program can be downloaded from the software page. After installation, turn on the program, enter the local address of your Raspberry under “Host Name”, enter 22 under “Port” and then click on “Open”.
When we click “Open” we will see the login screen. The default login details are:
login – pi
password – raspberry
We enter these details to log in and then we should be welcomed by this view:
Now we install Apache. It is a web server with a database and a PHP interpreter. To install the necessary programs we have to update the system. We do this by entering three commands one after the other:
sudo apt update
sudo apt upgrade
Server configuration
Now we have to install Apache. To do this, simply type the command below into the terminal:
sudo apt install apache2 -y
When we get a message in the terminal that the installation is complete, we can check if the server is working. To do this, enter the local address of your Raspberry – in my case it is “192.168.1.2” – and confirm with Enter. We should see the following view:
To be able to read PHP script pages now, we need to install a PHP interpreter. We type into the terminal:
sudo apt install php -y
Once the installation is complete, we recommend restarting Apache. To do this, enter the command below:
sudo service apache2 restart
After restarting, install the software with which the databases are to be operated. The way we use the database is trivial, but thanks to the use of databases we can add various conveniences and statistical tools later. To add a database tool together with the software that allows you to manage them in php, we need to type the commands:
sudo apt install mariadb-server -y
sudo apt install php-mysql -y
If the installation is successful, we will need to reset the server again for the changes to take effect. Now we have to create a new user for the database. To do this, we need to log in to the default user account root. To log in, we use the following command:
sudo mysql –user=root
Now we see that the system console moves to MariaDB, our database interpreter. Let’s create a new user – we will call them “domownik” with the password “domek”.
CREATE USER ‘domownik’@’localhost’ IDENTIFIED BY ‘domek’;
In order for our newly created user to be able to manage the databases, we need to give him permissions, so enter the command:
GRANT ALL PRIVILEGES ON *.* TO ‘domownik’@’localhost’;
Now click CTRL +C to exit the program (this shortcut terminates the running process; we can safely use it in other situations as well).
Now we will install a graphical interpreter to process databases. It is called phpMyAdmin and will make our work with databases much easier. Thanks to the phpMyAdmin software, we can create databases using a convenient graphical interface. For the installation we use the already known command “sudo apt install”, but this time we add “phpmyadmin” at the end, so that the command looks like this:
sudo apt install phpmyadmin
The installer will now start. In the first window we select our environment, i.e. apache2, in the next window we click on “ok”, and in the last window we select “no”.
Now let’s perform one more operation that will give us access to phpMyAdmin under /phpmyadmin. To do this, type the command:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Finally, we reset the server and go from the second computer connected to the same network to the address: rpi_local_address/phpmyadmin (of course we have to enter our real address, in my case 192.168.1.2/phpmyadmin). We confirm the operation with Enter. Now we should see a screen like below. We will use the data of our “domownik” to log in.
Now let’s create a database. Click on “New” in the upper left corner, fill in the fields as below, call the base “control” and set the character encoding to utf8_general_ci.
We will now see the table creation tool. Set “przekazniki” as the name and choose “2” as the number of columns.
After approval, the program will take you to the setting of columns that should be completed as below.
Once you have created the table, click the “SQL” button on the top bar and enter the following query into the text field. Then click “Go”.
INSERT INTO `przekazniki`(`przekaznik`, `stan`) VALUES (‘przekaznik1′,’0’), (‘przekaznik2′,’0’), (‘przekaznik3′,’0’), (‘przekaznik4′,’0’)
Today we set up the server, configured the database and created the table that allows us to check the status of the relays. The next part of the guide will help you create a test layout and upload a working website.
The second part of the guide can be found here.
How useful was this post?
Click on a star to rate it!
Average rating 0 / 5. Vote count: 0
No votes so far! Be the first to rate this post.