Created
April 16, 2021 11:39
-
-
Save darrenwatt/24a19f54239a16f99da2cdd70fe81335 to your computer and use it in GitHub Desktop.
Code to get temperature of mancave and from denmead weather data and display on inkyphat display on raspberry pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Python file uses the following encoding: utf-8 | |
from inky import InkyPHAT | |
from PIL import Image, ImageFont, ImageDraw | |
from font_fredoka_one import FredokaOne | |
from datetime import datetime | |
import time | |
import requests | |
from bs4 import BeautifulSoup | |
from influxdb import InfluxDBClient | |
import pprint | |
import math | |
import time | |
dbClient = InfluxDBClient(influx-address, influx-port, username, password, 'telegraf') | |
# pull latest temp reading for mancave - this is also logged to influx from another pi with a temp sensor | |
try: | |
manCaveTemp = dbClient.query('select last(*) from temp;') | |
for item in manCaveTemp: | |
data = str(item[0]['last_temp']) | |
except Exception as e: | |
print(type(e), type(e).__qualname__) | |
inky_display = InkyPHAT("black") | |
inky_display.set_border(inky_display.WHITE) | |
img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT)) | |
draw = ImageDraw.Draw(img) | |
font = ImageFont.truetype(FredokaOne, 22) | |
message = datetime.now().strftime('%H:%M') | |
message2 = "Mancave: " + data + "C" | |
# get local weather | |
url = 'https://www.andreoli.co.uk/weather/myweather.htm' | |
response = requests.get(url) | |
doc = BeautifulSoup(response.text, "html.parser") | |
cells = doc.findAll('td') | |
currentweather = str(cells[3]) | |
currentweather_split = currentweather.split() | |
currentweather_split2 = currentweather_split[1].split('>') | |
currentweather_split3 = currentweather_split2[2].split('°') | |
denmead_temp = float(currentweather_split3[0]) | |
# chuck denmead_temp into influxdb | |
json_body = [ | |
{ | |
"measurement": "outdoor_temp", | |
"tags": { | |
"host": "pi3", | |
"sensor": "denmead-website" | |
}, | |
"fields": { | |
"temp": denmead_temp | |
} | |
} | |
] | |
try: | |
dbClient.write_points(json_body) | |
except Exception as e: | |
print(e) | |
denmead_temp = str(denmead_temp) | |
message3 = "Outside: " + denmead_temp + "C" | |
print(message) | |
print(message2) | |
print(message3) | |
draw.text((8,0), message, inky_display.BLACK, font) | |
draw.text((8,22), message2, inky_display.BLACK, font) | |
draw.text((8,44), message3, inky_display.BLACK, font) | |
inky_display.set_image(img) | |
inky_display.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment