Go to content Go to menu

After building the Raspberry PI extension board it’s time to do some useful things with it.

The board contains an I2C temperature sensor and you can also connect external sensors so how can you use it to monitor temperatures over a long time range and store and visualize the measurements?

This post describes a very simple way to do this as well as how to store the measurement data local as well as in the cloud.

Create a file somewhere in your home directory on the Raspberry PI and call it temp.sh. Ensure that you can execute it by setting the correct file rights.

touch temp.sh
chmod 755 temp.sh

Now open temp.sh in your editor an copy and paste the following script into it.

#!/bin/bash
SCRIPTPATH=`pwd -P`
DAY=`date +"%Y%m%d"`
OLDDAY=$DAY
# parameters for cosm.com
COSM_VALUE="$SCRIPTPATH/temp-cosm.csv"
COSM_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
COSM_FEED="12345"
COSM_STREAM="RasPI_Ext_Temp"
while [ true ]; do
		# prepare new variable values
        DAY=`date +"%Y%m%d"`
        HR=`date +"%H%M%S"`
        CURRENT="temp-$DAY.log"
        OLD="temp-$OLDDAY.log"
        CURRENT_LOG="$SCRIPTPATH/$CURRENT"
        OLD_LOG="$SCRIPTPATH/$OLD"
		# read temperature from I2C sensor
        EXT=`$SCRIPTPATH/ds1624.sh`
		# check if a new day starts and if so
		# send last days measurements to dropbox /Logs folder
        if [ $DAY -ne $OLDDAY ]; then
                unix2dos -q $OLD_LOG
                /usr/local/bin/dropbox_uploader.sh 
                                upload $OLD_LOG /Logs/$OLD
                rm $OLD_LOG
                OLDDAY=$DAY
                touch $CURRENT_LOG
        fi
		# write to local logfile
        echo "$DAY-$HR $EXT" >> $CURRENT_LOG
		# send measured value to cosm.com
        echo $EXT > $COSM_VALUE
        curl --request PUT 
                --data-binary @$COSM_VALUE 
                --header "X-ApiKey: $COSM_KEY" 
                --header "Content-Type: text/csv" 
                http://api.cosm.com/v2/feeds/$COSM_FEED/datastreams/$COSM_STREAM.csv
		# wait another minute
        sleep 60s
done

It uses the script ds1624 from the extension board post to read temperatures from the DS1624 I2C temperature sensor on the extension board.

The unix utility unix2dos is used by this script to change the UNIX line endings into Windows line endings. If you dont need this you can comment out or delete this line.

Next you have to decide where to store the measured data.

The script takes a temperature measurement every minute and writes it into a file called temp-yyyymmdd.log. Where yyyymmdd represents the current date. Every day starting at midnight a new measurement file is created. Every file contains 1440 measurements consisting of one line and two fields per measurement. First field is the date and time of the measurement and the second field is the actual temperature in Celsius degrees.

20130331-000302 21.78
20130331-000405 21.75
20130331-000509 21.77
...
...

The script can also store a full days measurement file into your dropbox cloud using the
dropbox_uploader.sh. So if you want to store your data in the cloud first register for a free dropbox account, download and install the dropbox_uploader script and you are ready to go.

After some data logging it would be very interesting to create graphs from the measurements. One way would be to use for example Excel to do this but there is also a way creating charts automatically online using cosm.com. If you want to use it you have to register for a free account first. This provides you with an API key in form of a string of characters and numbers. After this create a so called feed. Feeds have a unique ID and contain one or more streams. These streams have names, contain some metadata like a description of what is measured and the unit of measurement. Note your API key, the feed id and the stream name and fill in this information in the variables on top of the script.

After some time you can watch and monitor your measurements in a graph similar to the following example:

cosm-stream.png

To use this script it is a good idea to start it in background so that it is continuously running even if you are not logged in. This can be done entering

./temp.sh &

on the command line or using an init.d script or the crontab.