Untitled

unknown
python
2 years ago
3.6 kB
8
Indexable
Never
```# -*- coding: utf-8 -*-
from sense_hat import SenseHat
import RPi.GPIO as gpio
import os, time

sense = SenseHat()
#sense.show_message("test 1")

#no colour
b = [0, 0, 0]
#setting colours
red = [255, 0, 0]
white = [255,255,255]
blue = [0, 0, 255]
green = [0, 255, 0]
pink = [228, 35, 157]

def heart():
sense.set_pixels([b, r, b, b, b, b, r, b,
r, r, r, b, b, r, r, r,
r, r, r, r, r, r, r, r,
b, r, r, r, r, r, r, b,
b, b, r, r, r, r, b, b,
b, b, b, r, r, b, b, b,
b, b, b, b, r, b, b, b])

@app.route("/temp/")
def temp():
#creating temp variable
temp = sense.get_temperature()
#round it to 2 decimal places
temp = round(temp, 2)
#return string of temperature
temp = str(temp)
#display message in text box
return("Temperature is: " + temp)

@app.route("/humidity/")
def humidity():
humidity = sense.get_humidity()
#round to 2 decimal places
humidity = round(humidity, 2)
#reading humidity in percentage on sense_hat
#multiplying sense_hat range to display right amount of pixels
humidity_value = 64 * humidity / 100
#return string of humidity
humidity = str(humidity_value)
#display message in text box
return("Humidity is: " + humidity)

@app.route("/pressure/")
def pressure():
pressure = sense.get_pressure()
#round to 2 decimal places
pressure = round(pressure, 2)
#dividing by 20 to display the pixels in sense_hat range
#max 1260hPa / 20 = 63 pixels
pressure_value = pressure/20
#return string of pressure
pressure = str(pressure)
#display message in text box
return("Pressure is: " + pressure)

@app.route("/<action>")
def action(action):
if action == "green":
#used sense.clear and then set RGB colours, as it clears the display prior to showing the colour
sense.clear(green)

elif action == "red":
sense.clear(red)

elif action == "blue":
sense.clear(blue)

elif action == "pink":
sense.clear(pink)

elif action == "smile":
sense.clear()
sense.show_message(":)")

elif action == "heart":
sense.clear()
heart()

elif action == "temp":
temp = sense.get_temperature()
print(temp)
#round it to 2 decimal places
temp = round(temp, 2)
#return string of temperature
temp = str(temp)
sense.clear()
#set temp value with the right amount of pixels
pixels = [red if i < temp_value else white for i in range(64)]
#display the colours on sense_hat according to the temp value
sense.show_message(pixels)

elif action == "pressure":
pressure()
pressure = sense.pressure
pressure_value = pressure / 20
pixels = [green if i < pressure_value else white for i in range(64)]
sense.set_pixels(pixels)

elif action == "humidity":
humidity = sense.get_humidity()
humidity = round(humidity, 2)
humidity_value = 64 * humidity / 100)
pixels = [blue if i < humidity_value else white for i in range(64)]
sense.set_pixels(pixels)

elif action == "off":
sense.clear()

if __name__ == "__main__":
app.run(host = "0.0.0.0", port = 5002, debug = True)```