Could someone please explain to can i get the CPU and ram info to show on my 16x2 LCD screen?
what commands do i neet to put in the python code?
Thanks in advance.
Code: Select all
GET_IP_CMD = "hostname --all-ip-addresses"
GET_TEMP_CMD = "/opt/vc/bin/vcgencmd measure_temp"
TOTAL_MEM_CMD = "free | grep 'Mem' | awk '{print $2}'"
USED_MEM_CMD = "free | grep '\-\/+' | awk '{print $3}'"Code: Select all
import psutil
# .. more script
# .. more script
while True:
CPU_usage = psutil.cpu_percent(interval = .5)
lcd.message('CPU Load %s%% ' % (CPU_usage))
Code: Select all
import os
# .. more script
# .. more script
# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def getRAMinfo():
p = os.popen('free')
i = 0
while 1:
i = i + 1
line = p.readline()
if i==2:
return(line.split()[1:4])
while True:
RAM_stats = getRAMinfo()
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)
lcd.message('RAM Used %sMB \n' % (RAM_used))
lcd.message('RAM Free %sMB ' % (RAM_free))
I used your code and got it working! I have no experience what so ever in coding but with a few cups of coffee i figured this out! Thank you all for your replies!JimmyN wrote:I have one Pi set up with a LED bargraph for an "at a glance" status that shows CPU temp, RAM used, and CPU load.
Another Pi is set up with a 16x2 LCD that scrolls through system information like IP address, CPU load, CPU clock speed, CPU temp, disk space/usage, etc.
To display CPU usage on the LCD I use 'psutils' to fetch the info. Here are those bits from the script.Your LCD "print" command may be different from "lcd.message" depending on which LCD library you're using.Code: Select all
import psutil # .. more script # .. more script while True: CPU_usage = psutil.cpu_percent(interval = .5) lcd.message('CPU Load %s%% ' % (CPU_usage))
For RAM usage I wasn't sure which values I wanted to display; Total, Free, or Used. So i use a function that collects all three and then I can decide what to display on the bargraph or LCD. For the LED bargraph I only show 'Used', but for the 2 line LCD display I use 'Free' and 'Used' since I have two lines to fill anyway. Or I can easily switch and use Total/Free or Used/FreeMaybe that will help you out a little bitCode: Select all
import os # .. more script # .. more script # Return RAM information (unit=kb) in a list # Index 0: total RAM # Index 1: used RAM # Index 2: free RAM def getRAMinfo(): p = os.popen('free') i = 0 while 1: i = i + 1 line = p.readline() if i==2: return(line.split()[1:4]) while True: RAM_stats = getRAMinfo() RAM_used = round(int(RAM_stats[1]) / 1000,1) RAM_free = round(int(RAM_stats[2]) / 1000,1) lcd.message('RAM Used %sMB \n' % (RAM_used)) lcd.message('RAM Free %sMB ' % (RAM_free))
Code: Select all
#more code and imported all the correct libraries
def cpu_tempC():
f = os.popen("/opt/vc/bin/vcgencmd measure_temp")
for i in f.readlines():
mytemp += i
mytemp = mytemp[5:-3]
#a while loop that shows correctly other informations
lcd.lcd_display_string("%s" % cpu_tempC + chr(223) + "C", 4)
Code: Select all
#more code and imported all the correct libraries
def cpu_tempC():
mytemp = ""
f = os.popen("/opt/vc/bin/vcgencmd measure_temp")
for i in f.readlines():
mytemp += i
mytemp = mytemp[5:-3]
return mytemp
#a while loop that shows correctly other informations
print("%s" % cpu_tempC() + chr(223) + "C", 4)