0% found this document useful (0 votes)
22 views15 pages

Experiment 7 MSK Raspberry Home Automation Flask Server CS

This document discusses setting up a home automation system using a Raspberry Pi. It describes installing the Flask microframework to turn the Raspberry Pi into a web server. It then discusses interfacing LEDs and a buzzer to GPIO pins on the Pi and writing Python code to control the pins from a web interface. HTML and Jinja template code is provided to display the pin states and buttons to toggle them. Running the Python code launches a web server that can control the connected devices remotely.

Uploaded by

Manoj Kavedia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views15 pages

Experiment 7 MSK Raspberry Home Automation Flask Server CS

This document discusses setting up a home automation system using a Raspberry Pi. It describes installing the Flask microframework to turn the Raspberry Pi into a web server. It then discusses interfacing LEDs and a buzzer to GPIO pins on the Pi and writing Python code to control the pins from a web interface. HTML and Jinja template code is provided to display the pin states and buttons to toggle them. Running the Python code launches a web server that can control the connected devices remotely.

Uploaded by

Manoj Kavedia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Home Automation

Er. Manoj S. Kavedia


www.kavediasir.yolasite.com
www.kaizenfuturetech.com
9324258878 / 8329988738
HA - Pin Layout of RPi
HA - Installation FLASK
Flask
 Flask is called a micro framework because it does not require particular tools
or libraries.
 It has no database abstraction layer, form validation, or any other
components where pre-existing third-party libraries provide common
functions.
 However, Flask supports extensions that can add application features as if
they were implemented in Flaskinstall pip itself.
 Python microframework called Flask to turn the Raspberry Pi into web server

To install Flask, We need to have pip installed. Run the following


commands to update your Pi and Install pip:
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get upgrade
pi@raspberrypi ~ $ sudo apt-get install python-pip python-flask
Then, use pip to install Flask and its dependencies:
pi@raspberrypi ~ $ sudo pip install flask
HA - Interfacing LED and Buzzer

GPIO25 - Pin22 - LED

GPIO8 - Pin24 - LED

GPIO7 - Pin26 - LED

GPIO15 - Pin12 - Buzzer


HA - Steps
To keep everything organized, start by creating a new folder:

pi@raspberrypi ~ $ mkdir web-server

pi@raspberrypi ~ $ cd web-server

pi@raspberrypi ~ $/web-server $

Create a new file called app.py. Store the follwoing code in


that file.
HA - Python Code
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)

GPIO.setmode(GPIO.BCM)
# Create a dictionary called pins to store the pin number, name, and pin state:
pins = {
12 : {'name' : 'GPIO 12', 'state' : GPIO.LOW},
22: {'name' : 'GPIO 22', 'state' : GPIO.LOW},
24 : {'name' : 'GPIO 24', 'state' : GPIO.LOW},
26 : {'name' : 'GPIO 26', 'state' : GPIO.LOW} }
# Set each pin as an output and make it low:for pin in pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)

@app.route("/")
def main():
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
# Put the pin dictionary into the template data dictionary:
templateData = {
'pins' : pins }
HA - Python Code
# Pass the template data into the template main.html and return it to the user
return render_template('main.html', **templateData)

# The function below is executed when someone requests a URL with the pin number
and action in it:

@app.route("/<changePin>/<action>")

def action(changePin, action):


# Convert the pin from the URL into an integer:
changePin = int(changePin)

# Get the device name for the pin being changed:


deviceName = pins[changePin]['name']
HA - Python Code
# If the action part of the URL is "on," execute the code indented below:
if action == "on":
# Set the pin high:
GPIO.output(changePin, GPIO.HIGH)

# Save the status message to be passed into the template:


message = "Turned " + deviceName + " on."
if action == "off":
GPIO.output(changePin, GPIO.LOW)
message = "Turned " + deviceName + " off."

# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)

templateData = {
'pins' : pins
}

return render_template('main.html', **templateData)


if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
HA - HTML File
Flask uses a template engine called Jinja2 that you can use to send dynamic
data from your Python script to your HTML file.

pi@raspberrypi:~/web-server $ mkdir templates

pi@raspberrypi:~/web-server $ cd templates

pi@raspberrypi:~/web-server/templates $

Create a new file called main.html.


HA - HTML File
<!DOCTYPE html><head>
<title>RPi Web Server</title>
<!-- Latest compiled and minified CSS -->

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-
1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
crossorigin="anonymous">

<!-- Optional theme -->


<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"
integrity="sha384-
fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r"
crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->


<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
integrity="sha384-
0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
crossorigin="anonymous"></script></head>
HA - HTML File
<body>
<h1>RPi Web Server</h1>
{% for pin in pins %}

<h2>{{ pins[pin].name }}
{% if pins[pin].state == true %}
is currently <strong>on</strong></h2><div class="row"><div class="col-md-2">
<a href="/{{pin}}/off" class="btn btn-block btn-lg btn-default" role="button">Turn
off</a></div></div>

{% else %}
is currently <strong>off</strong></h2><div class="row"><div class="col-md-2">
<a href="/{{pin}}/on" class="btn btn-block btn-lg btn-primary" role="button">Turn
on</a></div></div>

{% endif %} Note :
{% endfor %} Anything in double curly braces within the HTML
</body> template is interpreted as a variable that would be
</html> passed to it from the Python script via the
render_template function.
HA - HTML File
To launch your Raspberry Pi web server move to the folder that contains
the file app.py:
pi@raspberrypi:~/web-server/templates $ cd ..

Then run the following command:

pi@raspberrypi:~/web-server $ sudo python app.py

Web server should start immedi ately!

Open your Raspberry Pi address in your browser by entering its IP address,


in my case: http://0.0.0.0:80
HA - Output of Server Code
HA - Output of HTML
Enjoy
Home Automation

You might also like