RaspberryPi Temperature Sensor: Difference between revisions

From Wurst-Wasser.net
Jump to navigation Jump to search
Line 107: Line 107:
----
----
* Links
* Links
** http://www.raspberrypi-spy.co.uk/2013/03/raspberry-pi-1-wire-digital-thermometer-sensor/
** Where I got the ideas from: http://www.raspberrypi-spy.co.uk/2013/03/raspberry-pi-1-wire-digital-thermometer-sensor/
** http://psy.swansea.ac.uk/staff/carter/gnuplot/gnuplot_time.htm
** Where I learned to use xticks: http://psy.swansea.ac.uk/staff/carter/gnuplot/gnuplot_time.htm
** [[:Category:RaspberryPi]]
** [[:Category:RaspberryPi]]
* Footnotes:
* Footnotes:

Revision as of 22:45, 19 June 2013

Time to build a temperature sensor with my Pi.

Schematics

RaspberryPiDS18B20.png

Configure the Pi

Update the OS

This not necessary, but generally a good idea.

apt-get update
apt-get ugrade

Install missing packages

If you have already installed them, you can skip this step.

apt-get install bc
apt-get install gnuplot

Setup kernel modules for GPIO and 1-wire device

modprobe w1-gpio
modprobe w1-therm

Check setup

Check whether there is now our sensor device:

cd /sys/bus/w1/devices
ls -1
10-000802ad5087[1]
w1_bus_master1

See it's content (values):

cd 10-000802ad5087[2]
cat w1_slave
39 00 4b 46 ff ff 03 10 9c : crc=9c YES
39 00 4b 46 ff ff 03 10 9c t=28562[3]

Code

This is how I read the device and generate a nice plot using GNUplot:

#!/bin/bash
#set -x
#
# Script: readTemperature.sh
# Author: Heiko Kretschmer
# Purpose: Reading the temperature and generating a nice plot
#
#

#
# Globals
#
GDEVICEID="10-000802ad5087"
GDEVICESPATH="/sys/bus/w1/devices"
GDEVICEVALUEFILE="w1_slave"
GTIMESTAMP="`date '+%Y-%m-%d_%H:%M'`"
GTIMESTAMPTIME="`date '+%H:%M'`"
GTIMESTAMPDATE="`date '+%Y-%m-%d'`" # ISO 8601 date format
GTIMESTAMPDATEHUMANREADABLE="`date '+%A, %Y-%m-%d'`"
GFOLDERBASE="/home/pi/Development/temperature"
GFOLDERLOGS="${GFOLDERBASE}/logs"
GFOLDERGRAPHS="${GFOLDERBASE}/graphs"
GFOLDERTMP="${GFOLDERBASE}/tmp"
GFILELOG="${GFOLDERLOGS}/readTemperature_${GTIMESTAMPDATE}.log"
GFILEGRAPH="${GFOLDERGRAPHS}/readTemperature_${GTIMESTAMPDATE}.svg"
GFILEPLOTCOMMANDS="${GFOLDERTMP}/readTemperatur-plot.cmd"

#
# Main
#

# Init
test ! -d "${GFOLDERLOGS}" && mkdir "${GFOLDERLOGS}"
test ! -d "${GFOLDERGRAPHS}" && mkdir "${GFOLDERGRAPHS}"
test ! -d "${GFOLDERTMP}" && mkdir "${GFOLDERTMP}"

# Get the temperature
VALUERAW="`cat \"${GDEVICESPATH}/${GDEVICEID}/${GDEVICEVALUEFILE}\" | grep t= | cut -d= -f2`"
VALUE="`echo \"scale = 3; ${VALUERAW} / 1000\" | bc`"
#echo Temperature: ${VALUE}

# Write it into the log (one logfile per day to get nice graphs)
echo -e "${GTIMESTAMPTIME}\t${VALUE}" >> "${GFILELOG}"

# Generate the graph
test -f "${GFILEPLOTCOMMANDS}" && rm "${GFILEPLOTCOMMANDS}"
echo "set key inside right top vertical Right noreverse enhanced autotitles columnhead nobox" >> "${GFILEPLOTCOMMANDS}"
echo "set timefmt \"%H:%M\"" >> "${GFILEPLOTCOMMANDS}"
echo "set xdata time" >> "${GFILEPLOTCOMMANDS}"
echo "set style data linespoints" >> "${GFILEPLOTCOMMANDS}"
echo "set xtics \"01:00\"" >> "${GFILEPLOTCOMMANDS}"
echo "set title \"Temperature on day ${GTIMESTAMPDATEHUMANREADABLE}\"" >> "${GFILEPLOTCOMMANDS}"
echo "set ylabel \"°C\"" >> "${GFILEPLOTCOMMANDS}"
echo "set xlabel \"Time\"" >> "${GFILEPLOTCOMMANDS}"
echo "set yrange [ 0.000 : ] noreverse nowriteback" >> "${GFILEPLOTCOMMANDS}"
echo "set term svg" >> "${GFILEPLOTCOMMANDS}"
echo "set output \"${GFILEGRAPH}\"" >> "${GFILEPLOTCOMMANDS}"
echo "plot \"${GFILELOG}\" using 1:2 title 'Temperature'" >> "${GFILEPLOTCOMMANDS}"
cat "${GFILEPLOTCOMMANDS}" | gnuplot 

You might want to use a cronjob like this:

*/15 * * * *    /home/pi/Development/temperature/readTemperature.sh > /dev/null 2&>1

And this is how it looks:

File:RapsberryPiTemperaturePlotExample.svg


  1. This will differ, since this is the UID of my device.
  2. This will differ, since this is the UID of my device.
  3. This is the temperature in degrees celsius (multiplied by 1000), therefore I have 28.562°C in my living room. OMG! It's frickin' hot!