[otw_is sidebar=otw-sidebar-1]In this post I will discuss how you can create your Home Automation System using Raspberry Pi. Most of you know that there are a lot of ways in which you can control the Home Appliances using Raspberry Pi via Wi Fi here I am using the web server to automate the home appliances that is the web server is made based on the Raspberry Pi and this web server is used to get control of the AC appliances.

[otw_is sidebar=otw-sidebar-3]

Raspberry Pi based Home Automation System using Web Server with Python:

In the previous post I have discussed that how you create your own webserver based on the Raspberry Pi and how you can access this web server from your Wi Fi devices that is smart phone or laptop or personal computer. This post is the second part of Home Automation System based on the Raspberry Pi based web server. Before reading this post it is recommended that you must the previous post. After reading this post you will be able to make your own home automation based on Raspberry Pi, you will learn how to write the code for web server in Raspberry Pi using Python.

[otw_is sidebar=otw-sidebar-3]

Project Overview:

Before diving into the process it is good to have the overview of what the project is and how it will work. In this Home Automation System the Raspberry Pi will act as the web server, the Raspberry Pi will be connected to your home network that is your router via Wi Fi or via Ethernet cable and your Wi Fi device that can be your smart phone or your laptop is also connected to the same router. The router will assign a unique IP address to the Raspberry Pi and you can write this IP address in the web browser of you Smart phone or your laptop and can access the web page that I had made in the previous post. Don’t worry it is not that difficult as it looks, I have made the post so you can follow the simple steps, it is quite comfortable to study and build your Home Automation System based on Raspberry Pi and web server that is based on the Raspberry Pi. The block diagram of the whole project is shown below.

Raspberry Pi Web Page (HTML template):

pcbway

Here I am assuming that you have studied my previous post and you know how to write the code for web server in the Raspberry Pi using Python IDLE. I am preceding form the file that we have i.e. firstpart.py. This source code file is the web server for the Raspberry Pi written in the Python IDLE but it is missing the HTML template. So I will first write the HTML template so that the resulting Webpage looks more like a webpage. You can write the HTML file in any of the text editor Raspberry Pi has its own text editor called the “LeafPad”.

[otw_is sidebar=otw-sidebar-2]

Step1:

First of all open the LeafPad in your Raspberry Pi.

Step2:

Now you can copy and paste the following lines in your text editor:

<!Doctype html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>Hello, World!</h1>
<h2>the date and time on the server is:{{time}} </h2>
</body>
</html>

Save this file in the sub folder that we have made previously called Template.

Raspberry Pi Web Server Python Code:

After you have made the above file open the file called firstpart.py which we have created previously and then do changes as shown below. Follow the following simple steps.

Step1:

Open the file firstpart.py in the Raspberry Pi.

Step2:

Now make the changes as shown in the code below or copy and paste the following code in this file.
from flask import Flask,

[otw_is sidebar=otw-sidebar-3]

render_template
import datetime
app = Flask(__name__)
@app.route(“/”)
def hello():

now = datetime.datetime.now()
timestring = now.strftime(“%Y-%m-%d %H : %M”)
templateData = {
‘title’ : ‘Hello!’,
‘time’ : timestring
}
returnr
render_template(‘rasppi.html’,**templateData)
if __name__ == “__main__”:
app.run(host = ‘0.0.0.0’, port = 80 , debug = True)

Save this file with any name you want just remember to add the extension .py at the end of the name. Notice in the third line I have used the name “rasppi.html” this is the name of the file that we have made in the LeafPad above. This Python code will cause the Raspberry PI to act as the webserver and also give the data to the HTML template so that our webpage will become comfortable to see. Now connect your smart phone or the laptop to the router to which your Raspberry Pi is also connected and enter the IP address of your Raspberry Pi in the web browser and you will see the web page that we have created.
So far I have just made the HTML file and the Python file. Python code plays the major role to build the web server based on the Raspberry Pi and also gives the data to the HTLM template which you can say organize our web page. But note that I have not discussed the Home Automation Part. Once you are familiar with the Python file and the HTML file, their link with one another, working of the Python file and how to access the web page from your Wi Fi device, it is now time to modify our files to add the functionality of the Home Automation System. You may have guessed that the Python file needs some modification so that we can manipulate the GPIO pins of the Raspberry Pi and the HTML file need some modification so that web page will serve as Graphical User Interface so that we can manipulate the GPIO pins by accessing this webpage from any Wi Fi device connected to the router to which your Raspberry Pi is connected.
HTML file for web page GUI to control the Home Appliances:
Note that as also described earlier the files we have created that is that of Python and HTML are just demo. The complete HTML file for the Home Automation System based on the Raspberry Pi via Wi Fi. This can be thought of as web based Graphical User Interface for the Home Automation System.

<!DOCTYPE html>
<head>
<title>GPIO Control</title>
<link rel="stylesheet" href='../static/master.css'/>
</head>
<body>
<h1>RPi GPIO Control</h1>
<h2> Sensor Status </h2>
<h3> BUTTON ==> {{ button }}</h3>
<h3> MOTION ==> {{ senPIR }}</h3>
<br>

<h2> Actuator Status </h2>
<h3> RED LED ==> {{ ledRed }}</h3>
<h3> YLW LED ==> {{ ledYlw }}</h3>
<h3> GRN LED ==> {{ ledGrn }}</h3>
<br>
<h2> Commands </h2>
<h3>
RED LED Ctrl ==>
<a href="/ledRed/on" class="button">TURN ON</a>
<a href="/ledRed/off"class="button">TURN OFF</a>
</h3>
<h3>
YLW LED Ctrl ==>
<a href="/ledYlw/on" class="button">TURN ON</a>
<a href="/ledYlw/off"class="button">TURN OFF</a>
</h3>
<h3>
GRN LED Ctrl ==>
<a href="/ledGrn/on" class="button">TURN ON</a>
<a href="/ledGrn/off"class="button">TURN OFF</a>
</h3>

</body>
</html>

Python code Modification for GPIO manipulation through Wi Fi:
As described earlier the previously made Python file “firstpart.py” is just the demo of how the web server based on the Raspberry Pi works along with the HTML file “rasppi.HTML”. now follow the following simple steps to change the file “firstpart.py”.
Step1:
Open the Python IDLE in your Raspberry Pi.

Step2:


Now open the file “firstpart.py”. Now copy and paste the following code in the Python IDLE and overwrite the previous code.

import RPi.GPIO as GPIO
from flask import Flask render_template request
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#define sensors GPIOs
button = 20
senPIR = 16

#define actuators GPIOs
ledRed = 13
ledYlw = 19
ledGrn = 26

#initialize GPIO status variables
buttonSts = 0
senPIRSts = 0
ledRedSts = 0
ledYlwSts = 0
ledGrnSts = 0

# Define button and PIR sensor pins as an input
GPIO.setup(button, GPIO.IN)
GPIO.setup(senPIR, GPIO.IN)

# Define led pins as output
GPIO.setup(ledRed, GPIO.OUT)
GPIO.setup(ledYlw, GPIO.OUT)
GPIO.setup(ledGrn, GPIO.OUT)

# turn leds OFF
GPIO.output(ledRed, GPIO.LOW)
GPIO.output(ledYlw, GPIO.LOW)
GPIO.output(ledGrn, GPIO.LOW)

@app.route("/")
def index():
# Read GPIO Status
buttonSts = GPIO.input(button)
senPIRSts = GPIO.input(senPIR)
ledRedSts = GPIO.input(ledRed)
ledYlwSts = GPIO.input(ledYlw)
ledGrnSts = GPIO.input(ledGrn)
templateData = {
'button' : buttonSts,
'senPIR' : senPIRSts,
'ledRed' : ledRedSts,
'ledYlw' : ledYlwSts,
'ledGrn' : ledGrnSts,
}
return render_template('index.html', **templateData)

@app.route("/<deviceName>/<action>")
def action(deviceName, action):
if deviceName == 'ledRed':
actuator = ledRed
if deviceName == 'ledYlw':
actuator = ledYlw
if deviceName == 'ledGrn':
actuator = ledGrn

if action == "on":
GPIO.output(actuator, GPIO.HIGH)
if action == "off":
GPIO.output(actuator, GPIO.LOW)

buttonSts = GPIO.input(button)
senPIRSts = GPIO.input(senPIR)
ledRedSts = GPIO.input(ledRed)
ledYlwSts = GPIO.input(ledYlw)
ledGrnSts = GPIO.input(ledGrn)

templateData = {
'button' : buttonSts,
'senPIR' : senPIRSts,
'ledRed' : ledRedSts,
'ledYlw' : ledYlwSts,
'ledGrn' : ledGrnSts,
}
return render_template('rasppi.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)

This Python code imports the GPIO Python library and the flask micro framework. Now save this file with whatever name you want just remember to add the extension “.py” at the end of the name. So by following the above mentioned steps you can very easily create your own Home Automation System based on the Raspberry Pi web server.
That is all for now I hope this article would be very helpful for you. In the next article I will come up with more interesting applications of the Raspberry Pi. Till then stay connected, keep reading and enjoy learning. Raspberry Pi based Home Automation System using Web Server with Python code