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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <DHT.h>                      // 온습도 센서 라이브러리 사용 
#include <U8glib.h>                   // OLED 디스플레이 라이브러리 사용
 
#define DHT_PIN         4             // 온습도 센서 연결 Pin 번호
#define DHT_TYPE        DHT22         // 온습도 센서 타입
#define VOC_PIN         A0            // VOC 센서 연결 Pin 번호
#define CO2_PIN         A1            // Co2 센서 연결 Pin 번호
#define DUST_PIN        2             // 미세먼지 연결 Pin 번호
#define BTN_PIN         6             // BTN 연결 Pin 번호
#define BTN_LED_PIN     5             // BTN LED연결 Pin 번호
 
#define CHANGE_COUNT     3
 
// 객체 선언
DHT dht(DHT_PIN, DHT_TYPE);
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
 
// OLED Bitmap 아이콘(40*40) 배열
const unsigned char badIcon [] PROGMEM = {
  0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00,
  0x000x000x000x000x000xff0xff0xff0x800x070xff0xff0xff0xe00x0f0xff,
  0xff0xff0xf00x0c0x000x000x000x380x1c0x000x000x000x180x180x000x00,
  0x000x180x180x000x000x000x180x180x000x000x000x180x180x000x000x00,
  0x180x180x000x000x000x180x180x000x000x000x180x180x0e0x000x700x18,
  0x180x1f0x000xf80x180x180x1f0x000xf80x180x180x1f0x000xf80x180x18,
  0x0e0x000x700x180x180x000x000x000x180x180x000x000x000x180x180x00,
  0x000x000x180x180x000x000x000x180x180x000x3c0x000x180x180x070xff,
  0xe00x180x180x0f0xff0xf00x180x180x180x000x080x180x180x000x000x00,
  0x180x180x000x000x000x180x180x000x000x000x180x1c0x000x000x000x18,
  0x0c0x000x000x000x380x0f0x000x000x000x700x070xff0xff0xff0xe00x01,
  0xff0xff0xff0x800x000x000x000x000x000x000x000x000x000x000x000x00,
  0x000x000x000x000x000x000x000x00,
};
 
const unsigned char goodIcon [] PROGMEM = {
  0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00,
  0x000x000x000x000x000xff0xff0xff0x800x070xff0xff0xff0xe00x0f0xff,
  0xff0xff0xf00x0c0x000x000x000x380x1c0x000x000x000x180x180x000x00,
  0x000x180x180x000x000x000x180x180x000x000x000x180x180x000x000x00,
  0x180x180x000x000x000x180x180x000x000x000x180x180x0e0x000x700x18,
  0x180x1f0x000xf80x180x180x1f0x000xf80x180x180x1f0x000xf80x180x18,
  0x0e0x000x700x180x180x000x000x000x180x180x000x000x000x180x180x00,
  0x000x000x180x180x000x000x000x180x180x000x000x000x180x180x180x00,
  0x180x180x180x0f0xe70xf00x180x180x030xff0xc00x180x180x000x3c0x00,
  0x180x180x000x000x000x180x180x000x000x000x180x1c0x000x000x000x18,
  0x0c0x000x000x000x380x0f0x000x000x000x700x070xff0xff0xff0xe00x01,
  0xff0xff0xff0x800x000x000x000x000x000x000x000x000x000x000x000x00,
  0x000x000x000x000x000x000x000x00
};
 
 
float gasRo = 0;                              // Co2 센서 초기 저항 값
unsigned long dustStartTime = 0;              // 미세먼지 측정 시작 시간
unsigned long sampletimeMS = 30000;           // 미세먼지 샘플링 설정 시간
unsigned long lowPulseOccupancy = 0;          // 미세먼지 Pulse 합계
 
float humidity, temperature;                  // 온습도 값
float dustugm3;                               // 미세먼지 농도값
float vocppm;                                 // Voc 농도값
float co2ppm;                                 // Co2 농도값
bool enable = true;                           // 디바이스 On/Off 상태 값
int changeCnt;                                // 디스플레이 전환 카운트
int slideNum;                                 // 슬라이드 번호
bool pleasable = false;                       // 기준 수치 적정성
 
 
void setup() {
  Serial.begin(115200);
  dht.begin();                                // 온습도 센서 활성화
  u8g.begin();                                // OLED 모듈 활성화
 
  pinMode(BTN_PIN, INPUT);                    // 버튼 모듈 Pin 모드 설정
  pinMode(BTN_LED_PIN, OUTPUT);
  pinMode(DUST_PIN, INPUT);                   // 미세먼지 센서 Pin 모드 설정
 
  initConfig();                               // 초기 환경 설정
  clearOLED();                                // OLED 지우기
 
  Serial.println("## Start Air Quality Monitoring ##");
}
 
void loop() {
  btnEvent();                                               // 버튼 모듈 클릭 이벤트 함수 호출
 
  if (enable) {                                             // 디바이스 On 전환 시,
    measureVOC();                                                 // VOC 수치 측정
    measureCo2();                                                 // Co2 수치 측정
    measureDust();                                                // 미세먼지 수치 측정
    measureDHT();                                                 // 온도, 습도 수치 측정
 
    changeCnt--;                                                  // 전환 카운트 감소
    if (changeCnt <= 0) {                                           // 카운트 0 도달 시,
      slideNum = slideNum >= 5 ? 1 : slideNum + 1;                  // 슬라이드 번호 지정
      showOLED();                                                   // OLED 정보 출력
      changeCnt = CHANGE_COUNT;                                     // 전환 카운트 초기화
    }
    delay(1000);
  }
  delay(50);
}
 
 
// OLED 종료 함수 (기존 출력 정보 지우기)
void clearOLED() {
  u8g.firstPage();
  do {
    // Null
  } while ( u8g.nextPage() );
}
 
// OLED 출력 함수
void showOLED() {
  u8g.firstPage();
  do {
    switch (slideNum) {
      case 1:             // 슬라이드 번호 1 = 온도 정보 출력
        pleasable = temperature > 16 && temperature < 28;   // 측정 온도 기준 적정성 판단
        u8g.setFont(u8g_font_fub14);                        // 폰트 설정
        u8g.drawStr(6422"Temp");                        // 온도 정보 출력
        u8g.drawStr(6454, String(temperature).c_str());
        break;
      case 2:             // 슬라이드 번호 2 = 습도 정보 출력
        pleasable = humidity > 30 && humidity < 70;         // 측정 습도 기준 적정성 판단
        u8g.setFont(u8g_font_fub14);                        // 폰트 설정
        u8g.drawStr(6422"Humi");                        // 습도 정보 출력
        u8g.drawStr(6454, String(humidity).c_str());
        break;
      case 3:             // 슬라이드 번호 3 = 미세먼지 정보 출력
        pleasable = dustugm3 < 35;                          // 측정 미세먼지 기준 적정성 판단
        u8g.setFont(u8g_font_fub14);                        // 폰트 설정
        u8g.drawStr(6422"Dust");                        // 미세먼지 정보 출력
        u8g.drawStr(6454, String(dustugm3).c_str());
        break;
      case 4:             // 슬라이드 번호 4 = VOC 정보 출력
        pleasable = vocppm < 6;                            // 측정 VOC 기준 적정성 판단
        u8g.setFont(u8g_font_fub14);                        // 폰트 설정
        u8g.drawStr(6422"Voc");                         // VOC 정보 출력
        u8g.drawStr(6454, String(vocppm).c_str());
        break;
      case 5:             // 슬라이드 번호 5 = Co2 정보 출력
        pleasable = co2ppm < 20;                            // 측정 CO2 기준 적정성 판단
        u8g.setFont(u8g_font_fub14);                        // 폰트 설정
        u8g.drawStr(6422"Co2");                         // Co2 정보 출력
        u8g.drawStr(6454, String(co2ppm).c_str());
        break;
    }
    u8g.drawBitmapP(812540, pleasable ? goodIcon : badIcon);      // 기준 수치 적정성에 따른 상태 아이콘 출력
  } while ( u8g.nextPage() );
}
 
// 버튼 클릭 이벤트 함수
void btnEvent() {
  if (digitalRead(BTN_PIN)) {                   // 버튼 클릭 시,
    while (digitalRead(BTN_PIN)) {
      delay(100);
    }
    enable = !enable;                           // 동작 활성화 상태 전환
    if (enable) {                                   // 동작 활성화 시,
      initConfig();                                     // 초기 환경 설정
    } else {                                        // 동작 비활성화 시,
      clearOLED();                                      // OLED 출력 종료
    }
    digitalWrite(BTN_LED_PIN, enable);          // 동작 상태에 따라 버튼 LED 상태 설정
  }
}
 
 
// 초기 환경 설정 함수
void initConfig() {
  dustStartTime = millis();                   // 미세먼지 측정 시작 시간 설정
  changeCnt = CHANGE_COUNT;                   // 전환 카운트 갱신(초기화) 
  slideNum = 0;                               // 슬라이드 시작 번호 초기화
  lowPulseOccupancy = 0;                      // 미세먼지 Pulse 합계 초기화
}
 
 
// 온습도 측정 함수
void measureDHT() {
  humidity = dht.readHumidity();                  // 습도 측정
  temperature = dht.readTemperature();            // 온도 측정
 
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("DHT sensor error!");
    return;
  }
  Serial.print("DHT : (Temp) ");
  Serial.print(temperature);
  Serial.print("\t (Humi) ");
  Serial.println(humidity);
}
 
 
// 미세먼지 측정 함수
void measureDust() {
  // 미세먼지 센서(Grove-Dust Sensor) 측정 참조 - https://wiki.seeedstudio.com/Grove-Dust_Sensor/
  lowPulseOccupancy += pulseIn(DUST_PIN, LOW);
  if (millis() - dustStartTime >= sampletimeMS) {
    float ratio = lowPulseOccupancy / (sampletimeMS * 10.0);
    float concentration = 1.1 * pow(ratio, 3- 3.8 * pow(ratio, 2+ 520 * ratio + 0.62;
    dustugm3 = concentration * 100 / 1300;
    Serial.print("Dust(ug/m3) : ");
    Serial.println(dustugm3);
    lowPulseOccupancy = 0;
    dustStartTime = millis();
  }
}
 
 
// CO2 측정 함수
void measureCo2() {
  // 가스 센서 측정 참조 - https://thestempedia.com/tutorials/interfacing-mq-2-gas-sensor-with-evive/
  float vOut = analogRead(CO2_PIN) * 5.0 / 1023.0;
  float gasRs = (5.0 - vOut) / vOut;
 
  if (gasRo == 0) {
    gasRo = gasRs / 9.9;
    Serial.print("Gas Ro : ");
    Serial.print(gasRo);
    Serial.println(" kohm");
  } else {
    float ratio = gasRs / gasRo;
    co2ppm = pow(10, (log(ratio) - 1.51/ -0.34);
    Serial.print("(Co2) Vout : ");
    Serial.print(vOut);
    Serial.print("\t ppm : ");
    Serial.println(co2ppm);
  }
}
 
 
// VOC 측정 함수
void measureVOC() {
  // VOC 센서 측정 참조 - https://datasheetspdf.com/pdf/1418387/Ogam/GSBT11/1
  float vOut = analogRead(VOC_PIN) * 5.0 / 1023.0;
  vocppm = pow(10-0.867 + 1.274 * vOut);
  Serial.print("(Formaldehyde) Vout : ");
  Serial.print(vOut);
  Serial.print("\t ppm : ");
  Serial.println(vocppm);
}
cs

 

+ Recent posts