The “and the Temperature is” displays as text. The %s displays what’s after the other % sign out side the brackets. % (T) displays the variable T which is the rounded off temperature. Taken from the BME280
T = bme280.get_temperature()
T = round(T)
And the c (more text) to denote degrees Celsius. Have a look at this.
This will work with a BME680 etc, with some code changes.
#!/usr/bin/env python3
import sys
import os
import time, datetime
import RPi.GPIO as GPIO
from PIL import Image, ImageDraw, ImageFont
from unicornhatmini import UnicornHATMini
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
from bme280 import BME280
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
unicornhatmini = UnicornHATMini()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP) # A
GPIO.setup(6, GPIO.IN, pull_up_down = GPIO.PUD_UP) # B
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP) # X
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Y
# button_map
# 5: "A",
# 6: "B",
# 16: "X",
# 24: "Y"}
X = 0
Y = 0
M = 0
def Dim(channel):
unicornhatmini.set_brightness(0.5)
def Bright(channel):
unicornhatmini.set_brightness(1.0)
def Shutdown(channel):
global X
X = 1
def Exit(channel):
global Y
Y = 1
GPIO.add_event_detect(5, GPIO.FALLING, callback = Dim, bouncetime = 2000)
GPIO.add_event_detect(6, GPIO.FALLING, callback = Bright, bouncetime = 2000)
GPIO.add_event_detect(16, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
GPIO.add_event_detect(24, GPIO.FALLING, callback = Exit, bouncetime = 2000)
rotation = 0
if len(sys.argv) > 1:
try:
rotation = int(sys.argv[1])
except ValueError:
print("Usage: {} <rotation>".format(sys.argv[0]))
sys.exit(1)
unicornhatmini.set_rotation(rotation)
display_width, display_height = unicornhatmini.get_shape()
unicornhatmini.set_brightness(0.5)
font = ImageFont.truetype("/home/pi/5x7.ttf", 8)
offset_x = 0
while True:
if offset_x == 0 and M == 0:
text = time.strftime("It's %A %B %-d %-I:%M %p")
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((display_width, -1), text, font=font, fill=255)
r, g, b = (0, 255, 255)
elif offset_x == 0 and M == 1:
T = bme280.get_temperature()
T = round(T)
if T <= 0:
r, g, b = [0, 0, 255] # Blue
elif T > 0 and T < 13:
r, g, b, = [255, 255, 0] # Yellow
elif T >= 13 and T < 25:
r, g, b = [0, 255, 0] # Green
elif T >= 25 and T < 30:
r, g, b = [255, 50, 0] # Orange
elif T >= 30:
r, g, b = [255, 0, 0] # Red
text = ("and the Temperature is %sc") % (T)
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((display_width, -1), text, font=font, fill=255)
elif offset_x == 0 and M == 2:
H = bme280.get_humidity()
H = round(H)
if H < 30:
r, g, b = [255, 0, 0] # Red
elif H >= 30 and H <= 60:
r, g, b = [0, 255, 0] # Green
elif H > 60 and H < 80:
r, g, b = [255, 255, 0] # Yellow
elif H >= 80:
r, g, b = [255, 0, 0] # Red
text = ("with %s%% Humidity") % (H)
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((display_width, -1), text, font=font, fill=255)
elif offset_x == 0 and M == 3:
p = bme280.get_pressure()
P = round(p)
if P > 0 and P < 982: # Very Low
r, g, b = [255, 0, 0] # Red
elif P >= 982 and P < 1004: # Low
r, g, b = [255, 255, 0] # Yellow
elif P >= 1004 and P < 1026: # Mid Range
r, g, b = [0, 255, 0] # Green
elif P >= 1026 and P < 1048: # High
r, g, b = [0, 0, 255] # Blue
elif P >= 1048: # Very High
r, g, b = [255, 50, 0] # Orange
text = ("@ %smb Pressure") % (P)
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((display_width, -1), text, font=font, fill=255)
else:
for y in range(display_height):
for x in range(display_width):
if image.getpixel((x + offset_x, y)) == 255:
unicornhatmini.set_pixel(x, y, r, g, b)
else:
unicornhatmini.set_pixel(x, y, 0, 0, 0)
unicornhatmini.show()
time.sleep(0.05)
offset_x += 1
if offset_x + display_width > image.size[0]:
offset_x = 0
M = M + 1
if M == 4:
M = 0
if X == 1:
unicornhatmini.set_all(0, 0, 0)
unicornhatmini.show()
os.system("sudo shutdown now -P")
time.sleep(30)
if Y == (1):
unicornhatmini.set_all(0, 0, 0)
unicornhatmini.show()
raise SystemExit
time.sleep(30)
# Last edited on June 16th 2020
# added temperature humididty and pressure display
#
# run sudo crontab -e
# add
# @reboot python3 /home/pi/scroll_clock.py