Pi OLED updates

confirmed Pi OLED (SSD1306 I2C) works with new Pi Hole 6.x release code.
This commit is contained in:
Mikey Sklar 2025-06-04 11:13:02 -07:00
parent 9bea106029
commit e5c40eb8ce

View file

@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries # SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
# SPDX-FileCopyrightText: 2017 James DeVito for Adafruit Industries # SPDX-FileCopyrightText: 2017 James DeVito for Adafruit Industries
# SPDX-FileCopyrightText: 2025 Mikey Sklar for Adafruit Industries
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
@ -29,35 +30,26 @@
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does # Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)! # not support PIL/pillow (python imaging library)!
# Import Python System Libraries
import json import json
import subprocess import subprocess
import time import time
# Import Requests Library
import requests import requests
# Import Blinka
from board import SCL, SDA from board import SCL, SDA
import busio import busio
import adafruit_ssd1306 import adafruit_ssd1306
# Import Python Imaging Library
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
API_TOKEN = "YOUR_API_TOKEN_HERE" api_url = "http://localhost/api/stats/summary"
api_url = "http://localhost/admin/api.php?summaryRaw&auth="+API_TOKEN
# Create the I2C interface. # Create the I2C interface.
i2c = busio.I2C(SCL, SDA) i2c = busio.I2C(SCL, SDA)
# Create the SSD1306 OLED class. # Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height. Change these
# to the right size for your display!
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c) disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
# Leaving the OLED on for a long period of time can damage it # Leaving the OLED on for a long period of time can damage it
# Set these to prevent OLED burn in
DISPLAY_ON = 10 # on time in seconds DISPLAY_ON = 10 # on time in seconds
DISPLAY_OFF = 50 # off time in seconds DISPLAY_OFF = 50 # off time in seconds
@ -66,7 +58,6 @@ disp.fill(0)
disp.show() disp.show()
# Create blank image for drawing. # Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width width = disp.width
height = disp.height height = disp.height
image = Image.new('1', (width, height)) image = Image.new('1', (width, height))
@ -77,27 +68,21 @@ draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image. # Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0) draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2 padding = -2
top = padding top = padding
bottom = height - padding
# Move left to right keeping track of the current x position
# for drawing shapes.
x = 0 x = 0
# Load nice silkscreen font # Load nice silkscreen font
font = ImageFont.truetype('/home/pi/slkscr.ttf', 8) font = ImageFont.truetype('/home/pi/slkscr.ttf', 8)
while True: while True:
# Draw a black filled box to clear the image. # Clear the image buffer
draw.rectangle((0, 0, width, height), outline=0, fill=0) draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Shell scripts for system monitoring from here : # Shell scripts for system monitoring
# https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load cmd = "hostname -I | cut -d' ' -f1 | tr -d '\\n'"
cmd = "hostname -I | cut -d\' \' -f1 | tr -d \'\\n\'"
IP = subprocess.check_output(cmd, shell=True).decode("utf-8") IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "hostname | tr -d \'\\n\'" cmd = "hostname | tr -d '\\n'"
HOST = subprocess.check_output(cmd, shell=True).decode("utf-8") HOST = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "top -bn1 | grep load | awk " \ cmd = "top -bn1 | grep load | awk " \
"'{printf \"CPU Load: %.2f\", $(NF-2)}'" "'{printf \"CPU Load: %.2f\", $(NF-2)}'"
@ -109,35 +94,30 @@ while True:
"\"Disk: %d/%dGB %s\", $3,$2,$5}'" "\"Disk: %d/%dGB %s\", $3,$2,$5}'"
Disk = subprocess.check_output(cmd, shell=True).decode("utf-8") Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
# Pi Hole data! # Pi-Hole data!
try: try:
r = requests.get(api_url) r = requests.get(api_url, timeout=2)
data = json.loads(r.text) r.raise_for_status()
DNSQUERIES = data['dns_queries_today'] data = r.json()
ADSBLOCKED = data['ads_blocked_today'] DNSQUERIES = data["queries"]["total"]
CLIENTS = data['unique_clients'] ADSBLOCKED = data["queries"]["blocked"]
except KeyError: CLIENTS = data["clients"]["total"]
time.sleep(1) except Exception:
continue DNSQUERIES = 0
ADSBLOCKED = 0
CLIENTS = 0
draw.text((x, top), "IP: " + str(IP) + draw.text((x, top), "IP: " + IP + " (" + HOST + ")", font=font, fill=255)
" (" + HOST + ")", font=font, fill=255) draw.text((x, top + 8), "Ads Blocked: " + str(ADSBLOCKED), font=font, fill=255)
draw.text((x, top + 8), "Ads Blocked: " + draw.text((x, top + 16), "Clients: " + str(CLIENTS), font=font, fill=255)
str(ADSBLOCKED), font=font, fill=255) draw.text((x, top + 24), "DNS Queries: " + str(DNSQUERIES), font=font, fill=255)
draw.text((x, top + 16), "Clients: " +
str(CLIENTS), font=font, fill=255)
draw.text((x, top + 24), "DNS Queries: " +
str(DNSQUERIES), font=font, fill=255)
# skip over original stats
# draw.text((x, top+8), str(CPU), font=font, fill=255)
# draw.text((x, top+16), str(MemUsage), font=font, fill=255)
# draw.text((x, top+25), str(Disk), font=font, fill=255)
# Display image. # Display image.
disp.image(image) disp.image(image)
disp.show() disp.show()
time.sleep(DISPLAY_ON) time.sleep(DISPLAY_ON)
# Blank screen to prevent burn-in
disp.fill(0) disp.fill(0)
disp.show() disp.show()
time.sleep(DISPLAY_OFF) time.sleep(DISPLAY_OFF)