MarkA
Posts: 15
Joined: Thu Jan 12, 2012 2:32 pm

CPU usage indicator

Fri Jul 20, 2012 1:31 pm

Hi all,
I guess this is a bit trivial, but we all have to start somewhere! I how have a strip of 5 leds acting as a cpu usage indicator. I am particularly pleased with myself as I have not dome much in the way of electronics since school many years ago, and this is my first proper go with Python.
My daughter uses GCompris a lot, we cannot see the usage indicator as the game is full screen, hence my need for an indicator to see why it is sluggish.

Alternatively it is a chance to play with hardware and software and quickly get something that does something in the real world as opposed to on screen.

The code, if you are interested is here:

Code: Select all

#!/usr/bin/python

#CPU utilisaton indicator comprising 5 leds indicating cpu usage
#of greater than 20, 40, 60, 80 and 95%.

#Used when the on-screen indicator cannot be seen, such as with
#full screen games.

#each led has a 470 ohm resistor.
#uses 7 GPIO ports, including 3V3(pin 1) and 0V (pin 6). 



import time
import RPi.GPIO as GPIO
import psutil


GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

while True:
	mytime = psutil.cpu_percent(interval=1, percpu=False)
	if mytime>20:
		GPIO.output(11, False)
	else:
		GPIO.output(11, True)
	
	
	if mytime>40:
		GPIO.output(12, False)
	else:
		GPIO.output(12, True)
	

	if mytime>60:
		GPIO.output(13, False)
	else:
		GPIO.output(13, True)
	
	
	if mytime>80:
		GPIO.output(15, False)
	else:
		GPIO.output(15, True)
	
	
	if mytime>95:
		GPIO.output(16, False)
	else:
		GPIO.output(16, True)
I'm sure this code will optimise further.

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: CPU usage indicator

Sat Jul 21, 2012 11:53 am

Like it. :D

It would make a nice little beginners tutorial/project to add to the Wiki so it does not get lost in the forums.
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

MarkA
Posts: 15
Joined: Thu Jan 12, 2012 2:32 pm

Re: CPU usage indicator

Sun Jul 22, 2012 12:36 pm

I have just re-read this..... Apologies to all for my terrible spelling! I was doing it on the Pi via VNC while my daughter was playing on it, so things were somewhat laggy, hence not noticing the typos.

I have an idea to take it off the breadboard and solder all the components onto a connector so it makes a single plug in module on the Pi itself. This would easily translate into a unit on a cable extension.

I'll take some pictures as I do it and make it into a tutorial.

Thanks for the support.

Any pointers for optimising the code would be good, I read something about using libraries as python does not do case statements, but I gave up and used stuff I was more familiar with.


BTW, is there an easy way to get this to automatically start on boot up?

iggykoopa
Posts: 8
Joined: Sat Jun 16, 2012 12:20 pm

Re: CPU usage indicator

Sun Jul 22, 2012 3:28 pm

you should be able to add the script to /etc/rc.local to have it start at boot. Here's some changes I would make to it. Change the order of the elif's depending on whether you are typically higher or lower cpu usage.

Code: Select all

#!/usr/bin/python

#CPU utilisaton indicator comprising 5 leds indicating cpu usage
#of greater than 20, 40, 60, 80 and 95%.

#Used when the on-screen indicator cannot be seen, such as with
#full screen games.

#each led has a 470 ohm resistor.
#uses 7 GPIO ports, including 3V3(pin 1) and 0V (pin 6). 



import time
import RPi.GPIO as GPIO
import psutil


GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

while True:
   mytime = psutil.cpu_percent(interval=1, percpu=False)
   io1 = io2 = io3 = io4 = io5 = False

   if mytime> 95:
      io1 = io2 = io3 = io4 = io5 = True
   elif mytime > 80:
      io1 = io2 = io3 = io4 = True
   elif mytime > 60:
      io1 = io2 = io3 = True
   elif mytime > 40:
      io1 = io2 = True
   elif mytime > 20:
      io1 = True

   GPIO.output(11, io1)
   GPIO.output(12, io2)
   GPIO.output(13, io3)
   GPIO.output(14, io4)
   GPIO.output(15, io5)
   GPIO.output(16, io6)

MarkA
Posts: 15
Joined: Thu Jan 12, 2012 2:32 pm

Re: CPU usage indicator

Tue Jul 24, 2012 11:13 am

Than's what I was aiming for, far more readable and I don't doubt will be much faster. Certainly it is much neater and more logical.

