Raspberry Pi uses DHT11 temperature and humidity sensor

1. Related introduction

DHT11 introduction:

DHT11 is a relatively cheap temperature and humidity sensor module. Reading data only needs to occupy one IO port. Capable of measuring temperature and relative humidity simultaneously.

The data sheet of DHT11 can be seen here: http://wenku.baidu.com/view/1955cc70a417866fb84a8e7b.html

It should be noted that there is a sentence in the document: the fractional part is used for expansion, and now only 0 can be read. So the fractional part is currently always 0!


Raspberry Pi IO port introduction

I am using the B version of the Raspberry Pi, and the interface is as shown below. The NAME column is the actual IO port function of the Raspberry Pi. The PIN# column is the interface number used for programming the wiringpi and pi4j library files to be introduced later.


Introduction to wiringpi

wiringpi is a header file that controls the GPIO port of the Raspberry Pi through C language. After including this header file in C language, you can simply call the packaged method to control the GPIO port of the Raspberry Pi. The program needs to be installed first.

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

wiringpi download and install: http://wiringpi.com/download-and-install/

wiringpi documentation: http://wiringpi.com/reference/

Compile and run:

After writing the C file, compile it with the following command:

gcc -Wall -o executefilename cfilename.c -lwiringPi

gcc is the compiler, -Wall is to display warning messages when compiling, -o executefilename cfilename.c is to compile the cfilename.c file into an executable file named executefilename, -lwiringPi is to include the wiringPi header file in the executable file middle.

After compiling, a file named executefilename will be generated. You can run the following command with root privileges:

sudo ./executefilename


Introduction to pi4j

pi4j is a library file developed based on wiringpi to control the GPIO port of Raspberry Pi through java. By introducing related classes into the java program, you can use the encapsulated methods to control the GPIO port of the Raspberry Pi.

pi4j official website: http://pi4j.com/

pi4j download: http://pi4j.com/download.html

pi4j installation: http://pi4j.com/install.html

pi4j documentation: http://pi4j.com/apidocs/index.html

Compile and run:

After using pi4j, root privileges are required when compiling .java files and running .class files. E.g:

Compile:

sudo javac -classpath .:classes:/opt/pi4j/lib/'*' YourJavaFile.java

run:

sudo java -classpath .:classes:/opt/pi4j/lib/'*' YourClassFile.class

If you use sudo to compile and run and prompt that javac and java cannot be found, this is because the root user has not loaded the environment variables, so the java execution command cannot be found. You can use the following commands to switch to the root user with environment variables, and then compile and run:

sudo su - root


2. Hardware connection

Since I'm lazy and I'm currently a novice using ubuntu, I don't know what software to use to make the diagram, so I briefly describe the hardware connection in words.

The DHT11 pins with an empty grid are the front, the pins are facing down, and the 4 pins from the left are

DHT11 pin name
DHT11 pin function
Connect to the GPIO of the Raspberry Pi
1.VCC
Positive, connect 3.3V or 5V
Physical interface 1, which is 3.3V
2.DATA
data input and output
Physical interface 7, which is GPIO 7
3.NC
floating (that is, not connected anywhere)
not connected
4.GND
negative electrode
Physical interface 6, which is GND

In addition, according to the requirements of the DHT11 data sheet, I connected a 4.7K ohm resistor between DATA and VCC to function as a pull-up.


3. Software writing

Write a C program using wiringpi:

The following program draws on the program of a foreigner on the Internet, but I forgot where the address of the original post is.

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_TIME 85
#define DHT11PIN 7
#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;
}
The above program is saved as a .c file and compiled into an executable file. After running, the temperature and humidity will be printed on the screen.


The details of the data receiving and processing part in the program are as follows:

        if((i>=4)&&(i%2==0)){         //前3次分别是:1低电平,2高电平(即响应信号),3低电平(即数据第一个低电平)
                                      //i%2==0 是因为每次都是循环读取低电平和高电平,每次要循环2次才读出一个bit处理
            dht11_val[j/8]<<=1;       //读到后,j/8可以限制一个数的8个位,左移1位自动补0,相当于读出0
            if(counter>16)            //counter计数如果超过16,则高电平长,应读1.
                dht11_val[j/8]|=1;    //故再将上面数与1位或,使最后一位变成1 
            j++;                      //j++8个换成下一个数据
        }
if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))){

//这其中(dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)是将5个数相加,和1与。
//目的是防止读出数据都为0,和为0,0和1与后得0,所以if判断条件不成立,返回读取失败码。
//如果读出数据是不为0的正常数据,和1与后还得原数。



Write a java program using pi4j:

When using pi4j to write the timing required by DHT11, the DHT11 response signal was never received. Personally, I felt that java could not meet the microsecond timing required by DHT11, so the java program did not succeed. If there is any great god who successfully wrote DHT11 program in java, I hope to enlighten me.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728331&siteId=291194637