Difference between revisions of "RaspberryPi Temperature Sensor"

From Wurst-Wasser.net
Jump to: navigation, search
(Created page with "Time to build a temperature sensor with my Pi. === Schematics === === Configure the Pi === ==== Update the OS ==== apt-get update apt-get ugrade ==== Setup GPIO f...")
 
(Check setup)
 
(43 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
=== Schematics ===
 
=== Schematics ===
 
+
[[Image:RaspberryPiDS18B20.png|300px]]<br>''Remember that the [[Pi]] revisions have different pinouts! This will work for Revison 2, that is board revision 0004''<ref>[[Raspberry Board Revision Check]]</ref>. If you also want to measure humidity and don't need decimal degrees, take a look at [[RaspberryPi Humidity and Temperature Sensor]] which uses a [[DHT11]] instead of a [[DS18B20]]. But the [[DHT11]] has another disadvantage: There's no kernel module for the [[Pi]] for the [[DHT11]] for [[1-wire]].
  
 
=== Configure the [[Pi]] ===
 
=== Configure the [[Pi]] ===
 
==== Update the OS ====
 
==== Update the OS ====
 +
This not necessary, but generally a good idea.
 
  apt-get update
 
  apt-get update
  apt-get ugrade
+
  apt-get upgrade
==== Setup GPIO for 1-wire device ====
+
 
  modprobe w1+gpio
+
==== Install missing packages ====
  modprobe w1+therm
+
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
 +
That maps <tt>/sys/bus/w1</tt>, you might want to do: <tt>vi /etc/modules</tt> and add the modules
 +
 
 
==== Check setup ====
 
==== Check setup ====
 +
Check whether there is now our sensor device:
 
  cd /sys/bus/w1/devices
 
  cd /sys/bus/w1/devices
  ls
+
  ls -1
cd XX
+
(if not you might want to try adding <tt>dtoverlay=w1-gpio</tt> to <tt>/boot/config.txt</tt> and reboot)
cat XX
 
  
Result:
+
10-000802ad5087<ref>This will differ, since this is the UID of my device.</ref>
 +
w1_bus_master1
  
  ?
+
See it's content (values):
 +
  cd 10-000802ad5087<ref>This will differ, since this is the UID of my device.</ref>
 +
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<ref>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!</ref>
  
 
=== Code ===
 
=== 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"
 +
GTIMESTAMPFORMAT="%Y-%m-%d_%H:%M"
 +
GTIMESTAMP="`date +${GTIMESTAMPFORMAT}`"
 +
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}/readTemperature-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 "reset" >> "${GFILEPLOTCOMMANDS}"
 +
echo "set key inside right top vertical Right noreverse enhanced autotitles columnhead nobox" >> "${GFILEPLOTCOMMANDS}"
 +
 +
# Set time format for X axis
 +
echo "set timefmt \"%H:%M\"" >> "${GFILEPLOTCOMMANDS}"
 +
echo "set xdata time" >> "${GFILEPLOTCOMMANDS}"
 +
echo "set format x \"%H:%M\"" >> "${GFILEPLOTCOMMANDS}"
 +
 +
# Setup line style (#1) for the temperature line
 +
echo "set style line 1 lc rgb '#8b1a0e' pt 1 ps 1 lt 1 lw 2" >> "${GFILEPLOTCOMMANDS}" # http://www.gnuplotting.org/tag/grid/
 +
echo "set style data lines" >> "${GFILEPLOTCOMMANDS}"
 +
 +
# Set X tics (one tic per hour, rotate that tick-labels by 90 deg and move em a bit)
 +
echo "set xtics \"01:00\" rotate by 90 offset 0,-2 out nomirror" >> "${GFILEPLOTCOMMANDS}"
 +
 +
# Setup Grid (with line style #12)
 +
echo "set style line 12 lc rgb '#E0E0E0' lt 0 lw 1" >> "${GFILEPLOTCOMMANDS}" # http://www.gnuplotting.org/tag/grid/
 +
echo "set grid back ls 12" >> "${GFILEPLOTCOMMANDS}" # http://www.gnuplotting.org/tag/grid/
 +
 +
# Setup Title
 +
echo "set title \"Temperature on ${GTIMESTAMPDATEHUMANREADABLE}\"" >> "${GFILEPLOTCOMMANDS}"
 +
 +
# Label X and Y Axis
 +
echo "set ylabel \"°C\"" >> "${GFILEPLOTCOMMANDS}"
 +
echo "set xlabel \"Time\" offset 0,-0.5" >> "${GFILEPLOTCOMMANDS}"
 +
 +
# Setup Y range
 +
echo "set yrange [ 0.000 : ] noreverse nowriteback" >> "${GFILEPLOTCOMMANDS}"
 +
 +
# Set output file type to svg and plot it into file
 +
echo "set term svg size 640,480" >> "${GFILEPLOTCOMMANDS}"
 +
echo "set output \"${GFILEGRAPH}\"" >> "${GFILEPLOTCOMMANDS}"
 +
echo "plot \"${GFILELOG}\" using 1:2 title 'Temperature' with l ls 1" >> "${GFILEPLOTCOMMANDS}"
 +
cat "${GFILEPLOTCOMMANDS}" | gnuplot
 +
 +
You might want to use a cronjob like this:
 +
*/15 * * * *    /home/pi/Development/temperature/readTemperature.sh > /dev/null 2>&1
  
 +
=== Resulting graph ===
 +
And this is how it looks:<br>
 +
[[Image:RaspberryPiTemperaturePlotExample.svg|No, there's no mistake, it was veery hot in my living room all night and all day, until a lightning storm came]]
  
 +
----
 +
* More like this
 +
** [[RaspberryPi Humidity and Temperature Sensor]]
 +
** [[Tomat-O-Mat]]
 
* 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/
 +
** Where I learned to use xticks: http://psy.swansea.ac.uk/staff/carter/gnuplot/gnuplot_time.htm
 +
** Nice to know: How to setup timezones: <tt>dpkg-reconfigure tzdata</tt>
 +
** About various methods of connecting 1-wire devices: http://www.fhemwiki.de/wiki/Raspberry_Pi_und_1-Wire and http://blog.gegg.us/2013/03/4-different-methods-of-1-wire-access-on-raspberry-pi/
 +
** Where this will be used: [[Tomat-O-Mat]]
 +
** [[:Category:RaspberryPi|More about Raspberry Pi on this website]]
 +
* Footnotes:
 +
<references/>
  
  
 
[[Category:Development]]
 
[[Category:Development]]
 
[[Category:RaspberryPi]]
 
[[Category:RaspberryPi]]

Latest revision as of 20:49, 27 March 2018

Time to build a temperature sensor with my Pi.

Schematics

RaspberryPiDS18B20.png
Remember that the Pi revisions have different pinouts! This will work for Revison 2, that is board revision 0004[1]. If you also want to measure humidity and don't need decimal degrees, take a look at RaspberryPi Humidity and Temperature Sensor which uses a DHT11 instead of a DS18B20. But the DHT11 has another disadvantage: There's no kernel module for the Pi for the DHT11 for 1-wire.

Configure the Pi

Update the OS

This not necessary, but generally a good idea.

apt-get update
apt-get upgrade

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

That maps /sys/bus/w1, you might want to do: vi /etc/modules and add the modules

Check setup

Check whether there is now our sensor device:

cd /sys/bus/w1/devices
ls -1

(if not you might want to try adding dtoverlay=w1-gpio to /boot/config.txt and reboot)

10-000802ad5087[2]
w1_bus_master1

See it's content (values):

cd 10-000802ad5087[3]
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[4]

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"
GTIMESTAMPFORMAT="%Y-%m-%d_%H:%M"
GTIMESTAMP="`date +${GTIMESTAMPFORMAT}`"
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}/readTemperature-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 "reset" >> "${GFILEPLOTCOMMANDS}"
echo "set key inside right top vertical Right noreverse enhanced autotitles columnhead nobox" >> "${GFILEPLOTCOMMANDS}"

# Set time format for X axis
echo "set timefmt \"%H:%M\"" >> "${GFILEPLOTCOMMANDS}"
echo "set xdata time" >> "${GFILEPLOTCOMMANDS}"
echo "set format x \"%H:%M\"" >> "${GFILEPLOTCOMMANDS}"

# Setup line style (#1) for the temperature line
echo "set style line 1 lc rgb '#8b1a0e' pt 1 ps 1 lt 1 lw 2" >> "${GFILEPLOTCOMMANDS}" # http://www.gnuplotting.org/tag/grid/
echo "set style data lines" >> "${GFILEPLOTCOMMANDS}"

# Set X tics (one tic per hour, rotate that tick-labels by 90 deg and move em a bit)
echo "set xtics \"01:00\" rotate by 90 offset 0,-2 out nomirror" >> "${GFILEPLOTCOMMANDS}"

# Setup Grid (with line style #12)
echo "set style line 12 lc rgb '#E0E0E0' lt 0 lw 1" >> "${GFILEPLOTCOMMANDS}" # http://www.gnuplotting.org/tag/grid/
echo "set grid back ls 12" >> "${GFILEPLOTCOMMANDS}" # http://www.gnuplotting.org/tag/grid/

# Setup Title
echo "set title \"Temperature on ${GTIMESTAMPDATEHUMANREADABLE}\"" >> "${GFILEPLOTCOMMANDS}"

# Label X and Y Axis
echo "set ylabel \"°C\"" >> "${GFILEPLOTCOMMANDS}"
echo "set xlabel \"Time\" offset 0,-0.5" >> "${GFILEPLOTCOMMANDS}"

# Setup Y range
echo "set yrange [ 0.000 : ] noreverse nowriteback" >> "${GFILEPLOTCOMMANDS}"

# Set output file type to svg and plot it into file
echo "set term svg size 640,480" >> "${GFILEPLOTCOMMANDS}"
echo "set output \"${GFILEGRAPH}\"" >> "${GFILEPLOTCOMMANDS}"
echo "plot \"${GFILELOG}\" using 1:2 title 'Temperature' with l ls 1" >> "${GFILEPLOTCOMMANDS}"
cat "${GFILEPLOTCOMMANDS}" | gnuplot

You might want to use a cronjob like this:

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

Resulting graph

And this is how it looks:
No, there's no mistake, it was veery hot in my living room all night and all day, until a lightning storm came


  1. Raspberry Board Revision Check
  2. This will differ, since this is the UID of my device.
  3. This will differ, since this is the UID of my device.
  4. 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!