RaspberryPi Humidity and Temperature Sensor (obsolete): Difference between revisions
Jump to navigation
Jump to search
Line 52: | Line 52: | ||
while(digitalRead(DHT11PIN)==lststate) | while(digitalRead(DHT11PIN)==lststate) | ||
{ | { | ||
counter++; | |||
delayMicroseconds(1); | |||
if(counter==255) | |||
break; | |||
} | |||
lststate=digitalRead(DHT11PIN); | |||
if(counter==255) | |||
break; | break; | ||
// top 3 transistions are ignored | |||
if((i>=4)&&(i%2==0)){ | |||
dht11_val[j/8]<<=1; | |||
if(counter>16) | |||
dht11_val[j/8]|=1; | |||
j++; | |||
} | } | ||
} | } |
Revision as of 23:12, 24 June 2013
Schematics
Remember that the Pi revisions have different pinouts! This will work for Revison 2, that is board revision 0004[1]
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
Install DHT11 driver
Since there seems to be no kernel module, do this:
apt-get install git
git clone git://git.drogon.net/wiringPi cd wiringPi git pull origin cd wiringPi ./build
#include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #define MAX_TIME 85 #define DHT11PIN 7 int dht11_val[5]={0,0,0,0,0}; void dht11_read_val() { uint8_t lststate=HIGH; uint8_t counter=0; uint8_t j=0,i; float farenheit; for(i=0;i<5;i++) { dht11_val[i]=0; } pinMode(DHT11PIN,OUTPUT); digitalWrite(DHT11PIN,LOW); delay(18); digitalWrite(DHT11PIN,HIGH); delayMicroseconds(40); pinMode(DHT11PIN,INPUT); for(i=0;i<MAX_TIME;i++) { counter=0; while(digitalRead(DHT11PIN)==lststate) { counter++; delayMicroseconds(1); if(counter==255) break; } lststate=digitalRead(DHT11PIN); if(counter==255) break; // top 3 transistions are ignored if((i>=4)&&(i%2==0)){ dht11_val[j/8]<<=1; if(counter>16) dht11_val[j/8]|=1; j++;
}
} // verify checksum and print the verified data if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))) {
farenheit=dht11_val[2]*9./5.+32; printf("Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3],farenheit);
} else
printf("Invalid Data!!\n");
} int main(void) { //printf("Interfacing Temperature and Humidity Sensor (DHT11) With Raspberry Pi\n"); if(wiringPiSetup()==-1) {
exit(1);
} dht11_read_val(); return 0; }
gcc -o sensor hum_temp.c -L/usr/local/lib -lwiringPi
./sensor Humidity = 45.0 % Temperature = 22.0 *C (71.6 *F)
Check setup
Check whether there is now our sensor device:
cd /sys/bus/w1/devices ls -1
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
- More like this
- Links
- Where I got the ideas from:
- http://www.messtechniklabor.de/artikel-h0000-temperatur_und_luftfeuchtigkeit_messen.html
- http://www.tortosaforum.com/raspberrypi/dht11driver.htm
- http://www.frank-buss.de/raspberrypi/index.html
- http://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview
- http://www.geeetech.com/wiki/index.php/Electric_thermometer_by_using_DHT11_sensor_module
- Where I learned to use xticks: http://psy.swansea.ac.uk/staff/carter/gnuplot/gnuplot_time.htm
- Nice to know: How to setup timezones: dpkg-reconfigure tzdata
- 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
- More about Raspberry Pi on this website
- Where I got the ideas from:
- Footnotes:
- ↑ Raspberry Board Revision Check
- ↑ This will differ, since this is the UID of my device.
- ↑ This will differ, since this is the UID of my device.
- ↑ 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!