반응형
라즈베리파이3 B+ 온도 습도 센서 DHT22 동작코드
라즈베리파이 모델이 바뀌면서 에러가 난 부분입니다. 기존 코드에서 수정해주는 부분은
이렇게 한번 해보세요.
코드에서 57 라인을 `counter > 16` => `counter > 50` 이렇게 바꿔보세요.
라인 54~60 라인을 아래와 같이요.
/* ignore first 3 transitions */
if ( (i >= 4) && (i % 2 == 0) )
{
/* shove each bit into the storage bytes */
dht11_dat[j / 8] <<= 1;
/* if ( counter > 16 ) */
if ( counter > 50 ) /* <- !! here !! */
dht11_dat[j / 8] |= 1;
j++;
}
전체 잘 돌아가는 소스코드는 아래와 같습니다.
/*
* 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 = 21;
//static int DHTPIN = 7;
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;
}
}
laststate = sizecvt(digitalRead(DHTPIN));
if (counter == 255) break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
dht22_dat[j/8] <<= 1;
if (counter > 50)
dht22_dat[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) &&
(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)
{
int count = 0;
//여기에서 온도와 습도를 읽어옵니다.
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초간 기다립니다.
printf(" count %d", count++);
}
}
return 0 ;
}

반응형
'개발자 > Raspberry Pi3' 카테고리의 다른 글
| 라즈베리파이3 B+ 부팅 전원 안 들어올 때 해결 방법 (1) | 2024.03.13 |
|---|---|
| Python 및 C를 사용한 Raspberry Pi UART 통신 (2) | 2020.11.27 |
| Raspberry Pi로 Ubidots에 데이터 보내고 받기 1 (0) | 2020.11.04 |
| 라즈베리파이 동영상 스트리밍과 동영상 저장 (1) | 2020.06.12 |
| 스마트 농장 연동 시연 방법 - 스마트팜 데모. (1) | 2020.03.24 |
| 스마트 농장 Smart Farm 안드로이드 앱 Ver0.1. (0) | 2020.03.24 |
| Compute Module 3+ 를 이용한 IO 보드 설계 정보 (0) | 2020.03.08 |
| 수면케어 시스템 하드웨어 사진들 - PCB, 센서, 연결도 (0) | 2020.02.07 |
취업, 창업의 막막함, 외주 관리, 제품 부재!
당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약,
아이디어는 있지만 구현할 기술이 없는 막막함.
우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.
이제 고민을 멈추고, 캐어랩을 만나세요!
코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.
제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!
귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.
캐어랩