Raspberry Pi hd44780 with i2c LCD displaying performance
This is the final product:
The display will show the status of the raid array, and will show the current percentage load of the CPU and the memory.
First follow either of these guides:
https://www.circuitbasics.com/raspberry-pi-i2c-lcd-set-up-and-programming/
https://tutorials-raspberrypi.com/control-a-raspberry-pi-hd44780-lcd-display-via-i2c/
This will go over the needed libraries (i2c_lib.py and lcddriver.py).
The below script can be placed in /bin/ and scheduled in crontab using crontab -e
and then adding the line @reboot python3 /bin/lcdshowstatus.py &
This will automatically start the script after reboot.
This is the script to display the stats:
import lcddriver
from time import *
lcd = lcddriver.lcd()
lcd.lcd_clear()
import subprocess
import psutil
# This script will display the status of the raid array, in use memory and CPU load on the lcd screen.
# The LCD has two lines, so the script will display the raid status on the first line and the memory and CPU load on the second line.
# The script will run in an infinite loop, updating the status every 60 seconds.
# The script will use the mdadm command to get the raid status and psutil to get the memory and CPU load.
while True:
output = subprocess.check_output(['mdadm', '-D', '/dev/md0'])
output = output.decode('utf-8').split("\n")
# get the line that has the state:
for line in output:
if "State :" in line:
state = line.strip()
break
mem = psutil.virtual_memory()
lcd.lcd_clear()
lcd.lcd_display_string(state, 1)
lcd.lcd_display_string("Mem:" + str(int(mem.percent)) + "% CPU:" + str(int(psutil.cpu_percent())) + "%", 2)
sleep(60)
The full project is also available on Github. The needed libraries that work with this code are included.