Room Climate Monitor

Controlling data acquisition using a shell script

To collect the data from all sensors, I wrote a shell script which is started at boot time and stores all values in a CSV file (optional) and MySQL database, respectively. Furthermore, the script generates a UNIX <timestamp> to label the observations. The script also invokes “fswebcam” to take a snapshot with the webcam each 5 minutes. The still images are saved and renamed to <timestamp>.jpg, converted into thumbnails using ImageMagick and placed into separate folders. The UNIX <timestamp> is used to create a caption for each thumbnail. Because storage space is limited on a 16 GB SD card, pictures are deleted after 12 hours. The pictures are then presented as a gallery with the most recent picture in the middle surrounded by thumbnails. This feature can be added with a few lines of javascript and php code. In order to include javascript and php code in your posts, you should use the Exec-Php or a similar plugin and disable the WYSIWYG editor.

#!/bin/bash
# mysql username
SQLUSER="username"

# mysql password
SQLPASS="secret"

# mysql server hostname/IP
SQLHOST="localhost"
SQLDB="my_sql_database"
SQLTBL="sensor_data"

# Write each 60 seconds into the log-file
set -u
LogInterval=60
while true
do
        # Column 1: Date 2: Time 3: Unix epoch time
        TimeString=$(date +"{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}Y-{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}m-{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}d;{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}H:{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}M:{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}S")
        Timestamp=$(date +{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}s)
        TimeOffset=$(date -d '1970-01-01 0 sec' +{c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}s)
        Timestamp=$(($Timestamp - TimeOffset))
        # We request a value each 60 seconds
        if [ $(($Timestamp {c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4} 60)) -eq 0 ];
        then
        # Here we start the programs for reading sensor data:
                thData=$(sudo /var/www/bin/loldht)
                bpthData=$(sudo /var/www/bin/BMP085_read.py)

        # When the log-intervall is reached, we append data to the log-file
                if [ $(($Timestamp {c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4} $LogInterval)) -eq 0 ];
                then
                        if [ -n "$thData" ];
                        then
                        sudo echo "$TimeString;$Timestamp;$thData;$bpthData" >> /var/www/data/sensor.log
                        # Send data to the 128x64 OLED display
                        sudo python /var/www/bin/oled.py
                        # Store data in MySQL Database
                        arr=$(echo "$TimeString;$Timestamp;$thData;$bpthData" | sed "s/;/\',\'/g")
                        a="'"
                        sudo echo "INSERT INTO $SQLTBL VALUES ($a$arr$a);" | mysql -u$SQLUSER -p$SQLPASS -h$SQLHOST $SQLDB
                        fi
                fi
        fi
        sleep 1
        # Take a WebCam snapshot each 5 min
        if [ $(($Timestamp {c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4} 300)) -eq 0 ];
        then
        sudo fswebcam -c /var/www/bin/fswebcam.conf
        sleep 5
        # Use a timestamp to rename the captured image
        sudo mv /var/www/data/snaps/snap.jpg /var/www/data/snaps/$Timestamp.jpg
        sleep 1
        # Generate thumbnails for the WebCam gallery
        sudo convert -thumbnail 150 /var/www/data/snaps/$Timestamp.jpg /var/www/data/thumbs/$Timestamp.jpg
        sudo ls -C1 -t /var/www/data/snaps/*.jpg | awk 'NR>20'|xargs sudo rm -rf
        sudo ls -C1 -t /var/www/data/thumbs/*.jpg | awk 'NR>20'|xargs sudo rm -rf
        fi
        #  Use the CSV file with GNUplot each 10 min
        if [ $(($Timestamp {c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4} 600)) -eq 0 ]
        then
           sudo sh /var/www/bin/makeplot_day.sh
           sudo convert -thumbnail 150 /var/www/images/day.png /var/www/data/thumbs/day.png
        fi
        # Make a plot with GNUplot when an hour has passed
        if [ $(($Timestamp {c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4} 3605)) -eq 0 ]
        then
           sudo sh /var/www/bin/makeplot_week.sh
           sudo convert -thumbnail 150 /var/www/images/week.png /var/www/data/thumbs/week.png
        fi
        # Make a plot when 12 hours have passed
        if [ $(($Timestamp {c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4} 43210)) -eq 0 ]
        then
           sudo sh /var/www/bin/makeplot_month.sh
           sudo convert -thumbnail 150 /var/www/images/month.png /var/www/data/thumbs/month.png
        fi
done

The recorded sensor data can be used to generate graphs using GNUplot as described in the next tutorial. If you would like to apply some basic statistics on your data i.e. whiskers or boxplots, have a look at this page.