Advertisement
chteuchteu

Raspberry Pi: CPU usage monitoring using GPIO

Nov 5th, 2012
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.34 KB | None | 0 0
  1. #!/bin/bash
  2. # script created by chteuchteu
  3. # original article here:
  4. # http://www.chteuchteu.com/2012/11/05/raspberry-pi-cpu-usage-monitoring-using-gpio
  5. # www.chteuchteu.com
  6.  
  7. # Has to be done once at the begging of the script
  8. echo "4" > /sys/class/gpio/export 2> /dev/null
  9. echo "17" > /sys/class/gpio/export 2> /dev/null
  10. echo "18" > /sys/class/gpio/export 2> /dev/null
  11. echo "25" > /sys/class/gpio/export 2> /dev/null
  12. echo "22" > /sys/class/gpio/export 2> /dev/null
  13. echo "23" > /sys/class/gpio/export 2> /dev/null
  14. echo "24" > /sys/class/gpio/export 2> /dev/null
  15. echo "out" > /sys/class/gpio/gpio4/direction
  16. echo "out" > /sys/class/gpio/gpio17/direction
  17. echo "out" > /sys/class/gpio/gpio18/direction
  18. echo "out" > /sys/class/gpio/gpio25/direction
  19. echo "out" > /sys/class/gpio/gpio22/direction
  20. echo "out" > /sys/class/gpio/gpio23/direction
  21. echo "out" > /sys/class/gpio/gpio24/direction
  22.  
  23.         #################
  24.         #   1   |   18  #
  25.         #   2   |   4   #
  26.         #   3   |   25  #
  27.         #   4   |   17  #
  28.         #   5   |   22  #
  29.         #   6   |   23  #
  30.         #   7   |   24  #
  31.         #################
  32.  
  33.  
  34. #################################################
  35. #                   FUNCTIONS                   #
  36. #################################################
  37.  
  38. number=0
  39. gpio=0
  40.  
  41. function ON
  42. {
  43.     # Allow you to turn on the del (the number of the del is contained in "number")
  44.     # number <-> pin is done at the begining of the function
  45.  
  46.     if  [ ${number} -gt 0 ] ; then
  47.         if [ ${number} == 1 ] ; then
  48.             gpio=18
  49.         elif [ ${number} == 2 ] ; then
  50.             gpio=4
  51.         elif [ ${number} == 3 ] ; then
  52.             gpio=25
  53.         elif [ ${number} == 4 ] ; then
  54.             gpio=17
  55.         elif [ ${number} == 5 ] ; then
  56.             gpio=22
  57.         elif [ ${number} == 6 ] ; then
  58.             gpio=23
  59.         elif [ ${number} == 7 ] ; then
  60.             gpio=24
  61.         fi
  62.  
  63.         echo "1" > /sys/class/gpio/gpio$gpio/value
  64.         number=0
  65.         gpio=0
  66.     fi
  67. }
  68. function OFF
  69. {
  70.     # Allow you to turn off the del (the number of the del is contained in "number")
  71.     # number <-> pin is done at the begining of the function
  72.  
  73.     if  [ ${number} -gt 0 ] ; then
  74.         if [ ${number} == 1 ] ; then
  75.             gpio=18
  76.         elif [ ${number} == 2 ] ; then
  77.             gpio=4
  78.         elif [ ${number} == 3 ] ; then
  79.             gpio=25
  80.         elif [ ${number} == 4 ] ; then
  81.             gpio=17
  82.         elif [ ${number} == 5 ] ; then
  83.             gpio=22
  84.         elif [ ${number} == 6 ] ; then
  85.             gpio=23
  86.         elif [ ${number} == 7 ] ; then
  87.             gpio=24
  88.         fi
  89.  
  90.         echo "0" > /sys/class/gpio/gpio$gpio/value
  91.         number=0
  92.         gpio=0
  93.     fi
  94. }
  95.  
  96. function allON
  97. {
  98.     # Turns ON all the dels
  99.     number=1 ; ON
  100.     number=2 ; ON
  101.     number=3 ; ON
  102.     number=4 ; ON
  103.     number=5 ; ON
  104.     number=6 ; ON
  105.     number=7 ; ON
  106. }
  107. function allOFF
  108. {
  109.     # Turns OFF all the dels
  110.     number=1 ; OFF
  111.     number=2 ; OFF
  112.     number=3 ; OFF
  113.     number=4 ; OFF
  114.     number=5 ; OFF
  115.     number=6 ; OFF
  116.     number=7 ; OFF
  117. }
  118. function flash
  119. {
  120.     i=0
  121.     while [ ${i} -lt 10 ] ; do
  122.         allON
  123.         sleep 0.08
  124.         allOFF
  125.         sleep 0.08
  126.  
  127.         i=$(( i + 1))
  128.     done
  129. }
  130.  
  131. #################################################
  132. #                   MAIN CODE                   #
  133. #################################################
  134.  
  135. number=1 ; ON
  136. sleep 0.3
  137. number=2 ; ON
  138. sleep 0.3
  139. number=3 ; ON
  140. sleep 0.3
  141. number=4 ; ON
  142. sleep 0.3
  143. number=5 ; ON
  144. sleep 0.3
  145. number=6 ; ON
  146. sleep 0.3
  147. number=7 ; ON
  148. sleep 1
  149.  
  150. allOFF
  151.  
  152.  
  153. i=0
  154. while [ ${i} -lt 200 ] ; do
  155.     # Processing the current CPU usage
  156.     idle=`vmstat 2 3 | tail -n1 | sed "s/\ \ */\ /g" | cut -d' ' -f 16`
  157.     cpu=$(( 100 - idle ))
  158.  
  159.     echo "######## $i / 200 ########"
  160.     echo "CPU usage: $cpu        (idle: $idle)"
  161.  
  162.     # Switching the dels on or off depending of the "cpu" value
  163.     allOFF
  164.     if [ ${cpu} -lt 15 ] ; then
  165.         number=1    ; ON
  166.         echo " 1 0 0 0 0 0 0 "
  167.  
  168.     elif [ ${cpu} -lt 30 ] ; then
  169.         number=1    ; ON
  170.         number=2    ; ON
  171.         echo " 1 1 0 0 0 0 0 "
  172.  
  173.     elif [ ${cpu} -lt 45 ] ; then
  174.         number=1    ; ON
  175.         number=2    ; ON
  176.         number=3    ; ON
  177.         echo " 1 1 1 0 0 0 0 "
  178.  
  179.     elif [ ${cpu} -lt 60 ] ; then
  180.         number=1    ; ON
  181.         number=2    ; ON
  182.         number=3    ; ON
  183.         number=4    ; ON
  184.         echo " 1 1 1 1 0 0 0 "
  185.  
  186.     elif [ ${cpu} -lt 75 ] ; then
  187.         number=1    ; ON
  188.         number=2    ; ON
  189.         number=3    ; ON
  190.         number=4    ; ON
  191.         number=5    ; ON
  192.         echo " 1 1 1 1 1 0 0 "
  193.  
  194.     elif [ ${cpu} -lt 90 ] ; then
  195.         number=1    ; ON
  196.         number=2    ; ON
  197.         number=3    ; ON
  198.         number=4    ; ON
  199.         number=5    ; ON
  200.         number=6    ; ON
  201.         echo " 1 1 1 1 1 1 0 "
  202.  
  203.     else
  204.         number=1    ; ON
  205.         number=2    ; ON
  206.         number=3    ; ON
  207.         number=4    ; ON
  208.         number=5    ; ON
  209.         number=6    ; ON
  210.         number=7    ; ON
  211.         echo " 1 1 1 1 1 1 1 "
  212.     fi
  213.  
  214.     echo
  215.  
  216.     i=$(( i + 1))
  217. done
  218.  
  219. allOFF
  220.  
  221. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement