Graphing Set-up

Install & Setup the Graphing Tool

The temperatures and humidity recorded from the 1-wire sensors, can be displayed in a series of graphs. To make it easier to read and track trends.

For the purpose of the RaspBee Pi Project, we have used an open source proven tool called rddtool.

All the graphs are generated by the graphing tool rrdtool.  This section describes how to set-up this tool and automate the reading and recording of the 1-wire sensors.

rrdtool Installation

To install this tool, use the following command:

[email protected] ~ $ sudo apt-get install rrdtool

Commentary runs up the screen about the installation process.  If prompted [Y/n] to continue enter 'Y'.

Once installed you will be returned to the prompt.

rrdtool Setup

The tool is installed in the directory /usr/bin/rrdtool, however, we are going to create a directory to where we will keep and process all the output from the temperature sensors.  To do this we first need to create an rddtool database.

Perform the following commands to make the directory:

[email protected] ~ $ sudo mkdir /RaspBeePi_Monitor
[email protected] ~ $ cd /RaspBeePi_Monitor
[email protected]:/RaspBeePi_Monitor $

Now create a file to make the database:

[email protected]:/RaspBeePi_Monitor $ sudo nano make_database.sh

This will open up the editor.  Copy the following commands into the editor and save and exit:

#!/bin/bash
rrdtool create RBP_temp_database.rrd --start N --step 300 \
DS:airtemp:GAUGE:600:U:U \
DS:hivetemp:GAUGE:600:U:U \
RRA:AVERAGE:0.5:1:12 \
RRA:AVERAGE:0.5:1:288 \
RRA:AVERAGE:0.5:12:168 \
RRA:AVERAGE:0.5:12:720 \
RRA:AVERAGE:0.5:288:365

Now make the database by running the command.

[email protected]:/RaspBeePi_Monitor $ ls make_database.sh
[email protected]:/RaspBeePi_Monitor $ sudo sh make_database.sh

Now create a file to upload the temperature readings to the newly created database.  Each time this script is run, it updates the database with the latest temperature readings.  Before we create the script, we need to make a note of the names of the sensors.  Run the following command:

[email protected]:/RaspBeePi_Monitor $ ls -l /mnt/1wire
total 0
drwxrwxrwx 1 root root 4096 Apr 27 09:12 01.52DE79130000
drwxrwxrwx 1 root root 4096 Apr 27 09:12 26.D57725020000
drwxrwxrwx 1 root root 4096 Apr 27 09:12 28.2212F0080000
drwxr-xr-x 1 root root 4096 Apr 26 18:26 alarm
drwxr-xr-x 1 root root 4096 Apr 26 18:26 bus.0
drwxr-xr-x 1 root root 4096 Apr 26 18:26 settings
drwxrwxrwx 1 root root 4096 Apr 27 09:12 simultaneous
drwxr-xr-x 1 root root 4096 Apr 26 18:26 statistics
drwxr-xr-x 1 root root 4096 Apr 26 18:26 structure
drwxr-xr-x 1 root root 4096 Apr 26 18:26 system
drwxr-xr-x 1 root root 4096 Apr 26 18:26 uncached
Make a note of the directory names that are called 28.xxxxxxxxxxxx and/or 26.xxxxxxxxxxxx.  These are the address of the 1-wire sensors from which we will take the temperature readings.  These need to be added to the database update script we are going to create.
Note, at this point you need to decide which 1-wire sensor is going to record the outside 'air temperature' and which will record the internal hive temperature.  For our purposes, we have chosen to use the temperature reading from the humidity sensor (Sensor ID starting 26) as the sensor to take the 'air temperature'.  Since this sensor also reads humidity, it will also be used to take the 'air humidity' readings later.
Now create the update script:
[email protected]:/RaspBeePi_Monitor $ sudo nano update_temp.sh

Copy and paste the following into the newly opened file. Then save and exit.

