Sie sind auf Seite 1von 3

from flask import Flask

import Adafruit_DHT
import random
from gpiozero import RGBLED
import RPi.GPIO as GPIO
temperaturePin = 4
from time import sleep
app = Flask(__name__, static_url_path = "/image", static_folder = "static")
pin1 = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin1, GPIO.IN)
led = RGBLED(16,20,21)
ledr = 0
ledg = 0
ledb = 0
def LED(Red, Green, Blue):
if Red != 2: #2 is value for unchange
led.red = Red
if Green != 2:
led.green = Green
if Blue != 2:
led.blue = Blue
@app.route("/getTemperature")
def getTemperature():
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
temperatureData = "<h1>" + str(temperature) + "</h1>"
humidityData = "<h1>" + str(humidity) + "</h1>"

return temperatureData + humidityData


@app.route("/getLight")
def getLight():
global pin1
return "<h1>" + str(GPIO.input(pin1)) + "</h1>"
@app.route('/redLED')
def redLED():
global ledr
myhtml = '''
<p><a href="/">Return</a></p>
'''
if ledr % 2 == 0:
LED(1,2,2)
ledr += 1
else:
LED(0,2,2)
ledr += 1
return myhtml
@app.route('/greenLED')
def greenLED():
global ledg
myhtml = '''
<p><a href="/">Return</a></p>
'''
if ledg % 2 == 0:
LED(2,1,2)
ledg += 1
else:
LED(2,0,2)
ledg += 1
return myhtml
@app.route('/blueLED')
def blueLED():
global ledb
myhtml = '''
<p><a href="/">Return</a></p>
'''
if ledb % 2 == 0:
LED(2,2,1)
ledb += 1
else:
LED(2,2,0)
ledb += 1
return myhtml
@app.route('/image')
def thisIsLoli():
myhtml = '''
<img src="/image/e5886745611c49569821e4cc9eab77ce.jpg">
'''
@app.route("/")
def home():
myhtml = '''
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
setInterval(function()
{
$.ajax({
type:"get",
url:"/getTemperature",
datatype:"html",
success:function(data)
{
$( ".temperature" ).html(data);
}
});
$.ajax({
type:"get",
url:"/getLight",
datatype:"html",
success:function(data)
{
if (data >= 0.5) {
$( "#light" ).attr('src', '/image/sunrise.png');
} else {
console.log(data)
$( "#light" ).attr('src', '/image/sunrise.png');
console.log(document.getElementById('light'))
}
}});
}, 500);//time in milliseconds to update the data
</script>

<div class="temperature">
This is the HTML that will be replaced
</div>
<img src="/image/black.jpg" id="light" width="300px">
<a href="/redLED">
<img height="50" src="/image/redOn.png" id="red">
</a>
<a href="/greenLED">
<img height="50" src="/image/greenOn.png">
</a>
<a href="/blueLED">
<img height="50" src="/image/blueOn.png">
</a>
'''
return myhtml
if __name__ == "__main__":
app.run()

Das könnte Ihnen auch gefallen