Last active
December 6, 2019 22:41
-
-
Save clebio/e11840c66b09e5ebbbdc7b6271f8d9be to your computer and use it in GitHub Desktop.
System service on Raspberry Pi Buster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=OLED system monitor | |
[Service] | |
ExecStart=/usr/local/bin/oled-system.py | |
[Install] | |
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import time | |
import Adafruit_GPIO.SPI as SPI | |
import Adafruit_SSD1306 | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
import subprocess | |
import socket | |
from gpiozero import CPUTemperature, LoadAverage, DiskUsage | |
from signal import pause | |
def get_ip(): | |
# https://stackoverflow.com/a/28950776/381200 | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
try: | |
# doesn't even have to be reachable | |
s.connect(('10.255.255.255', 1)) | |
IP = s.getsockname()[0] | |
except: | |
IP = '127.0.0.1' | |
finally: | |
s.close() | |
return IP | |
bars = ['\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588', ] | |
#bars = ['_', 'o', 'O', '#'] | |
def cpu_bars(): | |
la = LoadAverage(minutes=1) | |
i = int(100 * la.load_average) | |
return bars[int((len(bars)-1) * i/100)], i | |
def read_system(pbar, width): | |
cpu = CPUTemperature(min_temp=50, max_temp=90) | |
disp_temp = 'Temp: {:3.2f}C'.format(cpu.temperature) | |
la = LoadAverage(min_load_average=0, max_load_average=2) | |
bar, val = cpu_bars() | |
pbar += bar | |
pbar = pbar[1:width] | |
disp_load = "CPU{:s}{:2.0f}%".format(pbar, val) | |
disk = DiskUsage() | |
disp_du = "Disk: {:3.2f}".format(disk.usage) | |
disp_net = "IP: {}".format(get_ip()) | |
system_report = "{}".format(disp_temp) | |
system_report = "{}\n{}\n{}\n{}\n".format(disp_temp, disp_load, disp_du, disp_net) | |
return system_report, pbar | |
RST = 0 | |
padding = 0 | |
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) | |
disp.begin() | |
width = disp.width | |
height = disp.height | |
top = padding | |
bottom = height - padding | |
#font = ImageFont.load_default() | |
font = ImageFont.truetype("FreeSans.ttf", 12) | |
x=0 | |
while True: | |
data = read_system() | |
bw = Image.new('1', (width, height)) | |
draw = ImageDraw.Draw(bw) | |
draw.text((0, top), data, font=font, fill=255) | |
disp.clear() | |
disp.image(bw) | |
disp.display() | |
time.sleep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
scp oled-system.py pi@172.16.11.110:~/ | |
ssh pi@172.16.11.110 bash <<EOF | |
chmod +x oled-system.py | |
sudo mv /home/pi/oled-system.py /usr/local/bin/ | |
sudo systemctl restart oled-system | |
EOF | |
echo "dunzo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why are gists named after the first file and not the description field above?? | |
# README.md | |
``` | |
sudo sh -c { | |
cp oled-system.service /etc/systemd/system/oled-system.service | |
cp oled_system.py /usr/local/bin/oled-system.py | |
systemctl daemon-reload | |
systemctl enable oled-system | |
} | |
``` | |
## Editing the file and propagating updates | |
``` | |
chmod +x restart.sh | |
brew install fswatch | |
fswatch -o oled-system.py | xargs -I{} ./restart.sh | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment