You got the Raspberry Pi and ready to make your DIY home project, right? How good if you get to know about an IoT (Internet of Things) home automation projects to control your house stuff like CCTV, Fan, and Lights? Well, IoT is not as challenging project as the general perception people are having about. In fact, it provides quick and easy ways to make communication between different devices on the web. Today, I will show you how you can build a Raspberry Pi home automation project for your daily home tasks.
What will you get at the end of this Pi Automation Project?
Using the Raspberry Pi, web server, internet, and some other hardware, you will control the lights and fans in your living room. You will also be able to monitor the functioning of the CCTV camera you placed in front of your main door. All these can be done remotely without going near to the devices or to stay near the plugs to on/off the switches.
Excited? Then, let’s get to the project.
How does it work?
The project will be based and work on two sections as any other web application does; Client and Server. Here, the client is the Raspberry Pi setup you made with the relay board. The already coded Python program will run on the Pi and sends the request to the server (web interface) which contains the User Interface and buttons. This setup helps you manage and control the things in your home like lights, fans, CCTV, etc.
The code sends the URL requests to the server using the library functions. This URL is nothing but the PHP file which is a form of an API which performs the task to analyze the contents of the .txt file. At this time, the Raspberry Pi checks all of the program lines consistently. If the request is true, then the server responds with the help of the relay and GPIO pins to On/Off the switch of the respective object.
Raspberry Pi Home Automation Project
Step-1: Gather required tools
First of all, you need to have all the essential and required components to create the project setup. For the list of the parts you will need, check the hardware and software sections below:
- The fastest and smartest Raspberry Pi board (here, I got the Pi 3 model B)
- An SD card (8GB recommended) – Check Resources
- A USB WiFi adapter (I took Edimax EW-7811Un) – Check Resources
- A Relay channel module (8-channel board recommended)
- Prototyping board
- An HDMI monitor (optional)
- Transistor, diode, flex cable, and jumper wires
- Soldering iron
- Cable blocks, knife, and screwdriver
- Power supply – Check Resources
- Raspberry Pi keyboard and mouse (optional) – Check Resources
- Raspberry Pi Operating System (I used Raspbian)
- Software tools like FileZilla/PuTTY/Nmap (optional)
- Language like Python/PHP/HTML/CSS
Step-2: Making the connections with relay
It is the time to connect the Raspberry Pi and other devices with the relay module. I took 8-channel relay board for this project, but you can take as per your needs.
Depending on the equipment you want to operate with the Pi, take the number of jumper wires. Here I will show how you can create connections for managing lights at your home.
- Connect the Raspberry Pi 3 with the relay using Red (positive) and Black (negative) cables.
- Also, make the ground connection.
- Cut the flex cable of the lamp which contains Brown and Blue wires inside. The Brown is for the live connection, and Blue is for neutral.
- Wring the ends of the wires, make proper insulation and connect the Blue ends of the wire with each other. You can use a soldering iron or cable blocks for this.
- Now, connect the Brown wire with the relay and tight it with the help of screws.
It’s testing time now.
Install the Operating System on the Raspberry Pi and also install Python language. You have two options to choose from; either the keyboard & mouse or the ssh tools like PuTTY & NMAP to connect with your Pi device.
Now, in the Raspberry Pi command terminal, write the program lines as shown below:
import RPi.GPIO as GPIO
Writing these lines allows importing the GPIO library for using on the Pi. The second pin on the GPIO will be used as our first pin out with which we will connect the relay board using Green wire. The pin output is set to True/False for turning On/Off lights, fans, and other electronic devices in your home.
Step-3: Set the web server
If you have a website, then you can use it as a server and transfer it over to the Raspberry Pi. It will control the IoT devices at your home from anywhere & anytime. Don’t worry if you are new to the programming with PHP, Python, and HTML because this project will require only a few coding lines. You can get help from the online tutorials and coding projects too.
Now, open the web directory in your website manager and create a file named as “lighton.php” and write the code below in that:
system(“echo raspberry | sudo -S python /var/www/PiHome/scripts/lights/lighton.py”); header( ‘Location: ‘raspberrykits.php’ ) ;
Here the location I have made to the homepage named as ‘raspberrykits,’ but you can set it to the light page too. The code here will compile and run the script ‘lighton.py’ within seconds.
After this, create a new file in the website directory. Create the path as mentioned below:
In the file, write the code:
import RPi.GPIO as GPIO
Then, save the script file. Go to your site design and give a hyperlink to ‘lighton.php’ for the ‘On’ button. So, as a result, when you press the button, the script will execute and the light will be turned on.
In the same way, we will execute the turning off program.
Create a file ‘lightoff.php’ in your website directory.
Write the same code as above:
system(“echo raspberry | sudo -S python /var/www/PiHome/scripts/lights/lightoff.py”); header( ‘Location: ‘ raspberrykits.php’ ) ;
Also, create the file:
And write the below lines in that:
import RPi.GPIO as GPIO
Save this Python script file. Give the hyperlink of “lightoff.php” for the ‘off’ button. When this script executes, the Raspberry Pi will turn the lights off.
In the same manner, you can create files and write codes for different electronics components in your home like fans, CCTV, TV, etc. You just need to connect them with relay correctly and execute codes in the way mentioned here.
You can also attach the Raspberry Pi touchscreen monitor with the setup. It will let you check the status of the lights, fans, etc. whether they are turned on or off on the screen. But, it’s optional. You can easily manage this setup without attaching any display.
I hope you got the points. So, what are you waiting for? Go, get your Pi, and start making this useful fun making Raspberry Pi home automation project.
Let me know your thoughts, experience, problems (if you faced any), and the solutions you came up with to successfully operate your home IoT stuff using Raspberry Pi.