You should be able to modify the how hot is my pi example, changing the position variable to use the psutil module instead.
Get the psutil module using: sudo apt-get -y install python-psutil
The function we will need is: psutil.cpu_percent()
Copy the HowHotIsMy.py script to a new name using: cp HowHotIsMy.py CpuMonitor.py
Open up the CpuMonitor.py script in a text editor (such as nano).
Start by adding psutil to the import list:
# Import the library functions we need
import time
import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()
import psutil
# Setup the LedBorg GPIO pins
Next remove all of the old setup values (lines 33-36), which we do not need any more:
# A function to turn the LedBorg off
def LedBorgOff():
SetLedBorg(0, 0, 0)
# Setup for processor monitor
pathSensor = '/sys/class/thermal/thermal_zone0/temp' # ...
readingPrintMultiplier = 0.001 # ...
tempHigh = 60000 # ...
tempLow = 30000 # ...
interval = 1 # ...
try:
Now remove lines 36 to 38, they simply handle the settings:
try:
# Make sure we are using floats
tempHigh = float(tempHigh)
tempLow = float(tempLow)
while True:
Next remove the old value reading code (lines 37-40) and the old level calculation (lines 41 & 42):
try:
while True:
# Read the temperature in from the file system
fSensor = open(pathSensor, 'r')
reading = float(fSensor.read())
fSensor.close()
# Pick the relevant colour
position = (reading - tempLow) / (tempHigh - tempLow)
if position < 0.0:
Nest we need to use the psutil.cpu_percent() reading by inserting a new calculation where the old one was:
try:
while True:
# Read the CPU usage
reading = psutil.cpu_percent()
position = reading / 100.0
if position < 0.0:
Finally change the print statement on line 63 to make more sense:
SetLedBorg(red, green, blue)
# Print the latest reading
print '%01.0f %%' % (reading)
# Wait a while
Save the file and close it, you can then run it with: sudo ./CpuMonitor.py
If you want the CPU monitor to refresh more quickly, reduce the value on line 33.
I made the changes mentioned and everything works perfectly.
Now I will try to increase the spectrum of colors by adding intermediary parameters with "if position"
piborg
Mon, 11/10/2014 - 12:26
Permalink
Raspberry Pi CPU monitoring with LedBorg
You should be able to modify the how hot is my pi example, changing the
position
variable to use the psutil module instead.Get the psutil module using:
sudo apt-get -y install python-psutil
The function we will need is:
psutil.cpu_percent()
Copy the HowHotIsMy.py script to a new name using:
cp HowHotIsMy.py CpuMonitor.py
Open up the CpuMonitor.py script in a text editor (such as nano).
Start by adding psutil to the import list:
Next remove all of the old setup values (lines 33-36), which we do not need any more:
Now remove lines 36 to 38, they simply handle the settings:
Next remove the old value reading code (lines 37-40) and the old level calculation (lines 41 & 42):
Nest we need to use the
psutil.cpu_percent()
reading by inserting a new calculation where the old one was:Finally change the
print
statement on line 63 to make more sense:Save the file and close it, you can then run it with:
sudo ./CpuMonitor.py
If you want the CPU monitor to refresh more quickly, reduce the value on line 33.
saccori
Mon, 11/10/2014 - 17:13
Permalink
Great ! Very useful
I made the changes mentioned and everything works perfectly.
Now I will try to increase the spectrum of colors by adding intermediary parameters with "if position"