I particularly like the x=y=z=True notation. Didn't know you could do that and it makes all the difference.

Many thanks,

--
M.
Thinks I should start with reading the tutorials! :)

maui
Posts: 25
Joined: Sun Jul 08, 2012 10:10 am

Re: CPU usage indicator

Wed Jul 25, 2012 10:53 am

aloha,

I was going to do the same thing, but I saw you already did :)

so this was the first time for me to do something with the GPIOs.

Here ist what I did:

I started with the RPi.GPIO

Code: Select all

wget http://raspberry-gpio-python.googlecode.com/files/python-rpi.gpio_0.3.1a-1_armhf.deb
sudo dpkg -i python-rpi.gpio_0.3.1a-1_armhf.deb
after that I copied the code from iggykoopa , with the correction that there is no GPIO.output(14, io4) ;)

Code: Select all

import time
import RPi.GPIO as GPIO
import psutil


GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

while True:
   mytime = psutil.cpu_percent(interval=1, percpu=False)
   io1 = io2 = io3 = io4 = io5 = False

   if mytime> 95:
      io1 = io2 = io3 = io4 = io5 = True
   elif mytime > 80:
      io1 = io2 = io3 = io4 = True
   elif mytime > 60:
      io1 = io2 = io3 = True
   elif mytime > 40:
      io1 = io2 = True
   elif mytime > 20:
      io1 = True

   GPIO.output(11, io1)
   GPIO.output(12, io2)
   GPIO.output(13, io3)
   GPIO.output(15, io4)
   GPIO.output(16, io5)
but after that I get the error in Python to run as root..

so I tried it from the terminal
with

Code: Select all

 sudo python CPUusageLED.py
but now I get the error that I need psutil.

so I tried to install that with the psuti from the python-pip
! and on Raspbian you need to install python-dev too ;)

Code: Select all

sudo apt-get install python-pip python-dev
sudo pip install psutil 
but I still get in terminal

Code: Select all

RPi.GPIO.ModeNotSetException: Please set mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
I hope you can help me :)

EDIT: I'm using the official Raspbian image

maui
Posts: 25
Joined: Sun Jul 08, 2012 10:10 am

Re: CPU usage indicator

Wed Jul 25, 2012 11:14 am

Okay it works with the right python code :) I think they canged something in the libs.

you need a GPIO.setmode first

Code: Select all

import time
import RPi.GPIO as GPIO
import psutil

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

while True:
   mytime = psutil.cpu_percent(interval=1, percpu=False)
   io1 = io2 = io3 = io4 = io5 = False

   if mytime> 95:
      io1 = io2 = io3 = io4 = io5 = True
   elif mytime > 80:
      io1 = io2 = io3 = io4 = True
   elif mytime > 60:
      io1 = io2 = io3 = True
   elif mytime > 40:
      io1 = io2 = True
   elif mytime > 20:
      io1 = True

   GPIO.output(11, io1)
   GPIO.output(12, io2)
   GPIO.output(13, io3)
   GPIO.output(15, io4)
   GPIO.output(16, io5)

User avatar
Jim JKla
Posts: 2218
Joined: Sun Jan 29, 2012 11:15 pm
Location: Newcastle upon Tyne UK

Re: CPU usage indicator

Wed Jul 25, 2012 11:22 am

Is this ready to go to the wiki it does look like an ideal project If you need some help just post here I have the thread on watch.

If you want to go on your own feel free I don't want to muscle in just post us a link so we can find it. ;)
Noob is not derogatory the noob is just the lower end of the noob--geek spectrum being a noob is just your first step towards being an uber-geek ;)

If you find a solution please post it in the wiki the forum dies too quick

maui
Posts: 25
Joined: Sun Jul 08, 2012 10:10 am

Re: CPU usage indicator

Wed Jul 25, 2012 12:29 pm

hey,

I was fiddeling around with the psutil.cpu_percent interval a bit, becaus 1 was to slow for me :)
I wanted the leds to be more smooth.

So I came up with interval=0.2 as the sweetspot for me.

0.1 is to sensetive and 0.3 again to slowish.

so here the complete code

Code: Select all

import time
import RPi.GPIO as GPIO
import psutil

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

