라즈베리 파이2 wiringPi를 이용한 온습도 프로젝트
라즈베리 파이 구성도
LED, SWITCH, DHT
> LED
BCM 18(12번핀), Ground(8번핀) -> LED
> Switch
3.3V(1번핀), BCM 23(16번핀), GROUND(14번핀) -> Swtich
> DHT
정면에서 본 기준 맨 왼쪽(3.3V) 가운데(BCM핀), 세 번째(Empty) 네 번째(ground)
라즈베리 파이 Eclipse
프로젝트 생성한다.
wiring_dht.c 소스파일
#include <wiringPi.h>
#include <stdio.h>
#include <unistd.h>
#define MAX_COUNT 85
#define DHT_PIN 1
int dhtVal[5] = { 0 };
void readData( void) {
unsigned short state = HIGH;
unsigned short counter = 0;
unsigned short j = 0, i;
float farenheit;
for (i = 0; i < 5; i++)
dhtVal[i] = 0;
pinMode(DHT_PIN, OUTPUT);
digitalWrite(DHT_PIN, LOW);
delay(18);
digitalWrite(DHT_PIN, HIGH);
delayMicroseconds(40);
pinMode(DHT_PIN, INPUT);
for (i = 0; i < MAX_COUNT; i++) {
counter = 0;
while (digitalRead (DHT_PIN) == state) {
counter++;
delayMicroseconds(1);
if (counter == 255)
break;
}
state = digitalRead(DHT_PIN);
if (counter == 255)
break;
if ((i >= 4) && (i % 2 == 0)) {
dhtVal[j / 8] <<= 1;
if (counter > 16)
dhtVal[j / 8] |= 1;
j++;
}
}
if ((j >= 40)
&& (dhtVal[4]
== ((dhtVal[0] + dhtVal[1] + dhtVal[2] + dhtVal[3]) & 0xFF))) {
farenheit = dhtVal[2] * 9. / 5. + 32;
printf("Humidity = %d.%d %% Tempearture = %d.%d *c (%.lf *F)\n",
dhtVal[0], dhtVal[1], dhtVal[2], dhtVal[3], farenheit);
} else
printf("Invalid Data!!\n" );
}
int main() {
if(wiringPiSetup () == -1) return -1;
while(1){
readData();
delay(3000);
}
return 0;
}
프로젝트를 선택하고 Alt+Enter
'라즈베리파이' 카테고리의 다른 글
라즈베리 파이 2 웹캠 프로젝트 (4) | 2016.03.13 |
---|---|
라즈베리 파이2 nodejs 설치 및 LED, BTN, DHT 프로젝트 (0) | 2016.03.09 |
라즈베리 파이2 Wiring PI 이용한 LED_BTN (0) | 2016.03.09 |
라즈베리 파이2 Wiring PI 설치 및 이용하여 LED_BLINK (0) | 2016.03.09 |
라즈베리 파이2 GPIO LED (0) | 2016.03.09 |
라즈베리 파이2 wiringPi를 이용한 온습도 프로젝트
라즈베리 파이 구성도
LED, SWITCH, DHT
> LED
BCM 18(12번핀), Ground(8번핀) -> LED
> Switch
3.3V(1번핀), BCM 23(16번핀), GROUND(14번핀) -> Swtich
> DHT
정면에서 본 기준 맨 왼쪽(3.3V) 가운데(BCM핀), 세 번째(Empty) 네 번째(ground)
라즈베리 파이 Eclipse
프로젝트 생성한다.
wiring_dht.c 소스파일
#include <wiringPi.h>
#include <stdio.h>
#include <unistd.h>
#define MAX_COUNT 85
#define DHT_PIN 1
int dhtVal[5] = { 0 };
void readData( void) {
unsigned short state = HIGH;
unsigned short counter = 0;
unsigned short j = 0, i;
float farenheit;
for (i = 0; i < 5; i++)
dhtVal[i] = 0;
pinMode(DHT_PIN, OUTPUT);
digitalWrite(DHT_PIN, LOW);
delay(18);
digitalWrite(DHT_PIN, HIGH);
delayMicroseconds(40);
pinMode(DHT_PIN, INPUT);
for (i = 0; i < MAX_COUNT; i++) {
counter = 0;
while (digitalRead (DHT_PIN) == state) {
counter++;
delayMicroseconds(1);
if (counter == 255)
break;
}
state = digitalRead(DHT_PIN);
if (counter == 255)
break;
if ((i >= 4) && (i % 2 == 0)) {
dhtVal[j / 8] <<= 1;
if (counter > 16)
dhtVal[j / 8] |= 1;
j++;
}
}
if ((j >= 40)
&& (dhtVal[4]
== ((dhtVal[0] + dhtVal[1] + dhtVal[2] + dhtVal[3]) & 0xFF))) {
farenheit = dhtVal[2] * 9. / 5. + 32;
printf("Humidity = %d.%d %% Tempearture = %d.%d *c (%.lf *F)\n",
dhtVal[0], dhtVal[1], dhtVal[2], dhtVal[3], farenheit);
} else
printf("Invalid Data!!\n" );
}
int main() {
if(wiringPiSetup () == -1) return -1;
while(1){
readData();
delay(3000);
}
return 0;
}
프로젝트를 선택하고 Alt+Enter
'라즈베리파이' 카테고리의 다른 글
라즈베리 파이 2 웹캠 프로젝트 (4) | 2016.03.13 |
---|---|
라즈베리 파이2 nodejs 설치 및 LED, BTN, DHT 프로젝트 (0) | 2016.03.09 |
라즈베리 파이2 Wiring PI 이용한 LED_BTN (0) | 2016.03.09 |
라즈베리 파이2 Wiring PI 설치 및 이용하여 LED_BLINK (0) | 2016.03.09 |
라즈베리 파이2 GPIO LED (0) | 2016.03.09 |