lednerg

Temperature Monitor for Raspberry Pi v21.02.25

Feb 25th, 2021 (edited)
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.40 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. # TempScan v21.02.25 for Raspberry Pi by @ThisSteveGuy
  4. #   Configurable temperature monitor with low pass filter
  5.  
  6. # Pastebin can mess up bash files, so you may need to run:
  7. # sed -i 's/\r$//' filename
  8.  
  9. # LPF = Low Pass Filter, less erratic and more realistic
  10. # Min = the lowest temperature found in the latest group of samples
  11. # Avg = the average temperature of the latest samples
  12. # Max = guess
  13.  
  14. # You'll notice a lot of exact duplicates scrolling up the Min and Max columns.
  15. #  This is because the pi's temperature sensor has a fairly low resolution.
  16. #  The Low Pass Filter (LPF) tries to correct for this shortcoming.
  17.  
  18. # The temps surrounding the gradient are for AutoColor mode,
  19. #  they're really just there for testing right now.
  20.  
  21. # ___ OPTIONS ___
  22.  
  23. # Keep making temperature readings or quit after a single one:
  24. Continuous=true
  25. # Scroll the temperature readings up the window or have them stay on the bottom:
  26. Scroll=true
  27. # Display colors as bars (colored backgrounds) or just as colored numbers:
  28. Bars=true
  29. # An option in case your terminal cannot support color:
  30. Color=true
  31. # Places labels underneath the types of measurements:
  32. ShowLabels=true
  33. # How relatively dark does a bar have to be before changing the text to white:
  34. DarkTextFix=100
  35. # Automatically adjust the cold and hot color temps over time:
  36. AutoColor=false
  37. # For setting the min and max colors manually (if AutoColor=false)
  38. #   Temperatures are in hundredths of a degree (4321 = 43.21 degrees C):
  39. ColdTemp=4000
  40. HotTemp=5500
  41. # Adjusts how aggressively the LPF column filters out erroneous readings:
  42. LowPassFilterStrength=100
  43.  
  44.  
  45. #   SELECT COLOR PALLETE BY COMMENTING OUT ALL THE OTHERS WITH # MARKS
  46. # You must have only one of each (palleteR, palleteG, palleteB) uncommented
  47.  
  48. # Colorblind-friendly palette:
  49. palleteR=( 49  69  116 171 224 255 254 253 244 215 165 255 )
  50. palleteG=( 54  117 173 217 243 255 224 174 109 48  0   255 )
  51. palleteB=( 149 180 209 233 248 191 144 97  67  39  38  255 )
  52.  
  53. # Weather map color palette:
  54. #palleteR=( 77  179 0   7   72  247 255 161 251 )
  55. #palleteG=( 10  0   7   247 180 244 110 1   222 )
  56. #palleteB=( 163 181 255 240 32  4   0   1   226 )
  57.  
  58. # Blue Cyan Green Yellow Red:
  59. #palleteR=( 0   0   128 255 128 )
  60. #palleteG=( 0   128 255 128 0   )
  61. #palleteB=( 128 255 128 0   0   )
  62.  
  63. # Blue to Red then White Hot:
  64. #palleteR=( 0   36  72  108 144 180 255 )
  65. #palleteG=( 0   0   0   0   0   0   255 )
  66. #palleteB=( 180 144 108 72  36  0   255 )
  67.  
  68.  
  69. #   TIME AND TEMPERATURE SAMPLE CONFIGURATION
  70.  
  71. # Number of temperature samples taken within each sample period:
  72. Samples=3
  73. # Duration of each sample period (ms):
  74. SamplingTime=300
  75. # Wait time between sampling periods (ms):
  76. UpdateDelay=700
  77.  
  78. #   Here's what the timeline would look like when those are set to 3, 300, and 700:
  79. #       Sample, wait 150ms, Sample, wait 150ms, Sample, wait 700ms
  80. #   That's 3 Samples in a total SamplingTime of 300ms (150+150) and an UpdateDelay of 700ms.
  81. #   Since 300ms + 700ms = 1000ms, it will take (approximately) 1 second to show each reading.
  82.  
  83. #   I honestly don't know the how many samples you can/should do within a set period of time.
  84. #   I wouldn't go higher than 10 per second. Any more would be overkill and you don't want
  85. #   the script itself to start affecting the temperature.
  86.  
  87.  
  88. # ___ END OF OPTIONS ___
  89.  
  90. letsRock() {
  91.     varInit
  92.     fixWaiter
  93.     if [ $Samples -eq 1 ] ; then
  94.         waitFor=$(($SamplingTime/$Samples))
  95.     else
  96.         waitFor=$(($SamplingTime/($Samples-1)))
  97.         if [ $UpdateDelay -ge $waitFor ] ; then
  98.             UpdateDelay=$(($UpdateDelay-$waitFor))
  99.         else
  100.             waitFor=$(($SamplingTime/$Samples))
  101.         fi
  102.     fi
  103.     colorScale
  104.     while true
  105.         do
  106.         makeSamples
  107.         output
  108.         if [ $Continuous = false ]; then echo "" ; return ; fi
  109.         waiter $UpdateDelay
  110.     done
  111. }
  112.  
  113. colorScale() {
  114.     cSRange=$(($HotTemp-$ColdTemp))
  115.     cSLeft=1
  116.     cSMaking=true
  117.     while [ $cSLeft -le 32 ] ;
  118.     do
  119.         cSColor=$((($ColdTemp+($cSRange/32)*$cSLeft) * $Big * 10))
  120.         formatText cSColor
  121.         cSMsg="${cSMsg}$formattedText"
  122.         cSLeft=$[$cSLeft+1]
  123.     done
  124.     cSMsg="${cSMsg}\E[0m"
  125.     cSMaking=false
  126. }
  127.  
  128. varInit() {
  129.     Big=999
  130.     getTemp
  131.     lpfRough=$tempRaw
  132.     lpfSmpl=$lpfRough
  133.     lpfOLD=$lpfSmpl
  134.     lpfDelta=0
  135.     smooth=100
  136.     sF=$(($LowPassFilterStrength * $LowPassFilterStrength +1))
  137.     topTemp=0
  138.     botTemp=$((99999*$Big))
  139.     colorResolution=10000
  140.     labeled=false
  141. }
  142.  
  143. getTemp() {
  144.     tempRaw=$(cat /sys/class/thermal/thermal_zone0/temp)
  145.     tempRaw=$(($tempRaw*$Big))
  146. }
  147.  
  148. makeSamples() {
  149.     avgSum=0
  150.     maxSmpl=$((-99999*$Big))
  151.     minSmpl=$((99999*$Big))
  152.     for (( k=0; k < $Samples; k++ )); do
  153.         waiter $waitFor
  154.         getTemp
  155.         avgSum=$(($avgSum + $tempRaw))
  156.         if [ $k -eq $(($Samples-1)) ]; then avgSmpl=$(($avgSum/$Samples)); fi
  157.         if [ $tempRaw -gt $maxSmpl ]; then maxSmpl=$tempRaw; fi
  158.         if [ $tempRaw -lt $minSmpl ]; then minSmpl=$tempRaw; fi
  159.         if [ $Samples -gt 1 ] ; then
  160.             lpfRough=$(($lpfRough * ($Samples-1)/$Samples + $tempRaw * 1/$Samples))
  161.         else
  162.             lpfRough=$(($lpfRough * 1/5 + $tempRaw * 4/5))
  163.         fi
  164.         lpfDelta=$(($lpfDelta * ($Samples-1)/$Samples + ($tempRaw - $lpfOLD)/$Big * 1/$Samples))
  165.         smooth=$(($lpfDelta * $lpfDelta / 100))
  166.         if [ $smooth -gt $sF ] ; then smooth=$sF ; fi
  167.         lpfSmpl=$(($lpfSmpl * ($sF-$smooth) / $sF + $lpfRough * $smooth / $sF))
  168.         lpfOLD=$lpfSmpl
  169.     done
  170.     if [ $lpfSmpl -ge $topTemp ] ; then
  171.         topTemp=$lpfSmpl
  172.         formatText topTemp
  173.         topMsg="$formattedText"
  174.     else
  175. #       topTemp=$(($topTemp*149/150+$avgSmpl*1/150))
  176.         formatText topTemp
  177.         topMsg="$formattedText"
  178.     fi
  179.     if [ $lpfSmpl -le $botTemp ] ; then
  180.         botTemp=$lpfSmpl
  181.         formatText botTemp
  182.         botMsg="$formattedText"
  183.     else
  184. #       botTemp=$(($botTemp*149/150+$avgSmpl*1/150))
  185.         formatText botTemp
  186.         botMsg="$formattedText"
  187.     fi
  188.     if [[ $AutoColor = true ]] ; then
  189.         ColdTemp=$((($botTemp / $Big +5) /10))
  190.         HotTemp=$((($topTemp / $Big +5) /10))
  191.     fi
  192. }
  193.  
  194. makeRGB() {
  195.     newColor=$((($1 - $ColdTemp) * $colorResolution / ($HotTemp - $ColdTemp +1 )))
  196.     if [[ $newColor -lt 0 ]] ; then newColor=0 ; fi
  197.     if [[ $newColor -gt $colorResolution ]] ; then newColor=$colorResolution ; fi
  198.     pal=("${palleteR[@]}")
  199.     makeColorComponent pal
  200.     red=$colorComponent
  201.     pal=("${palleteG[@]}")
  202.     makeColorComponent pal
  203.     grn=$colorComponent
  204.     pal=("${palleteB[@]}")
  205.     makeColorComponent pal
  206.     blu=$colorComponent
  207.     lum=$((( $red*299 + $grn*587 + $blu*114 ) /1000 ))
  208. }
  209.  
  210. makeColorComponent() {
  211.     palleteLength=("${#pal[@]}")
  212.     if [ $palleteLength -gt 1 ] ; then
  213.         section=$(( ($palleteLength-1) * $newColor / $colorResolution ))
  214.         leftColor=("${pal[$section]}")
  215.         rightColor=("${pal[$section+1]}")
  216.         shadesPerSection=$(($colorResolution / ($palleteLength-1)))
  217.         shade=$(( ($newColor - $shadesPerSection*$section) * 10000 / $shadesPerSection ))
  218.         colorComponent=$(($shade * ($rightColor - $leftColor) / 10000 + $leftColor))
  219.     else
  220.         colorComponent=("${pal[0]}")
  221.     fi
  222. }
  223.  
  224. output() {
  225.     labels=" \E[38;5;16;48;5;8m  LPF  \u2588  Min  \u2588  Avg  \u2588  Max  \E[0m"
  226.     formatText lpfSmpl
  227.     lpf="$formattedText"
  228.     formatText minSmpl
  229.     min="$formattedText"
  230.     formatText avgSmpl
  231.     avg="$formattedText"
  232.     formatText maxSmpl
  233.     max="$formattedText"
  234.     Messages="${botMsg}${cSMsg}${topMsg} \t"
  235.     if [[ $Scroll = true ]] ;
  236.     then
  237.         echo -e " ${lpf} ${min} ${avg} ${max}                                               "
  238.         if [[ $ShowLabels = true ]] ; then echo -ne "${labels} $Messages\r"; fi
  239.     else
  240.         if [[ $labeled = false ]] ;
  241.         then
  242.             if [[ $ShowLabels = true ]] ; then echo -e "${labels} (Press Enter to scroll once)"; fi
  243.             labeled=true
  244.         fi
  245.         echo -ne " ${lpf} ${min} ${avg} ${max} $Messages\r"
  246.     fi
  247. }
  248.  
  249. formatText() {
  250.     fourDigits=$((($1 / $Big +5) /10))
  251.     if [[ $Color = true ]] ; then
  252.         makeRGB fourDigits
  253.         if [[ $Bars = true ]] ; then
  254.             if [ $lum -le $DarkTextFix ] ; then textBG="8;2;200;200;200;4" ; else textBG="8;2;0;0;0;4" ; fi
  255.         fi
  256.         tColor="\E[3${textBG}8;2;${red};${grn};${blu}m"
  257.     else
  258.         tColor=""
  259.     fi
  260.     if [[ $cSMaking = true ]] ; then
  261.     if [[ $Bars = true ]] ; then
  262.         formattedText="$tColor "
  263.     else
  264.         formattedText="$tColor|"
  265.     fi
  266.     else
  267.         decimalize fourDigits
  268.         formattedText="$tColor $properNumber \E[0m"
  269.     fi
  270. }
  271.  
  272. decimalize() {
  273.     decimal=$(($1 - ($1 / 100)*100))
  274.     if [ $decimal -lt 10 ] ; then zero="0" ; else zero="" ; fi
  275.     properNumber="$(($fourDigits / 100)).${zero}${decimal}"
  276. }
  277.  
  278. fixWaiter() {
  279.     wTsum=0
  280.     waitAdjust=0
  281.     wTstart=$(date +%s%N)
  282.     waiter 5
  283.     wTfinish=$(date +%s%N)
  284.     wTresult=$((($wTfinish - $wTstart)/1000000))
  285.     waitAdjust=$(($wTsum+$wTresult-5))
  286. }
  287.  
  288. waiter() {
  289.     waitLeft=$(($1))
  290.     while [ $waitLeft -ge 1000 ] ;
  291.     do
  292.           if [ $waitLeft -ge 60000 ]; then sleep 60; waitLeft=$(($waitLeft-60000))
  293.         elif [ $waitLeft -ge 30000 ]; then sleep 30; waitLeft=$(($waitLeft-30000))
  294.         elif [ $waitLeft -ge 10000 ]; then sleep 10; waitLeft=$(($waitLeft-10000))
  295.         elif [ $waitLeft -ge 1000  ]; then sleep 1;  waitLeft=$(($waitLeft-1000))
  296.         fi
  297.     done
  298.     while [ $waitLeft -ge 5 -a $waitLeft -lt 1000 ] ;
  299.     do
  300.           if [ $waitLeft -ge 400 ]; then sleep .40; waitLeft=$(($waitLeft-400))
  301.         elif [ $waitLeft -ge 200 ]; then sleep .20; waitLeft=$(($waitLeft-200))
  302.         elif [ $waitLeft -ge 100 ]; then sleep .10; waitLeft=$(($waitLeft-100))
  303.         elif [ $waitLeft -ge 40 ]; then sleep .040; waitLeft=$(($waitLeft-40))
  304.         elif [ $waitLeft -ge 20 ]; then sleep .020; waitLeft=$(($waitLeft-20))
  305.         elif [ $waitLeft -ge 10 ]; then sleep .010; waitLeft=$(($waitLeft-10))
  306.         elif [ $waitLeft -ge 5 ]; then  sleep .005; waitLeft=$(($waitLeft-5))
  307.         fi
  308.         waitLeft=$(($waitLeft-$waitAdjust))
  309.     done
  310. }
  311.  
  312. letsRock
  313.  
Advertisement
Add Comment
Please, Sign In to add comment