while True:
    mytime = psutil.cpu_percent(interval=0.2, percpu=False)
    io1=io2=io3=io4=io5 = False
    
    if mytime>95:
        io1=io2=io3=io4=io5 = True
    elif mytime > 80:
        io1=io2=io3=io4 = True
    elif mytime > 60:
        io1=io2=io3 = True
    elif mytime > 40:
        io1=io2 = True
    elif mytime > 20:
        io1 = True

    GPIO.output(11,io1)
    GPIO.output(12,io2)
    GPIO.output(13,io3)
    GPIO.output(15,io4)
    GPIO.output(16,io5)
and finally to start the python script at boot
edit the rc.local with

Code: Select all

sudo nano /etc/rc.local
and add

Code: Select all

sudo python /home/pi/CPUusageLED.py

MarkA
Posts: 15
Joined: Thu Jan 12, 2012 2:32 pm

Re: CPU usage indicator

Wed Jul 25, 2012 8:22 pm

Excellent work everyone, Many thanks for taking my initial work and turning it into a tutorial. I am currently thinking about how to take the hardware off the prototyping board and make a small tidy unit.

Hopefully, kids permitting, I'll find some time to put something together in the near future.
As those of you with small kids know, the near future can be some time away! :)

khh
Posts: 49
Joined: Thu Jul 26, 2012 12:16 am

Re: CPU usage indicator

Thu Jul 26, 2012 10:36 am

This is actually what I was planning for my own first project with the pi as well, but I was thinking it'd do encoded GPIO output so I should only need to hog 4 pins to get up to 16 different LEDs. But this is a great starting point.

maui
Posts: 25
Joined: Sun Jul 08, 2012 10:10 am

Re: CPU usage indicator

Thu Jul 26, 2012 4:15 pm

I am currently thinking about how to take the hardware off the prototyping board and make a small tidy unit.
... done ;)

I was looking for something to stick on top of the RPi, like a adruino shield :)

so I bluid my first RPi shield:
EDIT: I used low power LEDs. GPIO Pin -> 470 Ohm -> LED -> GND
https://www.dropbox.com/sh/lldlulx649wnihv/NvflzuoQP_#/
# you can't add pictures here :( :roll:

for now there are 3 more unsued GPIO pins, I was thinking of a shutdown button :)

@khh how are you going to encode? I was thinking of a shift register too, but it was easyer to start with the bare LEDs

khh
Posts: 49
Joined: Thu Jul 26, 2012 12:16 am

Re: CPU usage indicator

Thu Jul 26, 2012 6:04 pm

maui wrote:@khh how are you going to encode? I was thinking of a shift register too, but it was easyer to start with the bare LEDs
I have several different ideas, but I haven't actually sat down and designed anything. The simplest would of course to just tread the 4 pins as a 4-bit number. That would leave me 16 unique states, but I could only use one at a time. I've also considered using a shift register, but that would be complicated, or implementing a full address controller and databus, but that would be insane. I think I might actually settle for 3 pins for a CPU monitor, giving each state a simple meaning.
0 - 00 % < CPU < 10 % - 1/8 LEDs (POWER/ON)
1 - 10 % < CPU < 20 % - 2/8 LEDs
2 - 20 % < CPU < 30 % - 3/8 LEDs
3 - 30 % < CPU < 40 % - 4/8 LEDs
4 - 50 % < CPU < 60 % - 5/8 LEDs
5 - 70 % < CPU < 80 % - 6/8 LEDs
6 - 80 % < CPU < 90 % - 7/8 LEDs
7 - 90 % < CPU < 100 % - 8/8 LEDs

mikerr
Posts: 2826
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: CPU usage indicator

Thu Jul 26, 2012 6:15 pm

maui wrote: # you can't add pictures here :( :roll:
You can add them as attachment as below

I'm thinking of doing a CPU meter like this (or network meter) - was going to use an led bargraph display instead
(which is just 10 LEDS in a package):
Attachments
BL10-2712SDA1.jpg
BL10-2712SDA1.jpg (3.28 KiB) Viewed 5362 times
Android app - Raspi Card Imager - download and image SD cards - No PC required !

maui
Posts: 25
Joined: Sun Jul 08, 2012 10:10 am

Re: CPU usage indicator

Thu Jul 26, 2012 6:26 pm

You can add them as attachment as below
yeah, <64kb :roll:

bargraph leds would be nice :)

and I was thinking about a I2C interface to a arduino... but that is a project for later ;)

jamolnng
Posts: 1
Joined: Mon Jul 01, 2013 1:00 am

Re: CPU usage indicator

Mon Jul 01, 2013 1:03 am

Where does psutil come from because I can't seem to find it anywhere?

Return to “Python”