본문 바로가기
kipfa 필기

0714_먼지센서 구동_막대그래프로 시각적으로 표시

by 헤옹스 2017. 7. 14.




---------------------------------------------------------------------------------------------------------------------------

구글 - Nodemcu softwareserial 검색

http://www.instructables.com/id/ESP8266-Two-Serial-Ports-With-SoftwareSerial-Libra/

사이트 내 espsoftwareserial 클릭

https://github.com/plerup/espsoftwareserial 에서

Clone or download 클릭 - Download ZIP으로 다운로드

아두이노 실행 - 스케치 - 라이브러리 포함하기 - ZIP 라이브러리추가 - 다운받은 파일 클릭

스케치 - 라이브러리 포함하기 - SoftwareSerial 클릭


새 아두이노 실행 - 파일 - 예제 - SoftwareSerial - swsertest

---------------------------------------------------------------------------------------------------------------------------

https://github.com/ekyuho/sensors/blob/master/sds011_test.ino






--소스코드--

#include <SoftwareSerial.h>
SoftwareSerial swSer(D4, 12, false, 256);
void setup() {
Serial.begin(115200);
swSer.begin(9600);
Serial.println("\nSoftware serial test started");
}
int stat = 1;
int cnt = 0;
char buf[10];
void loop() {
while (swSer.available() > 0) {
char c;
c = swSer.read();
//Serial.print("stat="+ String(stat) +", "+ "cnt="+ String(cnt) +" ");
//Serial.print(c, HEX);
//Serial.println(" ");
if (stat == 1) {
if (c == 0xAA) stat = 2;
} else
if (stat == 2) {
if (c == 0xC0) stat =3;
else stat = 1;
} else
if (stat == 3) {
buf[cnt++] = c;
if (cnt == 7) stat = 4;
} else
if (stat == 4) {
if (c == 0xAB) {
//check checusum
stat = 1;
}
else {
Serial.println("Eh? wrong tailer");
}
cnt = 0;
int pm25 = buf[0] + 256*buf[1];
int pm10 = buf[2] + 256*buf[3];
Serial.print(String(pm25) +","+ String(pm10)+ " ");
for (int i=0; i< pm10/10; i++)
Serial.print("X");
Serial.println();
}
}
}