아두이노 2개로 DTH11 온습도 센서 값 주고받기
2019. 7. 10. 14:41ㆍ드론
아두이노 2개를 사용해서, 한 쪽에서는 온습도 센서 값을 보내고 다른 하나는 받아서 시리얼로 띄우는 코드다
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String sCommand = ""; | |
char cTemp; | |
int receiveFlag = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if(receiveFlag) { | |
Serial.println(sCommand); | |
receiveFlag = 0; | |
sCommand = ""; | |
} | |
} | |
void serialEvent(void) | |
{ | |
while(Serial.available()) | |
{ | |
cTemp = Serial.read(); | |
if(cTemp == '\n') { // 개행까지 받는다 | |
receiveFlag = 1; | |
return; | |
} | |
sCommand += cTemp; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DHT.h> | |
#define DHTTYPE DHT11 | |
int pinDht = 2; | |
char buf1[30], buf2[30]; | |
DHT dht(pinDht, DHTTYPE); | |
void setup() { | |
Serial.begin(115200); | |
dht.begin(); | |
} | |
void loop() { | |
delay(1000); | |
float fTemp = dht.readTemperature(); | |
float fHumi = dht.readHumidity(); | |
if (isnan(fTemp) || isnan(fHumi)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
String msg1 = "Temperature: "; | |
msg1 += fTemp; | |
msg1 += "[C]\t\n"; | |
String msg2 = "Humidity: "; | |
msg2 += fHumi; | |
msg2 += "[%]\n"; | |
String msg1 = "TH"; | |
msg1.toCharArray(buf1, 30); | |
msg2.toCharArray(buf2, 30); | |
Serial.write(buf1); | |
Serial.write(buf2); | |
} |
'드론' 카테고리의 다른 글
내가 쓰는 Makefile (0) | 2019.07.14 |
---|---|
아두이노 시리얼 통신 제어 프로젝트 (0) | 2019.07.12 |
아두이노에 FreeRTOS 올리고 frBlink 예제 실행하기 (0) | 2019.07.09 |
아두이노 clcd 문자열 입력받아서 띄우기 (0) | 2019.07.09 |
아두이노 인터럽트로 제어하는 HIT 센서 (0) | 2019.07.09 |