Ranppa
Posts: 4
Joined: Wed Jul 01, 2015 11:34 am

16x2 LCD display to show ram and CPU info

Wed Jul 01, 2015 11:37 am

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.

User avatar
DougieLawson
Posts: 39302
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 16x2 LCD display to show ram and CPU info

Wed Jul 01, 2015 3:04 pm

What CPU and RAM info do you mean?

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}'"
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Ranppa
Posts: 4
Joined: Wed Jul 01, 2015 11:34 am

Re: 16x2 LCD display to show ram and CPU info

Wed Jul 01, 2015 3:07 pm

CPU usage in %, used ram/free ram. I already got it working with average load and CPU temp.

Ranppa
Posts: 4
Joined: Wed Jul 01, 2015 11:34 am

Re: 16x2 LCD display to show ram and CPU info

Wed Jul 01, 2015 7:59 pm

What do i put in the brackets to display ram info on the lcd?

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: 16x2 LCD display to show ram and CPU info

Fri Jul 03, 2015 10:40 am

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.

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))
Your LCD "print" command may be different from "lcd.message" depending on which LCD library you're using.

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/Free

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))
Maybe that will help you out a little bit
Last edited by JimmyN on Fri Jul 03, 2015 11:22 am, edited 1 time in total.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: 16x2 LCD display to show ram and CPU info

Fri Jul 03, 2015 11:11 am

Not entirely on-topic, but I just got a Nokia 5110 LCD module from DealExtreme and hooked it up to my Pi.

It was extremely cheap, easy to set up and looks nice.

It might be a bit small for some applications, but it makes a nice 6 line by 14 character display. With backlight!

Ranppa
Posts: 4
Joined: Wed Jul 01, 2015 11:34 am

Re: 16x2 LCD display to show ram and CPU info

Sun Jul 05, 2015 9:55 am

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.

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))
Your LCD "print" command may be different from "lcd.message" depending on which LCD library you're using.

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/Free

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))
Maybe that will help you out a little bit
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!

MetalizeYourBrain
Posts: 10
Joined: Thu Mar 24, 2016 5:24 pm

Re: 16x2 LCD display to show ram and CPU info

Sun Mar 26, 2017 12:23 pm

What about showing the CPU temperature? I've not been successfull using this code:

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)
The code compiles correctly but the display reports this:
<fuction cpu_tempC at 0x767c13f0>°C

I'm using a 20x4 LCD and everything works well.

User avatar
DougieLawson
Posts: 39302
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 16x2 LCD display to show ram and CPU info

Sun Mar 26, 2017 12:44 pm

Try this

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)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “HATs and other add-ons”