Skip to content

Instantly share code, notes, and snippets.

@jrbail01
Last active December 30, 2020 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrbail01/5560fd31a4b96b4ca93b6bd3ddcf2e31 to your computer and use it in GitHub Desktop.
Save jrbail01/5560fd31a4b96b4ca93b6bd3ddcf2e31 to your computer and use it in GitHub Desktop.
Example plug-in Pi Zero display script for Adafruit 128x32 OLED Bonnet plus reading Initial State API data
import time
import subprocess
from board import SCL, SDA, D4
import busio
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1305
import subprocess
from ISReader.Reader import Reader
# --------- User Settings ---------
SECONDS_BETWEEN_SCREENS = 5
BUCKET_KEY = "PLACE YOUR INITIAL STATE BUCKET KEY HERE"
ACCESS_KEY = "PLACE YOUR INITIAL STATE ACCESS KEY HERE"
STREAM_KEY1_TEMP = "crawl2 Temperature(F)"
STREAM_KEY1_HUM = "crawl2Humidity(%)"
STREAM_KEY2_TEMP = "attic Temperature(F)"
STREAM_KEY2_HUM = "atticHumidity(%)"
STREAM_KEY3_TEMP = "curr_temp"
STREAM_KEY3_HUM = "curr_humidity"
# ---------------------------------
# ------------- Set up OLED -------------
oled_reset = digitalio.DigitalInOut(D4)
i2c = busio.I2C(SCL, SDA)
disp = adafruit_ssd1305.SSD1305_I2C(128, 32, i2c, reset=oled_reset)
disp.fill(0)
disp.show()
width = disp.width
height = disp.height
image = Image.new("1", (width, height))
draw = ImageDraw.Draw(image)
draw.rectangle((0, 0, width, height), outline=0, fill=0)
padding = -2
top = padding
bottom = height - padding
x = 0
font = ImageFont.load_default()
# ---------------------------------------
# create Initial State Reader instance
reader = Reader(access_key=ACCESS_KEY, bucket_key=BUCKET_KEY)
seconds_between_reads = 0
# Insert boot delay to ensure network connection
time.sleep(30)
while True:
# Get IP address
ipaddr = subprocess.check_output(['hostname', '-I']).decode("utf-8").rstrip()
# Read data value from Initial State API
if seconds_between_reads <= 0:
try:
events = reader.get_latest()
except:
print("Attempt to access Initial State Read API failed. Retrying ...")
seconds_between_reads = 60 # Set number of seconds between API reads
# --- Draw a black filled box to clear the image then write first screen ---
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((x, top + 0), "Crawlspace: ", font=font, fill=255)
if (not events or STREAM_KEY1_TEMP not in events):
draw.text((x, top + 8), " " + "Unknown Temperature", font=font, fill=255)
else:
draw.text((x, top + 8), " " + events[STREAM_KEY1_TEMP]['value'] + " °F", font=font, fill=255)
if (not events or STREAM_KEY1_HUM not in events):
draw.text((x, top + 16), " " + "Unknown Humidity", font=font, fill=255)
else:
draw.text((x, top + 16), " " + events[STREAM_KEY1_HUM]['value'] + "% humidity", font=font, fill=255)
disp.image(image)
disp.show()
time.sleep(SECONDS_BETWEEN_SCREENS)
seconds_between_reads -= SECONDS_BETWEEN_SCREENS
# --------------------------------------------------------------------------
# --- Draw a black filled box to clear the image then write second screen ---
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((x, top + 0), "Attic: ", font=font, fill=255)
if (not events or STREAM_KEY2_TEMP not in events):
draw.text((x, top + 8), " " + "Unknown Temperature", font=font, fill=255)
else:
draw.text((x, top + 8), " " + events[STREAM_KEY2_TEMP]['value'] + " °F", font=font, fill=255)
if (not events or STREAM_KEY2_HUM not in events):
draw.text((x, top + 16), " " + "Unknown Humidity", font=font, fill=255)
else:
draw.text((x, top + 16), " " + events[STREAM_KEY2_HUM]['value'] + "% humidity", font=font, fill=255)
disp.image(image)
disp.show()
time.sleep(SECONDS_BETWEEN_SCREENS)
seconds_between_reads -= SECONDS_BETWEEN_SCREENS
# ---------------------------------------------------------------------------
# --- Draw a black filled box to clear the image then write third screen ---
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((x, top + 0), "Outside: ", font=font, fill=255)
if (not events or STREAM_KEY3_TEMP not in events):
draw.text((x, top + 8), " " + "Unknown Temperature", font=font, fill=255)
else:
draw.text((x, top + 8), " " + events[STREAM_KEY3_TEMP]['value'] + " °F", font=font, fill=255)
if (not events or STREAM_KEY3_HUM not in events):
draw.text((x, top + 16), " " + "Unknown Humidity", font=font, fill=255)
else:
draw.text((x, top + 16), " " + events[STREAM_KEY3_HUM]['value'] + "% humidity", font=font, fill=255)
disp.image(image)
disp.show()
time.sleep(SECONDS_BETWEEN_SCREENS)
seconds_between_reads -= SECONDS_BETWEEN_SCREENS
# --------------------------------------------------------------------------
# --- Draw a black filled box to clear the image then write fourth screen ---
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((x, top + 25), "IP: " + ipaddr, font=font, fill=255)
disp.image(image)
disp.show()
time.sleep(SECONDS_BETWEEN_SCREENS/4)
seconds_between_reads -= SECONDS_BETWEEN_SCREENS
# ---------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment