Plotting the temperature of your PI

If you are living in Arizona and you have a PI outside, you might want to keep an eye on its temperature. It might get a bit hot, and you might need to know how far you can push the PI before it starts acting funny...
So here is a recipe for doing this.

Assumptions:

I'm assuming that

  • you will use two systems: The PI, and a regular system, which I will refer to as the "PC."
  • Both are on the same network, and you can ssh into the pi, and you know the IP address of the PI, or it has a name you can somehow resolve into an IP address.
  • you use the account "pi" on the pi.
  • you have the plot utilities package installed

Shell script on the pi:

  • copy the text below into a file "thermal_recording.sh"

    ---------------------------------------
    #!/bin/bash
    while [ 0 ] ; do 
    (echo -n $(date +"%s ") " " ;cat /sys/class/thermal/thermal_zone0/temp)  >> temp_recordings ; sleep 60 ; done
    ----------------------------------------
    
  • run command

    chmod 755 thermal_recording.sh
  • To have script start on power-up run command

    crontab -e

    and add this line:

    @reboot /home/pi/thermal_recording.sh

Set up ssh login without password

  • on the PC, if you do not already have a file ~/.ssh/id_rsa.pub, run the command
    ssh-keygen
    You can give empty responses to all prompts.
  • scp the file ~/.ssh/id_rsa.pub to the pi
  • on the pi, append the content of the transferred file to file .ssh/authorized_keys
    cat ~/id_rsa.pub >> .ssh/authorized_keys
  • verify that you can ssh into the pi without the need of a password

If you want to publish your work to a web site, set up an appropriate folder there.

Set up the PC side of things

  • create a folder ~/bin
  • create the file ~/bin/temp_plot.sh with the content below. Adjust the variables DIR, PI_IP, and (if you desire so) PUBLISH_URL

    ---------------------------------------------------
    #!/bin/bash
    set -x
    
    #local variables: 
    #folder in which the work happens and the url to which to publish
    DIR=temp_plot_files
    PI_IP=xxx.xxx.xxx.xxx
    #leave this blank if you don't want to publish
    PUBLISH_URL="user@hostname:html_folder"
    
    #prep function. creates folder where the processing happens and
    #puts in the html template
    function prep
    {
    cd
    mkdir -p ${DIR}
    cd ${DIR}
    cat>abc.template<<EOF
    <html>
    <head>
    <script type="text/JavaScript">
    <!--
    function AutoRefresh( t ) {
    	setTimeout("location.reload(true);", t);
    }
    //   -->
    </script>
    </head>
    <body onload="AutoRefresh(100000)">
    <b>replacing</b><hr>
    <img src=temp.png></img>
    </body>
    </html>
    
    EOF
    }
    
    #iter function. Does the work periodically
    function iter 
    {
    rm -f `find . -type f | grep -v html | grep -v png|grep -v template`
    scp "pi@${PI_IP}:temp_recordings" .
    
    if [ $? -ne 0 ] ; then
      exit 
    fi
    
    gnuplot <<EOF
    set terminal png
    set output 'temp.png'
    set grid back
    show grid
    set xtic 3600*24
    set xdata time
    set timefmt "%s"
    set key outside bottom
    set format x "%m/%d"
    plot "temp_recordings" using (\$1-7*3600):(\$2/1000) smooth cspline with lines lw 4 title 'temp_centigrade'
    EOF
    
    cat abc.template | sed -e "s/replacing/$(date)/" > temp.html
    
    #publish if a url has been defined
    if [ "x${PUBLISH_URL}" != "x" ] ; then
      scp *.html *.png ${PUBLISH_URL}/.
    fi
    }
    
    prep 
    while [ 0 ] ; do iter ; sleep 600 ; done
    ---------------------------------------------------------
    
  • run
    chmod 755 ~/bin/temp_plot.sh
  • on the PC, run the command
    crontab -e
    and add the following line

    @reboot ~/bin/temp_plot.sh

At this point you can reboot the PI and the PC, and the scripts should start running
if you are lucky, you should see a working example here.