개발자/Raspberry Pi3

RHT-05 온도 습도센서를 사용해 Raspberry pi3 에서 데이터 획득하기

지구빵집 2017. 6. 8. 10:57
반응형



RHT-05 온도 습도센서를 사용해 Raspberry pi3 에서 데이터 획득하기

1. Spec 요약

- 습도범위: 0-100%RH

- 습도정밀도:±2 %RH

- 온도범위:-40~120 °C

- 온도정밀도:±0.3 °C

- 브랜드 : 씨링크테크


2. 파는곳

엘레파츠 https://www.eleparts.co.kr/EPX3CHDC

디바이스 마트 https://www.devicemart.co.kr/30181


3. Spec 상세 제품특징

상대 습도 & 온도 센서 

humidity 0-100%RH;          temperature -40 ~ 80Celsius

정밀도 : humidity +-2%RH;          temperature +-0.3Celsius

(Max +-5%RH);

고 정밀 Capacitive type 센서 (Polymer humidity capacitor)

보정된 디지털 출력 (1 와이어 인터페이스)

최대 전송거리 : ~100M

뛰어난 장기 안정성 : +-0.5%RH/year

저전력 소비

일체형 타입 (필터와 외장케이스)으로 설치가 편리

나사못 수량 랜덤 발송 (2개 or 3개)


RHT Series 온습도 센서 특징 - 판매처 자료

Part Number

Humidity범위

Humidity 정밀도
[%RH]

Temperature 범위

Temperature 정밀도

Package

RHT01

20-90%RH

±5 %RH

0-50 °C

±2 °C

4 Pin

RHT02

0-100%RH

±3 %RH

-40~80 °C

±0.5 °C

3 Wire

RHT03

0-100%RH

±2 %RH

-40~80 °C

±0.5 °C

4 Pin

RHT04

0-100%RH

±2 %RH

-40~100 °C

±0.3 °C

4 Pin

RHT05

0-100%RH

±2 %RH

-40~120 °C

±0.3

3 Wire

 

4. 라즈베리 파이 3 소스코드와 분석

아래 소스코드이다. 소스코드 편집은 nano 에디터를 사용해서 편집한다. 명령어는

$nano get-humitemp.c 이다. 화살표 키를 이용하여 편집 후 빠져 나올 때는 strl-x 를 누르고 y 를 누르고 엔터를 치면 된다.

센서의 출력은 90 라인의 DHTPIN = 11 에 표시된다. 이것은 wiriingPi 핀 번호를 쓰는 것이다. 와이어링 파이 라이브러리에 대한 자세한 설명은 아래 링크를 참고한다.


Raspberry Pi GPIO 와 wiringPi 라이브러리  http://fishpoint.tistory.com/1814


즉 센서의 출력핀 DHTPIN = 11 이라는 말은 wiringPi 핀번호 11번에 연결된 것이고, 물리적인 핀번호 26 번, 핀 이름은 CE1 에 연결되어 있다는 말이다.


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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 *      dht22.c:
 *    Simple test program to test the wiringPi functions
 *    Based on the existing dht11.c
 *    Amended by technion@lolware.net
 */
 
#include <wiringPi.h>
 
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
 
#define MAXTIMINGS 85
 
static int DHTPIN = 11;

