|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Import Python System Libraries |
| 3 | +import time |
| 4 | +import json |
| 5 | +import subprocess |
| 6 | + |
| 7 | +# Import Requests Library |
| 8 | +import requests |
| 9 | + |
| 10 | +#Import Blinka |
| 11 | +import digitalio |
| 12 | +import board |
| 13 | + |
| 14 | +# Import Python Imaging Library |
| 15 | +from PIL import Image, ImageDraw, ImageFont |
| 16 | +import adafruit_rgb_display.st7789 as st7789 |
| 17 | + |
| 18 | +api_url = 'http://localhost/admin/api.php' |
| 19 | + |
| 20 | +# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4): |
| 21 | +cs_pin = digitalio.DigitalInOut(board.CE0) |
| 22 | +dc_pin = digitalio.DigitalInOut(board.D25) |
| 23 | +reset_pin = None |
| 24 | + |
| 25 | +# Config for display baudrate (default max is 24mhz): |
| 26 | +BAUDRATE = 64000000 |
| 27 | + |
| 28 | +# Setup SPI bus using hardware SPI: |
| 29 | +spi = board.SPI() |
| 30 | + |
| 31 | +# Create the ST7789 display: |
| 32 | +disp = st7789.ST7789(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE, |
| 33 | + width=135, height=240, x_offset=53, y_offset=40) |
| 34 | + |
| 35 | +# Create blank image for drawing. |
| 36 | +# Make sure to create image with mode 'RGB' for full color. |
| 37 | +height = disp.width # we swap height/width to rotate it to landscape! |
| 38 | +width = disp.height |
| 39 | +image = Image.new('RGB', (width, height)) |
| 40 | +rotation = 90 |
| 41 | + |
| 42 | +# Get drawing object to draw on image. |
| 43 | +draw = ImageDraw.Draw(image) |
| 44 | + |
| 45 | +# Draw a black filled box to clear the image. |
| 46 | +draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0)) |
| 47 | +disp.image(image, rotation) |
| 48 | +# Draw some shapes. |
| 49 | +# First define some constants to allow easy resizing of shapes. |
| 50 | +padding = -2 |
| 51 | +top = padding |
| 52 | +bottom = height-padding |
| 53 | +# Move left to right keeping track of the current x position for drawing shapes. |
| 54 | +x = 0 |
| 55 | + |
| 56 | + |
| 57 | +# Alternatively load a TTF font. Make sure the .ttf font file is in the |
| 58 | +# same directory as the python script! |
| 59 | +# Some other nice fonts to try: http://www.dafont.com/bitmap.php |
| 60 | +font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24) |
| 61 | + |
| 62 | +# Turn on the backlight |
| 63 | +backlight = digitalio.DigitalInOut(board.D22) |
| 64 | +backlight.switch_to_output() |
| 65 | +backlight.value = True |
| 66 | + |
| 67 | +# Add buttons as inputs |
| 68 | +buttonA = digitalio.DigitalInOut(board.D23) |
| 69 | +buttonA.switch_to_input() |
| 70 | + |
| 71 | +while True: |
| 72 | + # Draw a black filled box to clear the image. |
| 73 | + draw.rectangle((0, 0, width, height), outline=0, fill=0) |
| 74 | + |
| 75 | + # Shell scripts for system monitoring from here: |
| 76 | + # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load |
| 77 | + cmd = "hostname -I | cut -d\' \' -f1" |
| 78 | + IP = "IP: "+subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 79 | + cmd = "hostname | tr -d \'\\n\'" |
| 80 | + HOST = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 81 | + cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'" |
| 82 | + CPU = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 83 | + cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'" |
| 84 | + MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 85 | + cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%d GB %s\", $3,$2,$5}'" |
| 86 | + Disk = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 87 | + cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk \'{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}\'" # pylint: disable=line-too-long |
| 88 | + |
| 89 | + # Pi Hole data! |
| 90 | + try: |
| 91 | + r = requests.get(api_url) |
| 92 | + data = json.loads(r.text) |
| 93 | + DNSQUERIES = data['dns_queries_today'] |
| 94 | + ADSBLOCKED = data['ads_blocked_today'] |
| 95 | + CLIENTS = data['unique_clients'] |
| 96 | + except KeyError: |
| 97 | + time.sleep(1) |
| 98 | + continue |
| 99 | + |
| 100 | + y = top |
| 101 | + if not buttonA.value: # just button A pressed |
| 102 | + draw.text((x, y), IP, font=font, fill="#FFFF00") |
| 103 | + y += font.getsize(IP)[1] |
| 104 | + draw.text((x, y), CPU, font=font, fill="#FFFF00") |
| 105 | + y += font.getsize(CPU)[1] |
| 106 | + draw.text((x, y), MemUsage, font=font, fill="#00FF00") |
| 107 | + y += font.getsize(MemUsage)[1] |
| 108 | + draw.text((x, y), Disk, font=font, fill="#0000FF") |
| 109 | + y += font.getsize(Disk)[1] |
| 110 | + draw.text((x, y), "DNS Queries: {}".format(DNSQUERIES), font=font, fill="#FF00FF") |
| 111 | + else: |
| 112 | + draw.text((x, y), IP, font=font, fill="#FFFF00") |
| 113 | + y += font.getsize(IP)[1] |
| 114 | + draw.text((x, y), HOST, font=font, fill="#FFFF00") |
| 115 | + y += font.getsize(HOST)[1] |
| 116 | + draw.text((x, y), "Ads Blocked: {}".format(str(ADSBLOCKED)), font=font, fill="#00FF00") |
| 117 | + y += font.getsize(str(ADSBLOCKED))[1] |
| 118 | + draw.text((x, y), "Clients: {}".format(str(CLIENTS)), font=font, fill="#0000FF") |
| 119 | + y += font.getsize(str(CLIENTS))[1] |
| 120 | + draw.text((x, y), "DNS Queries: {}".format(str(DNSQUERIES)), font=font, fill="#FF00FF") |
| 121 | + y += font.getsize(str(DNSQUERIES))[1] |
| 122 | + |
| 123 | + # Display image. |
| 124 | + disp.image(image, rotation) |
| 125 | + time.sleep(.1) |
0 commit comments