Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active January 31, 2021 22:16
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 davidsword/0e9556ac19b5ef02d93d73931fec659e to your computer and use it in GitHub Desktop.
Save davidsword/0e9556ac19b5ef02d93d73931fec659e to your computer and use it in GitHub Desktop.
rpi-oled-sys+pihole-stats.py
# System and PiHole stats for 128x32 OLED screen
#
# Add to: `/etc/rc.local` for boot start.
#
# @author learn.adafruit.com, davidsword
# @link https://gist.github.com/davidsword/0e9556ac19b5ef02d93d73931fec659e
# @version 20210131
import time
import subprocess
from datetime import datetime
import json
import requests
# @see https://pypi.org/project/PiHole-api/
import pihole as ph
# @see https://learn.adafruit.com/adafruit-pioled-128x32-mini-oled-for-raspberry-pi/usage
# @see https://learn.adafruit.com/pi-hole-ad-blocker-with-pi-zero-w/install-pioled
from board import SCL, SDA
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
api_url = '<ipofpiholehere>'
fontfile = 'font.ttf'
refreshinseconds = 15
# @see https://pypi.org/project/PiHole-api/
pihole = ph.PiHole(api_url);
i2c = busio.I2C(SCL, SDA)
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
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 = 0
bottom = height - padding
x = 0
font = ImageFont.truetype(fontfile, 20)
font2 = ImageFont.truetype(fontfile, 15)
while True:
now = datetime.now()
# only refresh once a minute
seconds = int(now.strftime('%S'))
if seconds < 16:
pihole.refresh()
draw.rectangle((0, 0, width, height), outline=0, fill=0)
cmd = "top -bn1 | grep load | awk '{printf \"%.1f\", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "free -m | awk 'NR==2{printf \"%.0f%%\", $3*100/$2 }'"
MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = 'df -h | awk \'$NF=="/"{printf "%s", $5}\''
Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
now = datetime.now()
# @see https://strftime.org
current = now.strftime("%I:%M")
draw.text((x, top + 0), CPU + " "+MemUsage+" "+Disk+" ", font=font, fill=255)
draw.text((x, top + 20), pihole.blocked + " " + pihole.ads_percentage + "% " + current + " ", font=font2, fill=255)
disp.image(image)
disp.show()
time.sleep(refreshinseconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment