Skip to content

Instantly share code, notes, and snippets.

@LucaDev
Created October 9, 2020 15:16
Show Gist options
  • Save LucaDev/e5db1327ff40891994ed0a7a8ce8fecc to your computer and use it in GitHub Desktop.
Save LucaDev/e5db1327ff40891994ed0a7a8ce8fecc to your computer and use it in GitHub Desktop.
Raspberry Pi OLED System Stats
#!/bin/python3
import time
import psutil
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from PIL import ImageFont
serial = i2c(port=1, address=0x3C)
device = ssd1306(serial)
font = ImageFont.truetype('Arimo-Regular.ttf', 12)
font_awesome = ImageFont.truetype('fa-light-300.ttf', 14)
while True:
with canvas(device) as draw:
IP = psutil.net_if_addrs()["eth0"][0].address
CPU_Usage = psutil.cpu_percent()
MemUsage = psutil.virtual_memory().percent
DiskUsage = psutil.disk_usage('/').percent
Temp = psutil.sensors_temperatures()['cpu_thermal'][0].current
# Draw
# Icon thermometer
temp_icon = chr(0xf2cb)
icon_pos_x = 4
if Temp >= 84:
temp_icon = chr(0xf7e4)
icon_pos_x = 2
elif Temp >= 80:
temp_icon = chr(0xf2c7)
elif Temp >= 75:
temp_icon = chr(0xf2c8)
elif Temp >= 60:
temp_icon = chr(0xf2c9)
elif Temp >= 40:
temp_icon = chr(0xf2ca)
draw.text((icon_pos_x, 3), temp_icon, font=font_awesome, fill=255)
# Text temp
draw.text((15, 4), str(Temp) + "°C", font=font, fill=255)
# Icon CPU
draw.text((66, 3), chr(0xf2db), font=font_awesome, fill=255)
# Text CPU Usage
draw.text((85, 4), str(CPU_Usage) + "%", font=font, fill=255)
# Icon sd-card
draw.text((2, 22), chr(0xf7c2), font=font_awesome, fill=255)
# Text Disk usage
draw.text((15, 23), str(DiskUsage) + "%", font=font, fill=255)
# Icon memory
draw.text((65, 22), chr(0xf538), font=font_awesome, fill=255)
# Text memory usage
draw.text((85, 23), str(MemUsage) + "%", font=font, fill=255)
# Icon network
draw.text((5, 44), chr(0xf6ff), font=font_awesome, fill=255)
# Text IP addresss
draw.text((29, 45), IP, font=font, fill=255)
# Wait before refresh
time.sleep(1)
@LucaDev
Copy link
Author

LucaDev commented Oct 9, 2020

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment