From 7a910ab962a1b8611c866264bf88a3833c7b49cf Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 2 Jun 2025 12:43:59 -0700 Subject: [PATCH 1/7] pi-hole / bookworm / st7789 changes A lot has changed since this guide was release. The Pi Hole 6.x release in early 2025 dramatically changed the API. Bookworm changes how some of the pins are used. Even the ST7789 MiniPiTFT chip has some different options. This code was tested and work with a MiniPiTFt on a Pi 4. I'd like to update the guide to support accommidate the software updates. --- Pi_Hole_Ad_Blocker/mini_pitft_stats.py | 109 ++++++++++++------------- 1 file changed, 52 insertions(+), 57 deletions(-) diff --git a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py index 3d1349458..b631d3e3f 100755 --- a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py +++ b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py @@ -19,11 +19,10 @@ from PIL import Image, ImageDraw, ImageFont import adafruit_rgb_display.st7789 as st7789 -API_TOKEN = "YOUR_API_TOKEN_HERE" -api_url = "http://localhost/admin/api.php?summaryRaw&auth="+API_TOKEN +API_URL = "http://pi.hole/api/stats/summary" # Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4): -cs_pin = digitalio.DigitalInOut(board.CE0) +cs_pin = digitalio.DigitalInOut(board.D17) dc_pin = digitalio.DigitalInOut(board.D25) reset_pin = None @@ -34,54 +33,42 @@ spi = board.SPI() # Create the ST7789 display: -disp = st7789.ST7789(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE, - width=135, height=240, x_offset=53, y_offset=40) +disp = st7789.ST7789( + spi, + dc_pin, + cs_pin, + reset_pin, + 135, + 240, + baudrate=BAUDRATE, + x_offset=53, + y_offset=40, + rotation=90 +) # Create blank image for drawing. # Make sure to create image with mode 'RGB' for full color. -height = disp.width # we swap height/width to rotate it to landscape! -width = disp.height -image = Image.new('RGB', (width, height)) -rotation = 90 - -# Get drawing object to draw on image. +CANVAS_WIDTH = disp.height +CANVAS_HEIGHT = disp.width +image = Image.new('RGB', (CANVAS_WIDTH, CANVAS_HEIGHT)) draw = ImageDraw.Draw(image) -# Draw a black filled box to clear the image. -draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0)) -disp.image(image, rotation) -# Draw some shapes. -# First define some constants to allow easy resizing of shapes. -padding = -2 -top = padding -bottom = height-padding -# Move left to right keeping track of the current x position for drawing shapes. -x = 0 - - -# Alternatively load a TTF font. Make sure the .ttf font file is in the -# same directory as the python script! -# Some other nice fonts to try: http://www.dafont.com/bitmap.php -font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24) - -# Turn on the backlight -backlight = digitalio.DigitalInOut(board.D22) -backlight.switch_to_output() -backlight.value = True - -# Add buttons as inputs +# Load default font (or replace with a TTF if desired) +FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" +font = ImageFont.truetype(FONT_PATH, 20) + buttonA = digitalio.DigitalInOut(board.D23) buttonA.switch_to_input() while True: # Draw a black filled box to clear the image. - draw.rectangle((0, 0, width, height), outline=0, fill=0) + draw.rectangle((0, 0, CANVAS_WIDTH, CANVAS_HEIGHT), outline=0, fill=(0, 0, 0)) # Shell scripts for system monitoring from here: # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load - cmd = "hostname -I | cut -d\' \' -f1" - IP = "IP: "+subprocess.check_output(cmd, shell=True).decode("utf-8") - cmd = "hostname | tr -d \'\\n\'" + cmd = "hostname -I | cut -d' ' -f1" + IP = "IP: " + subprocess.check_output(cmd, shell=True).decode("utf-8").strip() + cmd = "hostname | tr -d '\\n'" HOST = subprocess.check_output(cmd, shell=True).decode("utf-8") cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'" CPU = subprocess.check_output(cmd, shell=True).decode("utf-8") @@ -89,22 +76,24 @@ MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8") cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%d GB %s\", $3,$2,$5}'" Disk = subprocess.check_output(cmd, shell=True).decode("utf-8") - cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk \'{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}\'" # pylint: disable=line-too-long + cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}'" Temp = subprocess.check_output(cmd, shell=True).decode("utf-8") - # Pi Hole data! try: - r = requests.get(api_url) - data = json.loads(r.text) - DNSQUERIES = data['dns_queries_today'] - ADSBLOCKED = data['ads_blocked_today'] - CLIENTS = data['unique_clients'] - except KeyError: - time.sleep(1) - continue - - y = top + r = requests.get(API_URL, timeout=5) + r.raise_for_status() + data = r.json() + DNSQUERIES = data["queries"]["total"] + ADSBLOCKED = data["queries"]["blocked"] + CLIENTS = data["clients"]["total"] + except (KeyError, requests.RequestException, json.JSONDecodeError): + DNSQUERIES = None + ADSBLOCKED = None + CLIENTS = None + + y = top = 5 + x = 5 if not buttonA.value: # just button A pressed draw.text((x, y), IP, font=font, fill="#FFFF00") y += font.getbbox(IP)[3] @@ -121,13 +110,19 @@ y += font.getbbox(IP)[3] draw.text((x, y), HOST, font=font, fill="#FFFF00") y += font.getbbox(HOST)[3] - draw.text((x, y), "Ads Blocked: {}".format(str(ADSBLOCKED)), font=font, fill="#00FF00") - y += font.getbbox(str(ADSBLOCKED))[3] - draw.text((x, y), "Clients: {}".format(str(CLIENTS)), font=font, fill="#0000FF") - y += font.getbbox(str(CLIENTS))[3] - draw.text((x, y), "DNS Queries: {}".format(str(DNSQUERIES)), font=font, fill="#FF00FF") - y += font.getbbox(str(DNSQUERIES))[3] + if ADSBLOCKED is not None: + txt = f"Ads Blocked: {ADSBLOCKED}" + draw.text((x, y), txt, font=font, fill="#00FF00") + y += font.getbbox(txt)[3] + if CLIENTS is not None: + txt = f"Clients: {CLIENTS}" + draw.text((x, y), txt, font=font, fill="#0000FF") + y += font.getbbox(txt)[3] + if DNSQUERIES is not None: + txt = f"DNS Queries: {DNSQUERIES}" + draw.text((x, y), txt, font=font, fill="#FF00FF") + y += font.getbbox(txt)[3] # Display image. - disp.image(image, rotation) + disp.image(image) time.sleep(.1) From 8937ea355e13f494b04e4aa556dd916a0cc88c48 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 2 Jun 2025 12:51:41 -0700 Subject: [PATCH 2/7] linting long line. --- Pi_Hole_Ad_Blocker/mini_pitft_stats.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py index b631d3e3f..f1833e427 100755 --- a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py +++ b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py @@ -76,7 +76,10 @@ MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8") cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%d GB %s\", $3,$2,$5}'" Disk = subprocess.check_output(cmd, shell=True).decode("utf-8") - cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}'" + cmd = ( + "cat /sys/class/thermal/thermal_zone0/temp | " + "awk '{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}'" + ) Temp = subprocess.check_output(cmd, shell=True).decode("utf-8") # Pi Hole data! From 339e0d77805aa13853d9c778929d70ff66ae5ad3 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Wed, 4 Jun 2025 08:52:11 -0700 Subject: [PATCH 3/7] added extended SPDX --- Pi_Hole_Ad_Blocker/mini_pitft_stats.py | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py index f1833e427..445288e0f 100755 --- a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py +++ b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py @@ -1,7 +1,34 @@ # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries +# SPDX-FileCopyrightText: 2025 Mikey Sklar for Adafruit Industries # # SPDX-License-Identifier: MIT +# Copyright (c) 2017 Adafruit Industries +# Author: Brent Rubell, Mikey Sklar +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +# This example is for use on (Linux) computers that are using CPython with +# Adafruit Blinka to support CircuitPython libraries. CircuitPython does +# not support PIL/pillow (python imaging library)! + + # -*- coding: utf-8 -*- # Import Python System Libraries import time @@ -19,7 +46,7 @@ from PIL import Image, ImageDraw, ImageFont import adafruit_rgb_display.st7789 as st7789 -API_URL = "http://pi.hole/api/stats/summary" +API_URL = "http://localhost/api/stats/summary" # Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4): cs_pin = digitalio.DigitalInOut(board.D17) From 9bea10602928bbe1aa1052ae20923be1ef78d8a1 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Wed, 4 Jun 2025 09:13:46 -0700 Subject: [PATCH 4/7] increase font size --- Pi_Hole_Ad_Blocker/mini_pitft_stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py index 445288e0f..68e4576a9 100755 --- a/Pi_Hole_Ad_Blocker/mini_pitft_stats.py +++ b/Pi_Hole_Ad_Blocker/mini_pitft_stats.py @@ -82,7 +82,7 @@ # Load default font (or replace with a TTF if desired) FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" -font = ImageFont.truetype(FONT_PATH, 20) +font = ImageFont.truetype(FONT_PATH, 24) buttonA = digitalio.DigitalInOut(board.D23) buttonA.switch_to_input() From e5c40eb8ce7001fa71cfb60bba3f6e487281ebff Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Wed, 4 Jun 2025 11:13:02 -0700 Subject: [PATCH 5/7] Pi OLED updates confirmed Pi OLED (SSD1306 I2C) works with new Pi Hole 6.x release code. --- Pi_Hole_Ad_Blocker/stats.py | 74 ++++++++++++++----------------------- 1 file changed, 27 insertions(+), 47 deletions(-) diff --git a/Pi_Hole_Ad_Blocker/stats.py b/Pi_Hole_Ad_Blocker/stats.py index d1021ba68..d5af4450a 100644 --- a/Pi_Hole_Ad_Blocker/stats.py +++ b/Pi_Hole_Ad_Blocker/stats.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries # SPDX-FileCopyrightText: 2017 James DeVito for Adafruit Industries +# SPDX-FileCopyrightText: 2025 Mikey Sklar for Adafruit Industries # # SPDX-License-Identifier: MIT @@ -29,44 +30,34 @@ # Adafruit Blinka to support CircuitPython libraries. CircuitPython does # not support PIL/pillow (python imaging library)! -# Import Python System Libraries + import json import subprocess import time -# Import Requests Library import requests - -# Import Blinka from board import SCL, SDA import busio import adafruit_ssd1306 - -# Import Python Imaging Library from PIL import Image, ImageDraw, ImageFont -API_TOKEN = "YOUR_API_TOKEN_HERE" -api_url = "http://localhost/admin/api.php?summaryRaw&auth="+API_TOKEN +api_url = "http://localhost/api/stats/summary" # Create the I2C interface. i2c = busio.I2C(SCL, SDA) # 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) # 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_OFF = 50 # off time in seconds +DISPLAY_ON = 10 # on time in seconds +DISPLAY_OFF = 50 # off time in seconds # Clear display. disp.fill(0) disp.show() # Create blank image for drawing. -# Make sure to create image with mode '1' for 1-bit color. width = disp.width height = disp.height image = Image.new('1', (width, height)) @@ -77,27 +68,21 @@ # Draw a black filled box to clear the image. 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 top = padding -bottom = height - padding -# Move left to right keeping track of the current x position -# for drawing shapes. x = 0 # Load nice silkscreen font font = ImageFont.truetype('/home/pi/slkscr.ttf', 8) 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) - # Shell scripts for system monitoring from here : - # 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\'" + # Shell scripts for system monitoring + cmd = "hostname -I | cut -d' ' -f1 | tr -d '\\n'" 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") cmd = "top -bn1 | grep load | awk " \ "'{printf \"CPU Load: %.2f\", $(NF-2)}'" @@ -109,35 +94,30 @@ "\"Disk: %d/%dGB %s\", $3,$2,$5}'" Disk = subprocess.check_output(cmd, shell=True).decode("utf-8") - # Pi Hole data! + # Pi-Hole data! try: - r = requests.get(api_url) - data = json.loads(r.text) - DNSQUERIES = data['dns_queries_today'] - ADSBLOCKED = data['ads_blocked_today'] - CLIENTS = data['unique_clients'] - except KeyError: - time.sleep(1) - continue - - draw.text((x, top), "IP: " + str(IP) + - " (" + HOST + ")", font=font, fill=255) - draw.text((x, top + 8), "Ads Blocked: " + - str(ADSBLOCKED), 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) + r = requests.get(api_url, timeout=2) + r.raise_for_status() + data = r.json() + DNSQUERIES = data["queries"]["total"] + ADSBLOCKED = data["queries"]["blocked"] + CLIENTS = data["clients"]["total"] + except Exception: + DNSQUERIES = 0 + ADSBLOCKED = 0 + CLIENTS = 0 + + draw.text((x, top), "IP: " + IP + " (" + HOST + ")", font=font, fill=255) + draw.text((x, top + 8), "Ads Blocked: " + str(ADSBLOCKED), 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) # Display image. disp.image(image) disp.show() time.sleep(DISPLAY_ON) + + # Blank screen to prevent burn-in disp.fill(0) disp.show() time.sleep(DISPLAY_OFF) From 5cd01c6116f2adb0a2d665c03b0afa08d6698bbf Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Wed, 4 Jun 2025 11:37:46 -0700 Subject: [PATCH 6/7] linting --- Pi_Hole_Ad_Blocker/stats.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Pi_Hole_Ad_Blocker/stats.py b/Pi_Hole_Ad_Blocker/stats.py index d5af4450a..0e39361ce 100644 --- a/Pi_Hole_Ad_Blocker/stats.py +++ b/Pi_Hole_Ad_Blocker/stats.py @@ -31,7 +31,6 @@ # not support PIL/pillow (python imaging library)! -import json import subprocess import time @@ -102,7 +101,7 @@ DNSQUERIES = data["queries"]["total"] ADSBLOCKED = data["queries"]["blocked"] CLIENTS = data["clients"]["total"] - except Exception: + except (KeyError, requests.RequestException, json.JSONDecodeError): DNSQUERIES = 0 ADSBLOCKED = 0 CLIENTS = 0 From 5eff1adaa85e52da04bea75e0da1c5f26d899e0c Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Wed, 4 Jun 2025 11:56:01 -0700 Subject: [PATCH 7/7] Update stats.py --- Pi_Hole_Ad_Blocker/stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pi_Hole_Ad_Blocker/stats.py b/Pi_Hole_Ad_Blocker/stats.py index 0e39361ce..25e093404 100644 --- a/Pi_Hole_Ad_Blocker/stats.py +++ b/Pi_Hole_Ad_Blocker/stats.py @@ -101,7 +101,7 @@ DNSQUERIES = data["queries"]["total"] ADSBLOCKED = data["queries"]["blocked"] CLIENTS = data["clients"]["total"] - except (KeyError, requests.RequestException, json.JSONDecodeError): + except (KeyError, requests.RequestException): DNSQUERIES = 0 ADSBLOCKED = 0 CLIENTS = 0