RaspberryPi Humidity and Temperature Sensor (obsolete): Difference between revisions

From Wurst-Wasser.net
Jump to navigation Jump to search
Line 23: Line 23:
  ./build
  ./build


Create a file <tt>hum_temp.c</tt> with this content:
  #include <wiringPi.h>
  #include <wiringPi.h>
  #include <stdio.h>
  #include <stdio.h>

Revision as of 22:53, 24 June 2013

Schematics

RaspberryPiDHT11.png
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

Create a file hum_temp.c with this content:

#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.%dC (%.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: 46.0% Temperature: 22.0C (71.6 *F)

Code

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

TBD

You might want to use a cronjob like this:

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

Resulting graph

And this is how it looks:
RaspberryPiDHT11TemperaturePlotExample.svg
RaspberryPiDHT11HumidityPlotExample.svg