static int dht22_dat[5= {0,0,0,0,0};
 
static uint8_t sizecvt(const int read)
{
    /* digitalRead() and friends from wiringpi are defined as returning a value
    < 256. However, they are returned as int() types. This is a safety function */
 
    if (read > 255 || read < 0)
      {
        printf("Invalid data from wiringPi library\n");
        exit(EXIT_FAILURE);
      }
      return (uint8_t)read;
}
 
static int read_dht22_dat()
{
    uint8_t laststate = HIGH;
     uint8_t counter = 0;
      uint8_t j = 0, i;
 
      dht22_dat[0= dht22_dat[1= dht22_dat[2= dht22_dat[3= dht22_dat[4= 0;
 
      // pull pin down for 18 milliseconds
      pinMode(DHTPIN, OUTPUT);
      digitalWrite(DHTPIN, HIGH);
      delay(10);
      digitalWrite(DHTPIN, LOW);
      delay(18);
      // then pull it up for 40 microseconds
      digitalWrite(DHTPIN, HIGH);
      delayMicroseconds(40); 
      // prepare to read the pin
      pinMode(DHTPIN, INPUT);
 
      // detect change and read data
      for ( i=0; i< MAXTIMINGS; i++) {
        counter = 0;
        while (sizecvt(digitalRead(DHTPIN)) == laststate) {
              counter++;
              delayMicroseconds(1);
              if (counter == 255) {
                break;
              }
        }//end wile
        laststate = sizecvt(digitalRead(DHTPIN));
 
        if (counter == 255break;
 
        // ignore first 3 transitions
        if ((i >= 4&& (i%2 == 0)) {
          // shove each bit into the storage bytes
              dht22_dat[j/8<<= 1;
              if (counter > 16)
                dht22_dat[j/8|= 1;
                  j++;
        }
      }//end for
 
  // check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
  // print it out if data is good
      if ((j >= 40&& (dht22_dat[4== ((dht22_dat[0+ dht22_dat[1+ dht22_dat[2+ dht22_dat[3]) & 0xFF)) ) 
    {
        float t, h;
        h = (float)dht22_dat[0* 256 + (float)dht22_dat[1];
        h /= 10;
        t = (float)(dht22_dat[2& 0x7F)* 256 + (float)dht22_dat[3];
        t /= 10.0;
        if ((dht22_dat[2& 0x80!= 0)  t *= -1;
 
        printf("Humidity = %.2f %% Temperature = %.2f *C \n", h, t );
        return 1;
      }
      else
      {
        printf("Data not good, skip\n");
        return 0;
      }
}
 
//int main (int argc, char *argv[])
int main (void)
{
      //여기에서 온도와 습도를 읽어옵니다.
      if (wiringPiSetup() == -1)
        exit(EXIT_FAILURE) ;
    
      if (setuid(getuid()) < 0)
      {
        perror("Dropping privileges failed\n");
        exit(EXIT_FAILURE);
      }
 
      while(1){
          while (read_dht22_dat() == 0
          {
             delay(1000); // wait 1sec to refresh 1초간 기다립니다.
          }
    }
  return 0 ;
}
 
cs


컴파일 명령

$gcc -o get-humitemp get-humitemp.c -lwiringPi

하면 에러없이 컴파일이 될 것이다. 


5. 실행 결과 화면

아래 명령어를 사용하여 실행하면 다음과 같은 결과 화면을 볼 수 있다.

$sudo ./get-humitemp

데이터 출력시 나오는 Data not good, skip 메시지는 1-wire 통신을 수행하면서 데이터가 잘리거나 이상이 발생할 경우 데이터를 버리면서 나오는 메세지므로 동작은 잘하는 것이다.


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
pi@raspberrypi:~/smartfarm $ sudo ./get-humitemp
Humidity = 48.50 % Temperature = 26.70 *C
Data not good, skip
Humidity = 47.90 % Temperature = 26.80 *C
Data not good, skip
Humidity = 47.60 % Temperature = 26.80 *C
Data not good, skip
Humidity = 47.30 % Temperature = 26.80 *C
Data not good, skip
Data not good, skip
Data not good, skip
Humidity = 46.20 % Temperature = 26.90 *C
Data not good, skip
Humidity = 46.20 % Temperature = 26.90 *C
Data not good, skip
Humidity = 46.20 % Temperature = 26.90 *C
Data not good, skip
Data not good, skip
Humidity = 46.20 % Temperature = 26.90 *C
Data not good, skip
Humidity = 46.20 % Temperature = 26.90 *C
Data not good, skip
Humidity = 46.20 % Temperature = 26.90 *C
Data not good, skip
 
cs


6. 여러 참고 사진 

아래는 판매처 사이트와 테스트 하면서 찍은 사진들이다. 참고하시길~








반응형