#!/bin/bash
# Read the temperature data from the sensors
#NOTE:  The sensor addresses below are to be replaced with
# the sensor addresses of the sensors connected to the RaspBeePi.
#Replace 28.000000000000 and 28.111111111111 with the  
# address names of the installed sensors
cd /RaspBeePi_Monitor/
airtempread=`cat /mnt/1wire/28.000000000000/temperature`
hivetempread=`cat /mnt/1wire/28.111111111111/temperature`
# Format reading
airtemp=`echo $airtempread | cut -c -4`
hivetemp=`echo $hivetempread | cut -c -4`
# Update database
rrdtool update RBP_temp_database.rrd N:$airtemp:$hivetemp
# Create graphs
#00a76a = green trace colour for air readings
#c43f3f = RaspBeePi Pink trace colour for hive readings
# Hourly Graph
rrdtool graph RBP_temp_log_h.png -y 2:1 --vertical-label "[deg C]" --start -1h \
DEF:airtemp=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivetemp=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airtemp#00a76a:"Air temperature [deg C]" \
LINE1:hivetemp#c43f3f:"Hive temperature [deg C]"
# Daily Graph
rrdtool graph RBP_temp_log_d.png -y 2:1 --vertical-label "[deg C]" --start -1d \
DEF:airtemp=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivetemp=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airtemp#00a76a:"Air temperature [deg C]" \
LINE1:hivetemp#c43f3f:"Hive temperature [deg C]"
#Weekly Graph
rrdtool graph RBP_temp_log_w.png -y 2:1 --vertical-label "[dec C]" --start -1w \
DEF:airtemp=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivetemp=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airtemp#00a76a:"Air temperature [deg C]" \
LINE1:hivetemp#c43f3f:"Hive temperature [deg C]"
#Monthly Graph
rrdtool graph RBP_temp_log_m.png -y 2:1 --vertical-label "[deg C]" --start -1m \
DEF:airtemp=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivetemp=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airtemp#00a76a:"Air temperature [deg C]" \
LINE1:hivetemp#c43f3f:"Hive temperature [deg C]"
#Yearly Graph
rrdtool graph RBP_temp_log_y.png -y 2:1 --vertical-label "[deg C]" --start -1y \
DEF:airtemp=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivetemp=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airtemp#00a76a:"Air temperature [deg C]" \
LINE1:hivetemp#c43f3f:"Hive temperature [deg C]"

Make the file executable by entering the following command:

[email protected]:/RaspBeePi_Monitor $ sudo chmod +x update_temp.sh

To take a reading from the temperature sensors and produce the graphs enter the following command:

[email protected]:/RaspBeePi_Monitor $ sudo sh update_temp.sh
497x155
497x155
497x155
497x155
497x155
[email protected]:/RaspBeePi_Monitor $

If you now list the files in the directory, you will see that 5 files have been created with a name format of 'RBP_temp_log_x.png'.  Each of these files is a picture of a graph with the temperatures logged onto them.  Each picture shows the average temperature for a particular time.  Temperature for the past hour, day, week, month and year.

Example graph showing temperature readings for the past 24 hours.

[email protected]:/RaspBeePi_Monitor $ ls -l
total 76
-rw-r--r-- 1 root root   248 Apr 27 09:05 make_database.sh
-rw-r--r-- 1 root root 26820 Apr 27 09:54 RBP_temp_database.rrd
-rw-r--r-- 1 root root  6166 Apr 27 09:54 RBP_temp_log_d.png
-rw-r--r-- 1 root root  6234 Apr 27 09:54 RBP_temp_log_h.png
-rw-r--r-- 1 root root  6490 Apr 27 09:54 RBP_temp_log_m.png
-rw-r--r-- 1 root root  6635 Apr 27 09:54 RBP_temp_log_w.png
-rw-r--r-- 1 root root  7199 Apr 27 09:54 RBP_temp_log_y.png
-rwxr-xr-x 1 root root  2198 Apr 27 09:54 update.sh

To  view the files we need to install some software.  

Run the following command to install a program that allows you to view the graph files from the command prompt:

[email protected]:/RaspBeePi_Monitor $ sudo apt-get install fbi

To display the image file enter the following command

[email protected]:/RaspBeePi_Monitor $ sudo fbi --autozoom --noverbose --vt 1 RBP_temp_log_d.png

Automating Graph Refresh

We really need the graphs to update with the temperatures at regular intervals.  To do this we can schedule the command to run at  regular intervals using the crontab command.

crontab is a schedule of recurring things to be done in the form of commands to be run on the system.

