DHT11 Sensor

阅读这篇DHT11传感器文章后, 决定自己动手实验. 树莓派GPIO时好时坏, 换了两次系统, 又换了两次派, 问题仍然存在. 差一点以为树莓派坏了, 后来才发现杜邦线有问题, 谁会去怀疑连电线都有问题?! 去年为了贪便宜, 低价购买杜邦线/DHT11, 做工非常差, 不值得拥有.

卖杜邦线/DHT11的淘宝店: https://telesky.tmall.com/

dht11.png

Using C

Code

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS    85
#define DHTPIN        7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };

void read_dht11_dat()  
{
    uint8_t laststate    = HIGH;
    uint8_t counter        = 0;
    uint8_t j        = 0, i;
    float    f;

    dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;

    pinMode( DHTPIN, OUTPUT );
    digitalWrite( DHTPIN, LOW );
    delay( 18 );
    digitalWrite( DHTPIN, HIGH );
    delayMicroseconds( 40 );
    pinMode( DHTPIN, INPUT );

    for ( i = 0; i < MAXTIMINGS; i++ )
    {
        counter = 0;
        while ( digitalRead( DHTPIN ) == laststate )
        {
            counter++;
            delayMicroseconds( 1 );
            if ( counter == 255 )
            {
                break;
            }
        }
        laststate = digitalRead( DHTPIN );

        if ( counter == 255 )
            break;

        if ( (i >= 4) && (i % 2 == 0) )
        {
            dht11_dat[j / 8] <<= 1;
            if ( counter > 16 )
                dht11_dat[j / 8] |= 1;
            j++;
        }
    }

    if ( (j >= 40) &&
         (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )
    {
        f = dht11_dat[2] * 9. / 5. + 32;
        printf( "Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n",
            dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
    }else  {
        printf( "Data not good, skip\n" );
    }
}

int main( void )  
{
    printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );

    if ( wiringPiSetup() == -1 )
        exit( 1 );

    while ( 1 )
    {
        read_dht11_dat();
        delay( 1000 );
    }

    return(0);
}

Compile

$ sudo apt install wiringPi
$ gcc -o dht11 dht11.c -lwiringPi -lwiringPiDev

Run

$ sudo ./dht11
Raspberry Pi wiringPi DHT11 Temperature test program  
Data not good, skip  
Data not good, skip  
Humidity = 57.0 % Temperature = 28.0 C (82.4 F)  
Humidity = 56.0 % Temperature = 28.0 C (82.4 F)  
Humidity = 55.0 % Temperature = 28.0 C (82.4 F)  
Data not good, skip  
Humidity = 55.0 % Temperature = 28.0 C (82.4 F)  
Humidity = 54.0 % Temperature = 28.0 C (82.4 F)  
Humidity = 55.0 % Temperature = 28.0 C (82.4 F)  
Data not good, skip  
Humidity = 54.0 % Temperature = 28.0 C (82.4 F)  

Using Python

Install

$ wget https://github.com/adafruit/Adafruit_Python_DHT/archive/master.zip
$ unzip master.zip
$ cd Adafruit_Python_DHT-master
$ sudo apt install build-essential python-dev
$ sudo python setup.py install
$ python -c 'import Adafruit_DHT'

Code

#!/usr/bin/env python

import Adafruit_DHT  
import time

sensor = Adafruit_DHT.DHT11  
pin = 4

while True:  
  humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  print humidity, temperature
  time.sleep(1)

Run

$ ./dht11.py
56.0 27.0  
56.0 27.0  
57.0 27.0  
57.0 27.0  
57.0 28.0  
57.0 28.0  
58.0 28.0  
58.0 28.0  
59.0 28.0