[转载] 树莓派读取温湿度传感器DHT11

时间:2024-04-03 22:06:38

原文地址: http://blog.csdn.net/liang890319/article/details/8739683

硬件:

树莓派 2.0

DHT模块  接树莓派5V GND GPIO1

功能:读取传感器数据并打印出来

//
//mydht11.c
//
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h> typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32; #define HIGH_TIME 32 int pinNumber =; //use gpio1 to read data
uint32 databuf; uint8 readSensorData(void)
{
uint8 crc;
uint8 i; pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, ); // output a high level
delay();
digitalWrite(pinNumber, ); // output a low level
pinMode(pinNumber, INPUT); // set mode to input
pullUpDnControl(pinNumber,PUD_UP); delayMicroseconds();
if(digitalRead(pinNumber)==) //SENSOR ANS
{
while(!digitalRead(pinNumber)); //wait to high for(i=;i<;i++)
{
while(digitalRead(pinNumber)); //data clock start
while(!digitalRead(pinNumber)); //data start
delayMicroseconds(HIGH_TIME);
databuf*=;
if(digitalRead(pinNumber)==) //
{
databuf++;
}
} for(i=;i<;i++)
{
while(digitalRead(pinNumber)); //data clock start
while(!digitalRead(pinNumber)); //data start
delayMicroseconds(HIGH_TIME);
crc*=;
if(digitalRead(pinNumber)==) //
{
crc++;
}
}
return ;
}
else
{
return ;
}
} int main (void)
{ printf("Use GPIO1 to read data!\n"); if (- == wiringPiSetup()) {
printf("Setup wiringPi failed!");
return ;
} pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, ); // output a high level printf("Enter OS-------\n");
while() {
pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, ); // output a high level
delay();
if(readSensorData())
{
printf("Congratulations ! Sensor data read ok!\n");
printf("RH:%d.%d\n",(databuf>>)&0xff,(databuf>>)&0xff);
printf("TMP:%d.%d\n",(databuf>>)&0xff,databuf&0xff);
databuf=;
}
else
{
printf("Sorry! Sensor dosent ans!\n");
databuf=;
}
}
return ;
}

源代码2:

//dht11.c 
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_TIME 85
#define DHT11PIN 1
#define ATTEMPTS 5 //retry 5 times when no response
int dht11_val[]={,,,,}; int dht11_read_val(){
uint8_t lststate=HIGH; //last state
uint8_t counter=;
uint8_t j=,i;
for(i=;i<;i++)
dht11_val[i]=; //host send start signal
pinMode(DHT11PIN,OUTPUT); //set pin to output
digitalWrite(DHT11PIN,LOW); //set to low at least 18ms
delay();
digitalWrite(DHT11PIN,HIGH); //set to high 20-40us
delayMicroseconds(); //start recieve dht response
pinMode(DHT11PIN,INPUT); //set pin to input
for(i=;i<MAX_TIME;i++)
{
counter=;
while(digitalRead(DHT11PIN)==lststate){ //read pin state to see if dht responsed. if dht always high for 255 + 1 times, break this while circle
counter++;
delayMicroseconds();
if(counter==)
break;
}
lststate=digitalRead(DHT11PIN); //read current state and store as last state.
if(counter==) //if dht always high for 255 + 1 times, break this for circle
break;
// top 3 transistions are ignored, maybe aim to wait for dht finish response signal
if((i>=)&&(i%==)){
dht11_val[j/]<<=; //write 1 bit to 0 by moving left (auto add 0)
if(counter>) //long mean 1
dht11_val[j/]|=; //write 1 bit to 1
j++;
}
}
// verify checksum and print the verified data
if((j>=)&&(dht11_val[]==((dht11_val[]+dht11_val[]+dht11_val[]+dht11_val[])& 0xFF))){
printf("RH:%d,TEMP:%d\n",dht11_val[],dht11_val[]);
return ;
}
else
return ;
} int main(void){
int attempts=ATTEMPTS;
if(wiringPiSetup()==-)
exit();
while(attempts){ //you have 5 times to retry
int success = dht11_read_val(); //get result including printing out
if (success) { //if get result, quit program; if not, retry 5 times then quit
break;
}
attempts--;
delay();
}
return ;
}

wiringpi是通过C语言控制树莓派GPIO口的头文件。在C语言中包含这个头文件之后可以很简单的调用已经封装好的方法来控制树莓派GPIO口。程序需要先安装。

wiringpi官网:http://wiringpi.com/

wiringpi下载和安装:http://wiringpi.com/download-and-install/

wiringpi文档:http://wiringpi.com/reference/

编译和运行:

写好C文件后,通过如下命令进行编译:

$ gcc -Wall -o dt11_test dt11-.c -lwiringPi     #连接动态库

gcc是编译器,-Wall是在编译时显示警告信息,-o executefilename cfilename.c是将cfilename.c文件编译成文件名为executefilename的可执行文件,-lwiringPi是将wiringPi头文件包含在可执行文件中。

编译完之后会生成文件名为executefilename的文件,使用root权限执行如下命令即可运行:

运行程序:

$ sudo ./dt_11_test 

wiringPi

#wiringPi 源代码,我已经同步到bitbucket.org了。

https://bitbucket.org/sndnvaps/wiringpi/

github.com

https://github.com/sndnvaps/WiringPi

编译wiringpi 动态库

使用git clone  git@bitbucket.org:sndnvaps/wiringpi.git -b master_upstream        #下载源代码

cd wiringpi

./build