Skip to content

Commit e5c40eb

Browse files
committed
Pi OLED updates
confirmed Pi OLED (SSD1306 I2C) works with new Pi Hole 6.x release code.
1 parent 9bea106 commit e5c40eb

File tree

1 file changed

+27
-47
lines changed

1 file changed

+27
-47
lines changed

Pi_Hole_Ad_Blocker/stats.py

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
22
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
33
# SPDX-FileCopyrightText: 2017 James DeVito for Adafruit Industries
4+
# SPDX-FileCopyrightText: 2025 Mikey Sklar for Adafruit Industries
45
#
56
# SPDX-License-Identifier: MIT
67

@@ -29,44 +30,34 @@
2930
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
3031
# not support PIL/pillow (python imaging library)!
3132

32-
# Import Python System Libraries
33+
3334
import json
3435
import subprocess
3536
import time
3637

37-
# Import Requests Library
3838
import requests
39-
40-
# Import Blinka
4139
from board import SCL, SDA
4240
import busio
4341
import adafruit_ssd1306
44-
45-
# Import Python Imaging Library
4642
from PIL import Image, ImageDraw, ImageFont
4743

48-
API_TOKEN = "YOUR_API_TOKEN_HERE"
49-
api_url = "http://localhost/admin/api.php?summaryRaw&auth="+API_TOKEN
44+
api_url = "http://localhost/api/stats/summary"
5045

5146
# Create the I2C interface.
5247
i2c = busio.I2C(SCL, SDA)
5348

5449
# Create the SSD1306 OLED class.
55-
# The first two parameters are the pixel width and pixel height. Change these
56-
# to the right size for your display!
5750
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
5851

5952
# Leaving the OLED on for a long period of time can damage it
60-
# Set these to prevent OLED burn in
61-
DISPLAY_ON = 10 # on time in seconds
62-
DISPLAY_OFF = 50 # off time in seconds
53+
DISPLAY_ON = 10 # on time in seconds
54+
DISPLAY_OFF = 50 # off time in seconds
6355

6456
# Clear display.
6557
disp.fill(0)
6658
disp.show()
6759

6860
# Create blank image for drawing.
69-
# Make sure to create image with mode '1' for 1-bit color.
7061
width = disp.width
7162
height = disp.height
7263
image = Image.new('1', (width, height))
@@ -77,27 +68,21 @@
7768
# Draw a black filled box to clear the image.
7869
draw.rectangle((0, 0, width, height), outline=0, fill=0)
7970

80-
# Draw some shapes.
81-
# First define some constants to allow easy resizing of shapes.
8271
padding = -2
8372
top = padding
84-
bottom = height - padding
85-
# Move left to right keeping track of the current x position
86-
# for drawing shapes.
8773
x = 0
8874

8975
# Load nice silkscreen font
9076
font = ImageFont.truetype('/home/pi/slkscr.ttf', 8)
9177

9278
while True:
93-
# Draw a black filled box to clear the image.
79+
# Clear the image buffer
9480
draw.rectangle((0, 0, width, height), outline=0, fill=0)
9581

96-
# Shell scripts for system monitoring from here :
97-
# https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
98-
cmd = "hostname -I | cut -d\' \' -f1 | tr -d \'\\n\'"
82+
# Shell scripts for system monitoring
83+
cmd = "hostname -I | cut -d' ' -f1 | tr -d '\\n'"
9984
IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
100-
cmd = "hostname | tr -d \'\\n\'"
85+
cmd = "hostname | tr -d '\\n'"
10186
HOST = subprocess.check_output(cmd, shell=True).decode("utf-8")
10287
cmd = "top -bn1 | grep load | awk " \
10388
"'{printf \"CPU Load: %.2f\", $(NF-2)}'"
@@ -109,35 +94,30 @@
10994
"\"Disk: %d/%dGB %s\", $3,$2,$5}'"
11095
Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
11196

112-
# Pi Hole data!
97+
# Pi-Hole data!
11398
try:
114-
r = requests.get(api_url)
115-
data = json.loads(r.text)
116-
DNSQUERIES = data['dns_queries_today']
117-
ADSBLOCKED = data['ads_blocked_today']
118-
CLIENTS = data['unique_clients']
119-
except KeyError:
120-
time.sleep(1)
121-
continue
122-
123-
draw.text((x, top), "IP: " + str(IP) +
124-
" (" + HOST + ")", font=font, fill=255)
125-
draw.text((x, top + 8), "Ads Blocked: " +
126-
str(ADSBLOCKED), font=font, fill=255)
127-
draw.text((x, top + 16), "Clients: " +
128-
str(CLIENTS), font=font, fill=255)
129-
draw.text((x, top + 24), "DNS Queries: " +
130-
str(DNSQUERIES), font=font, fill=255)
131-
132-
# skip over original stats
133-
# draw.text((x, top+8), str(CPU), font=font, fill=255)
134-
# draw.text((x, top+16), str(MemUsage), font=font, fill=255)
135-
# draw.text((x, top+25), str(Disk), font=font, fill=255)
99+
r = requests.get(api_url, timeout=2)
100+
r.raise_for_status()
101+
data = r.json()
102+
DNSQUERIES = data["queries"]["total"]
103+
ADSBLOCKED = data["queries"]["blocked"]
104+
CLIENTS = data["clients"]["total"]
105+
except Exception:
106+
DNSQUERIES = 0
107+
ADSBLOCKED = 0
108+
CLIENTS = 0
109+
110+
draw.text((x, top), "IP: " + IP + " (" + HOST + ")", font=font, fill=255)
111+
draw.text((x, top + 8), "Ads Blocked: " + str(ADSBLOCKED), font=font, fill=255)
112+
draw.text((x, top + 16), "Clients: " + str(CLIENTS), font=font, fill=255)
113+
draw.text((x, top + 24), "DNS Queries: " + str(DNSQUERIES), font=font, fill=255)
136114

137115
# Display image.
138116
disp.image(image)
139117
disp.show()
140118
time.sleep(DISPLAY_ON)
119+
120+
# Blank screen to prevent burn-in
141121
disp.fill(0)
142122
disp.show()
143123
time.sleep(DISPLAY_OFF)

0 commit comments

Comments
 (0)