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
#include <PHYSIs_Master.h>
#include <DHT.h>                      // 온습도 센서 라이브러리 사용 
#include <Adafruit_NeoPixel.h>
 
#define DUST_PIN        2             // 미세먼지 연결 Pin 번호 
#define DHT_PIN         4             // 온습도 센서 연결 Pin 번호
#define DHT_TYPE        DHT22         // 온습도 센서 타입
#define LED_PIN         5
 
 
PHYSIs_WiFi physisWiFi;
DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, LED_PIN, NEO_GRB + NEO_KHZ800);
 
const String WIFI_SSID     = "your wifi";             // WiFi 명
const String WIFI_PWD      = "your password";         // WiFi 비밀번호
const String SERIAL_NUMBER = "your serial number";    // PHYSIs KIT 시리얼번호
const String PUB_TOPIC     = "Sensing";               // Publish Topic
 
unsigned long sensingTime = 0;                // 센서 측정 시간
unsigned long sensingInterval = 2000;         // 센서 측정 간격
unsigned long dustStartTime = 0;              // 미세먼지 측정 시작 시간
unsigned long sampletimeMS = 10000;           // 미세먼지 샘플링 설정 시간
unsigned long lowPulseOccupancy = 0;          // 미세먼지 Pulse 합계
int humidity, temperature;                    // 온습도 값
int dustugm3;                                 // 미세먼지 농도값
uint32_t ledColor;
 
void setup() {
  Serial.begin(115200);
  dht.begin();                                // 온습도 센서 활성화
  strip.begin();
  
  physisWiFi.enable();                             // WiFi 모듈 활성화
 
  physisWiFi.connectWiFi(WIFI_SSID, WIFI_PWD);     // 지정 WiFi에 대한 연결 요청
  Serial.print("# WiFi Connecting..");
  delay(1000);
  while (!physisWiFi.isWiFiStatus()) {             // WiFi가 연결될때 까지 반복해서 연결상태를 확인
    Serial.print(".");
    delay(1000);
  }
  Serial.println(F("Connected..."));
 
  Serial.print("# MQTT Connecting..");
  if (physisWiFi.connectMQTT()) {                  // PHYSIs 플랫폼의 MQTT Broker와 연결
    Serial.println("Success...");
  } else {
    Serial.print("Fail...");
  }
 
  strip.setBrightness(50);
  strip.show();                                   // LED 초기화
}
 
void loop() {
  if (millis() - sensingTime >= sensingInterval) {        // 센서 측정 시간이 측정 간격을 초과하였을 경우,
    measureDust();                                            // 미세먼지 측정
    measureDHT();                                             // 온습도 측정
    setDustLedColor();                                        // 미세먼지 상태 LED 색상 설정
    sendSensingData();                                        // 미세먼지, 온습도 정보 전송
    sensingTime = millis();                                   // 센서 측정 시간 재설정
  }
  
  showLedEffect();                                      // 미세먼지 상태 LED 출력
}
 
 
// 미세먼지 농도에 따른 LED 색상 설정 함수
void setDustLedColor(){
  if(dustugm3 > 35){
    ledColor = strip.Color(25500);
  }else if (dustugm3 < 15){    
    ledColor = strip.Color(00255);
  }else{    
    ledColor = strip.Color(02550);
  }
}
 
// LED 효과 출력 함수 
void showLedEffect() {
  for (int i = 0; i < 3; i++) {                            
    for (int j = 0; j < strip.numPixels(); j = j + 3) {    
      strip.setPixelColor(j + i, ledColor);                // LED 색상 출력
    }
    strip.show();
    delay(100);
    for (int j = 0; j < strip.numPixels(); j = j + 3) {
      strip.setPixelColor(j + i, 0);                       // 이전 LED 색상 초기화
    }
  }
}
 
 
// 센싱(미세먼지, 온/습도) 정보 전송 함수
void sendSensingData() {
  String data = String(dustugm3) + "," + String(temperature) + "," + String(humidity);
  physisWiFi.publish(SERIAL_NUMBER, PUB_TOPIC, data);       // MQTT 메시지 전송
}
 
// 온습도 측정 함수
void measureDHT() {
  humidity = (int)dht.readHumidity();                  // 습도 측정
  temperature = (int)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 = (int)(concentration * 100 / 1300);
    Serial.print("Dust(ug/m3) : ");
    Serial.println(dustugm3);
    lowPulseOccupancy = 0;
    dustStartTime = millis();
  }
}
cs

PHYSIs_Dust_Monitor.aia
0.12MB

+ Recent posts