To add a scheduled job run the following command:

[email protected]:/RaspBeePi_Monitor $ sudo crontab -e

Now add the following line to the file, save and close the file:

 

# m h dom mon dow command
*/5 * * * * /RaspBeePi_Monitor/update_temp.sh > /RaspBeePi_Monitor/RaspBeePi_cron.log 2>&1

This command runs the update_temp.sh script every five minutes. "&> /dev/null" takes the output from this command and discards it.

 

Setting up the Graphs for the Humidity Sensor

[email protected] ~ $ cd /RaspBeePi_Monitor
[email protected]:/RaspBeePi_Monitor $

Now create a file to make the database:

[email protected]:/RaspBeePi_Monitor $ sudo nano make_humidity_database.sh

This will open up the editor.  Copy the following commands into the editor and save and exit:

#!/bin/bash
rrdtool create RBP_humidity_database.rrd --start N --step 300 \
DS:airhumidity:GAUGE:600:U:U \
DS:hivehumidity:GAUGE:600:U:U \
RRA:AVERAGE:0.5:1:12 \
RRA:AVERAGE:0.5:1:288 \
RRA:AVERAGE:0.5:12:168 \
RRA:AVERAGE:0.5:12:720 \
RRA:AVERAGE:0.5:288:365

Now make the database by running the command.

[email protected]:/RaspBeePi_Monitor $ ls make_humidity_database.sh
[email protected]:/RaspBeePi_Monitor $ sudo sh make_humidity_database.sh

Now create a file to upload the humidity readings to the newly created database.  Each time this script is run, it updates the database with the latest humidity readings.  Before we create the script, we need to make a note of the names of the sensors.  Run the following command:

[email protected]:/RaspBeePi_Monitor $ ls -l /mnt/1wire
total 0
drwxrwxrwx 1 root root 4096 Apr 27 09:12 01.52DE79130000
drwxrwxrwx 1 root root 4096 Apr 27 09:12 26.D57725020000
drwxrwxrwx 1 root root 4096 Apr 27 09:12 28.2212F0080000
drwxr-xr-x 1 root root 4096 Apr 26 18:26 alarm
drwxr-xr-x 1 root root 4096 Apr 26 18:26 bus.0
drwxr-xr-x 1 root root 4096 Apr 26 18:26 settings
drwxrwxrwx 1 root root 4096 Apr 27 09:12 simultaneous
drwxr-xr-x 1 root root 4096 Apr 26 18:26 statistics
drwxr-xr-x 1 root root 4096 Apr 26 18:26 structure
drwxr-xr-x 1 root root 4096 Apr 26 18:26 system
drwxr-xr-x 1 root root 4096 Apr 26 18:26 uncached
Make a note of the directory names that are called 26.xxxxxxxxxxxx.  These are the address of the 1-wire sensors from which we will take the humidity readings.  These need to be added to the database update script we are going to create.
Note, at this point you need to decide which 1-wire sensor is going to record the outside 'air humidity' and which will record the internal hive humidity. 
Now create the update script:
[email protected]:/RaspBeePi_Monitor $ sudo nano update_humidity.sh

Copy and paste the following into the newly opened file. Then save and exit.

