반응형


1. Dht-22와  라즈베리파이를 다음처럼 연결합니다..

Dht-22            raspberry pi

VCC                3.3V

DATA              GPIO22

GND               GND



2. WiringPi 다운로드 및 설치

$ git clone git://git.drogon.net/wiringPi

$ cd wiringPi

$ ./build


3. 예제 코드 컴파일 및 테스트

아래 코드를 라즈베리파이로 복사한 후..  컴파일 및 실행..

pi@raspberrypi:~ $ gcc dht22.c -lwiringPi

pi@raspberrypi:~ $ sudo ./a.out 

Raspberry Pi DHT11/DHT22 temperature/humidity test

Humidity = 53.5 % Temperature = 31.2 *C (88.2 *F)

Humidity = 53.7 % Temperature = 31.4 *C (88.5 *F)

Data not good, skip

Humidity = 52.5 % Temperature = 31.4 *C (88.5 *F)

Humidity = 55.2 % Temperature = 31.4 *C (88.5 *F)

Humidity = 60.0 % Temperature = 31.4 *C (88.5 *F)

Humidity = 61.7 % Temperature = 31.5 *C (88.7 *F)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 *  dht.c:
 *    read temperature and humidity from DHT11 or DHT22 sensor
 */
 
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
 
#define MAX_TIMINGS    85
#define DHT_PIN        3    /* GPIO-22 */
 
int data[5] = { 0, 0, 0, 0, 0 };
 
void read_dht_data()
{
    uint8_t laststate    = HIGH;
    uint8_t counter        = 0;
    uint8_t j            = 0, i;
 
    data[0] = data[1] = data[2] = data[3] = data[4] = 0;
 
    /* pull pin down for 18 milliseconds */
    pinMode( DHT_PIN, OUTPUT );
    digitalWrite( DHT_PIN, LOW );
    delay( 18 );
 
    /* prepare to read the pin */
    pinMode( DHT_PIN, INPUT );
 
    /* detect change and read data */
    for ( i = 0; i < MAX_TIMINGS; i++ )
    {
        counter = 0;
        while ( digitalRead( DHT_PIN ) == laststate )
        {
            counter++;
            delayMicroseconds( 1 );
            if ( counter == 255 )
            {
                break;
            }
        }
        laststate = digitalRead( DHT_PIN );
 
        if ( counter == 255 )
            break;
 
        /* ignore first 3 transitions */
        if ( (i >= 4) && (i % 2 == 0) )
        {
            /* shove each bit into the storage bytes */
            data[j / 8] <<= 1;
            if ( counter > 16 )
                data[j / 8] |= 1;
            j++;
        }
    }
 
    /*
     * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
     * print it out if data is good
     */
    if ( (j >= 40) &&
         (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
    {
        float h = (float)((data[0] << 8) + data[1]) / 10;
        if ( h > 100 )
        {
            h = data[0];    // for DHT11
        }
        float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
        if ( c > 125 )
        {
            c = data[2];    // for DHT11
        }
        if ( data[2] & 0x80 )
        {
            c = -c;
        }
        float f = c * 1.8f + 32;
        printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );
    }else  {
        printf( "Data not good, skip\n" );
    }
}
 
int main( void )
{
    printf( "Raspberry Pi DHT11/DHT22 temperature/humidity test\n" );
 
    if ( wiringPiSetup() == -1 )
        exit( 1 );
 
    while ( 1 )
    {
        read_dht_data();
        delay( 2000 ); /* wait 2 seconds before next read */
    }
 
    return(0);
}
cs



출처

http://www.uugear.com/portfolio/read-dht1122-temperature-humidity-sensor-from-raspberry-pi/



문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts