HI everyone, so this is a mini--project that I did this semester. This is extremely easy once you get the hang of python language. I suggest you to study basics in python before going ahead with the main program.
Components Required
- Raspberry Pi
- 8 Channel Relay (12V/ 7A/ 240 V AC)
- Bulb (60W)
Description of components:
Raspberry Pi
The Raspberry Pi is a credit-card sized computer that can plug into your TV and a keyboard which was found for the intention of teaching basic concepts in schools. It is a portable little computer which can be used in electronic projects, and for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. It has an IEEE 802.5 standard Ethernet card and also MAC – Media Access Control which is being controlled by a chip called as the ASIC – Application Specific Integrated Circuit. By using the Ethernet card we can communicate through the internet with an approximate speed of data transfer of 100 MB/s.
It is an ARM based Processor designed and fabricated by QCOMM international by using 32nm CMOS technology. The operating speed of the processor is 700MHz and overclocking of the processor is also possible. The Processor is capable of multi-tasking and gaining control of the GPIO pins. We can take video out using HDMI – High Definition Media Interface.
Procedure:
First we boot the software of noobs from the raspberry pi official website on to the SD card. Then, we get connect the raspberry pi to the monitor and keyboard. At once, we get the details of the process taking place inside the raspberry pi. We type the username and password as
Username: pi
Password: raspberry
The IP address is obtained by typing "ipconfig".
In order to use the GUI of the controller, I connect the controller to the laptop with remote desktop connection. I typed the IP address to the list area in remote desktop address.
after this, prepare a ASK file and then type the main program and call the HTML program. The entire program is run in python language
MAIN PROGRAM:
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 = {
24 : {'name' : 'light', 'state' : GPIO.LOW},
25 : {'name' : 'light2', '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
}
# 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']
# 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."
if action == "toggle":
# Read the pin and set it to whatever it isn't (that is, toggle it):
GPIO.output(changePin, not GPIO.input(changePin))
message = "Toggled " + deviceName + "."
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
# Along with the pin dictionary, put the message into the template
data dictionary:
templateData = {
'message' : message,
'pins' : pins
}
return render_template('main.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=800, debug=True)
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 = {
24 : {'name' : 'light', 'state' : GPIO.LOW},
25 : {'name' : 'light2', '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
}
# 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>/<
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']
# 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."
if action == "toggle":
# Read the pin and set it to whatever it isn't (that is, toggle it):
GPIO.output(changePin, not GPIO.input(changePin))
message = "Toggled " + deviceName + "."
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
# Along with the pin dictionary, put the message into the template
data dictionary:
templateData = {
'message' : message,
'pins' : pins
}
return render_template('main.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=800, debug=True)
HTML PROGRAM
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><META http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body> | |
| <div> | |
| <h1>Device Listing and Status</h1> | |
| {% for pin in pins %} | |
| <p>The {{ pins[pin].name }} | |
| {% if pins[pin].state == true %} | |
| is currently on (<a>turn off</a>) | |
| {% else %} | |
| is currently off (<a>turn on</a>) | |
| {% endif %} | |
| </p> | |
| {% endfor %} | |
| {% if message %} | |
| <h2>{{ message }}</h2> | |
| {% endif %} | |
| </div> | |
| </body></html> link to the website is: Iqube http://blog.iqubean.in/home-automation-using-iot/Iqube |
No comments:
Post a Comment