#!/bin/bash
# Read the temperature data from the sensors
#NOTE:  The sensor addresses below are to be replaced with
# the sensor addresses of the sensors connected to the RaspBeePi.
#Replace 26.000000000000 and 26.111111111111 with the  
# address names of the installed sensors
cd /RaspBeePi_Monitor/
airhumidread=`cat /mnt/1wire/26.000000000000/humidity`
hivehumidread=`cat /mnt/1wire/26.111111111111/humidity`
# Format reading
airhumidity=`echo $airhumidread | cut -c -4`
hivehumidity=`echo $hivehumidread | cut -c -4`
# Update database
rrdtool update RBP_humidity_database.rrd N:$airhumidity:$hivehumidity
# Create graphs
#00a76a = green trace colour for air readings
#c43f3f = RaspBeePi Pink trace colour for hive readings
# Hourly Graph
rrdtool graph RBP_temp_log_h.png -y 2:1 --vertical-label "[% RH]" --start -1h \
DEF:airhumidity=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivehumidity=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airhumidity#00a76a:"Air temperature [% RH]" \
LINE1:hivehumidity#c43f3f:"Hive temperature [% RH]"
# Daily Graph
rrdtool graph RBP_temp_log_d.png -y 2:1 --vertical-label "[% RH]" --start -1d \
DEF:airhumidity=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivehumidity=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airhumidity#00a76a:"Air temperature [% RH]" \
LINE1:hivehumidity#c43f3f:"Hive temperature [% RH]"
#Weekly Graph
rrdtool graph RBP_temp_log_w.png -y 2:1 --vertical-label "[% RH]" --start -1w \
DEF:airhumidity=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivehumidity=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airhumidity#00a76a:"Air temperature [% RH]" \
LINE1:hivehumidity#c43f3f:"Hive temperature [% RH]"
#Monthly Graph
rrdtool graph RBP_temp_log_m.png -y 2:1 --vertical-label "[% RH]" --start -1m \
DEF:airhumidity=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivehumidity=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airhumidity#00a76a:"Air temperature [% RH]" \
LINE1:hivehumidity#c43f3f:"Hive temperature [% RH]"
#Yearly Graph
rrdtool graph RBP_temp_log_y.png -y 2:1 --vertical-label "[% RH]" --start -1y \
DEF:airhumidity=RBP_temp_database.rrd:airtemp:AVERAGE \
DEF:hivehumidity=RBP_temp_database.rrd:hivetemp:AVERAGE \
LINE1:airhumidity#00a76a:"Air temperature [% RH]" \
LINE1:hivehumidity#c43f3f:"Hive temperature [% RH]"

Make the file executable by entering the following command:

[email protected]:/RaspBeePi_Monitor $ sudo chmod +x update_humidity.sh

To take a reading from the humidity sensors and produce the graphs enter the following command:

[email protected]:/RaspBeePi_Monitor $ sudo sh update_humidity.sh
497x155
497x155
497x155
497x155
497x155
[email protected]:/RaspBeePi_Monitor $

If you now list the files in the directory, you will see that 5 files have been created with a name format of 'RBP_humidity_log_x.png'.  Each of these files is a picture of a graph with the humidity logged onto them.  Each picture shows the average humidity for a particular time.  Humidity for the past hour, day, week, month and year.

Example graph showing humidity readings for the past 24 hours.

[email protected]:/RaspBeePi_Monitor $ ls -l
total 76
-rw-r--r-- 1 root root   248 Apr 27 09:05 make_database.sh
-rw-r--r-- 1 root root 26820 Apr 27 09:54 RBP_temp_database.rrd
-rw-r--r-- 1 root root  6166 Apr 27 09:54 RBP_temp_log_d.png
-rw-r--r-- 1 root root  6234 Apr 27 09:54 RBP_temp_log_h.png
-rw-r--r-- 1 root root  6490 Apr 27 09:54 RBP_temp_log_m.png
-rw-r--r-- 1 root root  6635 Apr 27 09:54 RBP_temp_log_w.png
-rw-r--r-- 1 root root  7199 Apr 27 09:54 RBP_temp_log_y.png
-rwxr-xr-x 1 root root  2198 Apr 27 09:54 update.sh

To  view the files we need to install some software.  

Run the following command to install a program that allows you to view the graph files from the command prompt:

[email protected]:/RaspBeePi_Monitor $ sudo apt-get install fbi

To display the image file enter the following command

[email protected]:/RaspBeePi_Monitor $ sudo fbi --autozoom --noverbose --vt 1 RBP_temp_log_d.png

Automating Graph Refresh

We really need the graphs to update with the temperatures at regular intervals.  To do this we can schedule the command to run at  regular intervals using the crontab command.

crontab is a schedule of recurring things to be done in the form of commands to be run on the system.

To add a scheduled job run the following command:

[email protected]:/RaspBeePi_Monitor $ sudo crontab -e

Now add the following line to the file, save and close the file:

 

# m h dom mon dow command
*/5 * * * * /RaspBeePi_Monitor/update_temp.sh > /RaspBeePi_Monitor/RaspBeePi_cron.log 2>&1

This command runs the update_temp.sh script every five minutes. "&> /dev/null" takes the output from this command and discards it.