Skip to content

Instantly share code, notes, and snippets.

@mikekilpatrick
Forked from joshsisto/pi_weather.py
Created September 2, 2021 03:35
Show Gist options
  • Select an option

  • Save mikekilpatrick/b964cf1eff50c5766739bb4d2ec49dfe to your computer and use it in GitHub Desktop.

Select an option

Save mikekilpatrick/b964cf1eff50c5766739bb4d2ec49dfe to your computer and use it in GitHub Desktop.
from math import log
from flask import Flask, render_template
from sense_hat import SenseHat
app = Flask(__name__)
def weather():
sense = SenseHat()
sense.clear()
celcius = round(sense.get_temperature(), 1)
fahrenheit = round(1.8 * celcius + 32, 1)
humidity = round(sense.get_humidity(), 1)
pressure = round(sense.get_pressure(), 1)
dewpoint = round(243.04 * (log(humidity / 100) + ((17.625 * celcius) / (243.04 + celcius))) / (17.625 - log(humidity / 100) - (17.625 * celcius) / (243.04 + celcius)), 1)
acceleration = sense.get_accelerometer_raw()
x = round(acceleration['x'], 0)
y = round(acceleration['y'], 0)
z = round(acceleration['z'], 0)
if x == -1:
sense.set_rotation(90)
elif y == 1:
sense.set_rotation(0)
elif y == -1:
sense.set_rotation(180)
else:
sense.set_rotation(180)
if fahrenheit > 20 and fahrenheit < 80:
bg_color = [0, 0, 155] # blue
if fahrenheit > 81 and fahrenheit < 90:
bg_color = [0, 155, 0] # Green
if fahrenheit > 91 and fahrenheit < 100:
bg_color = [155, 155, 0] # Yellow
if fahrenheit > 101 and fahrenheit < 102:
bg_color = [255, 127, 0] # Orange
if fahrenheit > 103 and fahrenheit < 104:
bg_color = [155, 0, 0] # Red
if fahrenheit > 105 and fahrenheit < 109:
bg_color = [255, 0, 0] # Bright Red
if fahrenheit > 110 and fahrenheit < 120:
bg_color = [155, 155, 155] # White
else:
bg_color = [0, 155, 0] # Green
result = ' Temp. F ' + str(fahrenheit) + ' Temp. C ' + str(celcius) + ' Hum. ' + str(humidity) + ' Press. ' + str(pressure) + ' DewPoint ' + str(dewpoint)
#for x in range(5):
print(result)
sense.show_message(result, scroll_speed=0.10, back_colour=bg_color, text_colour=[155, 155, 155])
@app.route('/')
def index():
sense = SenseHat()
sense.clear()
celcius = round(sense.get_temperature(), 1)
fahrenheit = round(1.8 * celcius + 32, 1)
humidity = round(sense.get_humidity(), 1)
pressure = round(sense.get_pressure(), 1)
dewpoint = round(243.04 * (log(humidity / 100) + ((17.625 * celcius) / (243.04 + celcius))) / (17.625 - log(humidity / 100) - (17.625 * celcius) / (243.04 + celcius)), 1)
acceleration = sense.get_accelerometer_raw()
x = round(acceleration['x'], 1)
y = round(acceleration['y'], 1)
z = round(acceleration['z'], 1)
return render_template('weather.html', celcius=celcius, fahrenheit=fahrenheit, humidity=humidity, pressure=pressure, dewpoint=dewpoint, x=x, y=y, z=z)
if __name__ == '__main__':
app.run(host='0.0.0.0')
weather()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment