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

时间:2021-11-11 04:36:28

原文地址: 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 =1;  //use gpio1 to read data
uint32 databuf;
 
 

uint8 readSensorData(void)
{
    uint8 crc; 
    uint8 i;
 
    pinMode(pinNumber,OUTPUT); // set mode to output
    digitalWrite(pinNumber, 0); // output a high level 
    delay(25);
    digitalWrite(pinNumber, 1); // output a low level 
    pinMode(pinNumber, INPUT); // set mode to input
    pullUpDnControl(pinNumber,PUD_UP);

    delayMicroseconds(27);
    if(digitalRead(pinNumber)==0) //SENSOR ANS
       {
         while(!digitalRead(pinNumber)); //wait to high

      for(i=0;i<32;i++)
       {
       while(digitalRead(pinNumber)); //data clock start
       while(!digitalRead(pinNumber)); //data start
          delayMicroseconds(HIGH_TIME);
          databuf*=2;
           if(digitalRead(pinNumber)==1) //1
            {
                databuf++;
            }
        }

      for(i=0;i<8;i++)
       {
       while(digitalRead(pinNumber)); //data clock start
       while(!digitalRead(pinNumber)); //data start
          delayMicroseconds(HIGH_TIME);
          crc*=2;  
          if(digitalRead(pinNumber)==1) //1
           {
                crc++;
           }
        }
    return 1;
       }
   else
        {
        return 0;
         }
}
 
int main (void)
{

  printf("Use GPIO1 to read data!\n");

  if (-1 == wiringPiSetup()) {
    printf("Setup wiringPi failed!");
    return 1;
  }
 
  pinMode(pinNumber, OUTPUT); // set mode to output
  digitalWrite(pinNumber, 1); // output a high level 

  printf("Enter OS-------\n");
  while(1) {
    pinMode(pinNumber,OUTPUT); // set mode to output
    digitalWrite(pinNumber, 1); // output a high level 
    delay(3000);
    if(readSensorData())
    {
       printf("Congratulations ! Sensor data read ok!\n");
       printf("RH:%d.%d\n",(databuf>>24)&0xff,(databuf>>16)&0xff); 
       printf("TMP:%d.%d\n",(databuf>>8)&0xff,databuf&0xff);
       databuf=0;
     }
    else
     {
        printf("Sorry! Sensor dosent ans!\n");
       databuf=0;
      }
  }
  return 0;
}

 

 源代码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[5]={0,0,0,0,0}; int dht11_read_val(){ uint8_t lststate=HIGH; //last state uint8_t counter=0; uint8_t j=0,i; for(i=0;i<5;i++) dht11_val[i]=0; //host send start signal pinMode(DHT11PIN,OUTPUT); //set pin to output digitalWrite(DHT11PIN,LOW); //set to low at least 18ms delay(18); digitalWrite(DHT11PIN,HIGH); //set to high 20-40us delayMicroseconds(40); //start recieve dht response pinMode(DHT11PIN,INPUT); //set pin to input for(i=0;i<MAX_TIME;i++) { counter=0; 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(1); if(counter==255) break; } lststate=digitalRead(DHT11PIN); //read current state and store as last state. if(counter==255) //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>=4)&&(i%2==0)){ dht11_val[j/8]<<=1; //write 1 bit to 0 by moving left (auto add 0) if(counter>16) //long mean 1 dht11_val[j/8]|=1; //write 1 bit to 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))){ printf("RH:%d,TEMP:%d\n",dht11_val[0],dht11_val[2]); return 1; } else return 0; } int main(void){ int attempts=ATTEMPTS; if(wiringPiSetup()==-1) exit(1); 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(2500); } return 0; }

 

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