Jan 8, 2014

Monitor fireplace water temperature using Raspberry Pi + DS18B20 sensors + Alarm

Before you start make sure that you have configured cacti installation.
To complete the project, you will need the following parts :
  • Raspberry Pi
  • DS18B20 Digital temperature sensor(s)
  • 4 .7K or 10K ohm resistor
  • Jumper wire
I used waterproof version of the DS18B20 the device has three leads: red(VCC), black(GND) and yellow(DATA). Every seller may have different variation of colors!
How to connect sensor to your Raspberry Pi.


As you can see sensor is NOT waterproof, but this is only for example!
Now login in Raspberry Pi and open terminal. You can use ssh to operate with Pi.
Type these commands into a terminal window:
 sudo modprobe w1-gpio 
 sudo modprobe w1-therm 
 cd /sys/bus/w1/devices 
 ls 
 cd 28-xxxx (change this to match what serial number pops up) 
 cat w1_slave 

If everything is OK, you will see current temperature.

I have two sensors with unique serial numbers for my fireplace in/out water.
I use this perl script,because reading the sensors on the command line with cat isn't very useful.

Script is for ONLY ONE SENSOR!
 #!/usr/bin/perl
 $mods = `cat /proc/modules`;
 if ($mods =~ /w1_gpio/ && $mods =~ /w1_therm/)
 {
  print "w1 modules already loaded \n";
 }
 else 
 {
 print "loading w1 modules \n";
 $mod_gpio = `sudo modprobe w1-gpio`;
 $mod_them = `sudo modprobe w1-therm`;
 }
 $sensor_temp = `cat /sys/bus/w1/devices/10-*/w1_slave 2>&1`;
 if ($sensor_temp !~ /No such file or directory/)
 {
 if ($sensor_temp !~ /NO/)
 {
     $sensor_temp =~ /t=(\d+)/i;
     $tempreature = (($1/1000)-0); #You can fix your temp value.In this case is0
     print "fireplace temp = $tempreature\n"; #Print fireplace temp as result
     exit;
 }
 die "Error locating sensor file or sensor CRC was invalid";
 }

Now you need to install RRDtool archiving and graphing temperature readings.
This command will install it.
 sudo apt-get install rrdtool

Then create database to store readings.
 pi@raspberrypi:~/rrdtool create fireplace.rrd --step 300 \ 
 DS:temp:GAUGE:600:0:100 \ 
 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 

A shell script produces graphs.
 
 #!/bin/bash 
 RRDPATH="/home/pi/temperature/" 
 RAWCOLOUR="#FF0000" 
 TRENDCOLOUR="#0000FF" 
 # Edited 2012/12/9 to add running averages to hourly and daily graphs 
 #hour 
 rrdtool graph $RRDPATH/hour.png --start -6h \ 
 DEF:temp=$RRDPATH/fireplace.rrd:temp:AVERAGE \ 
 CDEF:trend=temp,1800,TREND \ 
 LINE2:temp$RAWCOLOUR:"Hourly Raspberry PI temperature" \ 
 LINE1:trend$TRENDCOLOUR:"30 min average" 
 #day 
 rrdtool graph $RRDPATH/day.png --start -1d \ 
 DEF:temp=$RRDPATH/fireplace.rrd:temp:AVERAGE \ 
 CDEF:trend=temp,21600,TREND \ 
 LINE2:temp$RAWCOLOUR:"Daily Raspberry PI temperature" \ 
 LINE1:trend$TRENDCOLOUR:"6h average" 
 #week 
 rrdtool graph $RRDPATH/week.png --start -1w \ 
 DEF:temp=$RRDPATH/fireplace.rrd:temp:AVERAGE \ 
 LINE2:temp$RAWCOLOUR:"Weekly Raspberry PI temperature" \ 
 #month 
 rrdtool graph $RRDPATH/month.png --start -1m \ 
 DEF:temp=$RRDPATH/fireplace.rrd:temp:AVERAGE \ 
 LINE1:temp$RAWCOLOUR:"Monthly Raspberry PI temperature" \ 
 #year 
 rrdtool graph $RRDPATH/year.png --start -1y \ 
 DEF:temp=$RRDPATH/fireplace.rrd:temp:AVERAGE \ 
 LINE1:temp$RAWCOLOUR:"Yearly Raspberry PI temperature" \ 

To run the perl sensor reading code and the graph generating script every five minutes from a cron job.
 # m h dom mon dow command 
 */5 * * * * /home/pi/temperature/get_temp_one.pl 
 */5 * * * * /home/pi/temperature/graph_temp_one.sh  

Where get_temp_one.pl is the perl sensor code, and graph_temp.sh plots the graphs.



Cacti can use external rrd files. I found very good tutorial to make it happen.
TUTORIAL: external rrd files in cacti

Aaand final result in cacti!





You can download all need files from DROPBOX
 get_temp_one.pl - script for ONE sensor 
 get_temp_multiple.pl – script for MULTIPLE sensors 
 graph_temp_one.sh – graph ONE sensor 
 graph_temp_multiple.sh – graph MULTIPLE sensor 
 create_rrd.sh – creates rrd file 
 createrrd_multi.sh - creates rrd file for multiple sensors 

If you want mail alert when temperature is high or low check here.
 

 Hope you like it! Enjoy!

You can buy  DS18B20 sensors from HERE