You are on page 1of 4

from flask import Flask

import RPi.GPIO as GPIO


import random
from gpiozero import LED, LightSensor
from time import sleep
import Adafruit_DHT

GPIO.setmode(GPIO.BCM)

pin = 21
GPIO.setup(pin, GPIO.IN)
app = Flask(__name__)
app = Flask(__name__, static_url_path = "/stuff", static_folder =
"static")
t = GPIO.input(pin)
red = LED(2)
blue = LED(3)
green = LED(4)
humidity,temperature = Adafruit_DHT.read_retry(11, 14)

@app.route("/Day_Or_Night")
def DayorNight():
t = GPIO.input(pin)
sleep(0.1)
if t == 0:
myhtml = '''<img src = "/stuff/Day.png"/>'''
elif t == 1:
myhtml = '''<img src = "/stuff/Night.png"/>'''
else:
myhtml = '''<img src = "/stuff/day_night_2.png"/>'''

return myhtml

@app.route("/Temp_And_Humid")
def TempandHumid():
humidity,temperature = Adafruit_DHT.read_retry(11, 14)
myhtml = "<h2>" + str(temperature) +" "+ str(humidity) + "</h2>"
return myhtml

@app.route("/")
def home():

myhtml = '''
<center>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
setInterval(function()
{
$.ajax({
type:"get",
url:"/Day_Or_Night",
datatype:"html",
success:function(data)
{
//Whatever is returned by app.route("/Day_Or_Night") will be
inside the variable "data"
//All the html inside of the <div class="random"> will be
replaced
$( ".random" ).html(data);
}
});
}, 500);//time in milliseconds to update the data
</script>
<h1> Turn the LED ON and OFF</h1>
Here is a time of the day:
<div class="random">
This is the HTML that will be replaced
</div>

<script>
setInterval(function()
{
$.ajax({
type:"get",
url:"/Temp_And_Humid",
datatype:"html",
success:function(data)
{
$( ".temp" ).html(data);
}
});
}, 500);//time in milliseconds to update the data
</script>
<h1> Temperature and Humidity</h1>
Here is the temperature and humidity:
<div class="temp">
This is the HTML that will be replaced
</div>

<p><a href="/red/on"><img src = "/stuff/led_on.png"/><br/>Red


ON</a><br/>
<p><a href="/red/off"><img src = "/stuff/led_off.png"/><br/>Red
OFF</a><br/>
<p><a href="/blue/on"><img src = "/stuff/led_on.png"/><br/>Blue
ON</a></br>
<p><a href="/blue/off"><img src = "/stuff/led_off.png"/><br/>Blue
OFF</a><br/>
<p><a href="/green/on"><img src =
"/stuff/led_on.png"/><br/>Green ON</a></br>
<p><a href="/green/off"><img src =
"/stuff/led_off.png"/><br/>Green OFF</a><br/>
</center>
'''
return myhtml

@app.route ("/<string:color>/<string:state>/")
def onoroff(color,state):
if color == "red" and state == "on":
red.on()
DayorNight()
myhtml = '''
<p><a href="/">Go Back Home</a>
'''
return myhtml
if color == "red" and state == "off":
red.off()
myhtml = '''
<p><a href="/">Go Back Home</a>
'''
return myhtml
if color == "blue" and state == "on":
blue.on()
myhtml = '''
<p><a href="/">Go Back Home</a>
'''
return myhtml
if color == "blue" and state == "off":
blue.off()
myhtml = '''
<p><a href="/">Go Back Home</a>
'''
return myhtml
if color == "green" and state == "on":
green.on()
myhtml = '''
<p><a href="/">Go Back Home</a>
'''
return myhtml
if color == "green" and state == "off":
green.off()
myhtml = '''
<p><a href="/">Go Back Home</a>
'''
return myhtml

if __name__ == "__main__":
app.run(host = '0.0.0.0', port=240)

You might also like