Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # script created by chteuchteu
- # original article here:
- # http://www.chteuchteu.com/2012/11/05/raspberry-pi-cpu-usage-monitoring-using-gpio
- # www.chteuchteu.com
- # Has to be done once at the begging of the script
- echo "4" > /sys/class/gpio/export 2> /dev/null
- echo "17" > /sys/class/gpio/export 2> /dev/null
- echo "18" > /sys/class/gpio/export 2> /dev/null
- echo "25" > /sys/class/gpio/export 2> /dev/null
- echo "22" > /sys/class/gpio/export 2> /dev/null
- echo "23" > /sys/class/gpio/export 2> /dev/null
- echo "24" > /sys/class/gpio/export 2> /dev/null
- echo "out" > /sys/class/gpio/gpio4/direction
- echo "out" > /sys/class/gpio/gpio17/direction
- echo "out" > /sys/class/gpio/gpio18/direction
- echo "out" > /sys/class/gpio/gpio25/direction
- echo "out" > /sys/class/gpio/gpio22/direction
- echo "out" > /sys/class/gpio/gpio23/direction
- echo "out" > /sys/class/gpio/gpio24/direction
- #################
- # 1 | 18 #
- # 2 | 4 #
- # 3 | 25 #
- # 4 | 17 #
- # 5 | 22 #
- # 6 | 23 #
- # 7 | 24 #
- #################
- #################################################
- # FUNCTIONS #
- #################################################
- number=0
- gpio=0
- function ON
- {
- # Allow you to turn on the del (the number of the del is contained in "number")
- # number <-> pin is done at the begining of the function
- if [ ${number} -gt 0 ] ; then
- if [ ${number} == 1 ] ; then
- gpio=18
- elif [ ${number} == 2 ] ; then
- gpio=4
- elif [ ${number} == 3 ] ; then
- gpio=25
- elif [ ${number} == 4 ] ; then
- gpio=17
- elif [ ${number} == 5 ] ; then
- gpio=22
- elif [ ${number} == 6 ] ; then
- gpio=23
- elif [ ${number} == 7 ] ; then
- gpio=24
- fi
- echo "1" > /sys/class/gpio/gpio$gpio/value
- number=0
- gpio=0
- fi
- }
- function OFF
- {
- # Allow you to turn off the del (the number of the del is contained in "number")
- # number <-> pin is done at the begining of the function
- if [ ${number} -gt 0 ] ; then
- if [ ${number} == 1 ] ; then
- gpio=18
- elif [ ${number} == 2 ] ; then
- gpio=4
- elif [ ${number} == 3 ] ; then
- gpio=25
- elif [ ${number} == 4 ] ; then
- gpio=17
- elif [ ${number} == 5 ] ; then
- gpio=22
- elif [ ${number} == 6 ] ; then
- gpio=23
- elif [ ${number} == 7 ] ; then
- gpio=24
- fi
- echo "0" > /sys/class/gpio/gpio$gpio/value
- number=0
- gpio=0
- fi
- }
- function allON
- {
- # Turns ON all the dels
- number=1 ; ON
- number=2 ; ON
- number=3 ; ON
- number=4 ; ON
- number=5 ; ON
- number=6 ; ON
- number=7 ; ON
- }
- function allOFF
- {
- # Turns OFF all the dels
- number=1 ; OFF
- number=2 ; OFF
- number=3 ; OFF
- number=4 ; OFF
- number=5 ; OFF
- number=6 ; OFF
- number=7 ; OFF
- }
- function flash
- {
- i=0
- while [ ${i} -lt 10 ] ; do
- allON
- sleep 0.08
- allOFF
- sleep 0.08
- i=$(( i + 1))
- done
- }
- #################################################
- # MAIN CODE #
- #################################################
- number=1 ; ON
- sleep 0.3
- number=2 ; ON
- sleep 0.3
- number=3 ; ON
- sleep 0.3
- number=4 ; ON
- sleep 0.3
- number=5 ; ON
- sleep 0.3
- number=6 ; ON
- sleep 0.3
- number=7 ; ON
- sleep 1
- allOFF
- i=0
- while [ ${i} -lt 200 ] ; do
- # Processing the current CPU usage
- idle=`vmstat 2 3 | tail -n1 | sed "s/\ \ */\ /g" | cut -d' ' -f 16`
- cpu=$(( 100 - idle ))
- echo "######## $i / 200 ########"
- echo "CPU usage: $cpu (idle: $idle)"
- # Switching the dels on or off depending of the "cpu" value
- allOFF
- if [ ${cpu} -lt 15 ] ; then
- number=1 ; ON
- echo " 1 0 0 0 0 0 0 "
- elif [ ${cpu} -lt 30 ] ; then
- number=1 ; ON
- number=2 ; ON
- echo " 1 1 0 0 0 0 0 "
- elif [ ${cpu} -lt 45 ] ; then
- number=1 ; ON
- number=2 ; ON
- number=3 ; ON
- echo " 1 1 1 0 0 0 0 "
- elif [ ${cpu} -lt 60 ] ; then
- number=1 ; ON
- number=2 ; ON
- number=3 ; ON
- number=4 ; ON
- echo " 1 1 1 1 0 0 0 "
- elif [ ${cpu} -lt 75 ] ; then
- number=1 ; ON
- number=2 ; ON
- number=3 ; ON
- number=4 ; ON
- number=5 ; ON
- echo " 1 1 1 1 1 0 0 "
- elif [ ${cpu} -lt 90 ] ; then
- number=1 ; ON
- number=2 ; ON
- number=3 ; ON
- number=4 ; ON
- number=5 ; ON
- number=6 ; ON
- echo " 1 1 1 1 1 1 0 "
- else
- number=1 ; ON
- number=2 ; ON
- number=3 ; ON
- number=4 ; ON
- number=5 ; ON
- number=6 ; ON
- number=7 ; ON
- echo " 1 1 1 1 1 1 1 "
- fi
- echo
- i=$(( i + 1))
- done
- allOFF
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement