-
Notifications
You must be signed in to change notification settings - Fork 0
/
Firebase_with_esp8266_ppd42ns_dht11_LED.ino
208 lines (163 loc) · 4.32 KB
/
Firebase_with_esp8266_ppd42ns_dht11_LED.ino
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
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <FirebaseArduino.h>
#include <DHT11.h>
// Set these to run example.
#define FIREBASE_HOST "mise-3795b.firebaseio.com"
#define FIREBASE_AUTH "PD2N1V1zCi9FdfefMYVNB49R0YdMogQx74a8tB3h"
#define WIFI_SSID "KTEgg145"
#define WIFI_PASSWORD "kaii1357!!i"
#define DHT11_Delay 5000
//set ESP8266
ESP8266WiFiMulti wifiMulti;
//전역변수 선언
//ppd42ns
int ppd42ns_pin = 5;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 5000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float pcsPerCF = 0;
float ugm3 = 0;
float ratio = 0;
float concentration = 0;
//PM 평균
int count = 0;
float total = 0;
float average = 0;
//DHT11
int dht11_pin = 4;
DHT11 dht11(dht11_pin);
//LED
int B_pin = 13; //Connect Blue led to Digital pin 9
int R_pin = 15;//Connect Red led to Digital pin 10
int G_pin = 2;//Connect Green led to Digital pin 11
int B = 0;
int R = 0;
int G = 0;
void setup() {
Serial.begin(115200);
//ppd42ns Setting
pinMode(ppd42ns_pin, INPUT);
starttime = millis();
//LED Setting
pinMode(B_pin, OUTPUT);
pinMode(R_pin, OUTPUT);
pinMode(G_pin, OUTPUT);
// connect to wifi.
wifiMulti.addAP("KTEgg145", "kaii1357!!i"); // add Wi-Fi networks you want to connect to
wifiMulti.addAP("iPhone", "kaii1357");
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(1000);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.setFloat("pm10", 0);
}
void loop() {
ppd42ns();
CallDHT11();
ledAction();
}
void ledAction() {
B = Firebase.getInt("led_B");
R = Firebase.getInt("led_R");
G = Firebase.getInt("led_G");
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
digitalWrite(B_pin, B);
digitalWrite(R_pin, R);
digitalWrite(G_pin, G);
/*
analogWrite(B_pin, B);
analogWrite(R_pin, R);
analogWrite(G_pin, G);
*/
}
void CallDHT11() {
int err;
float temp, humi;
if ((err = dht11.read(humi, temp)) == 0)
{
Firebase.setFloat("temperature", temp);
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
Firebase.setFloat("humidity", humi);
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
Serial.print("temperature:");
Serial.print(temp);
Serial.print(" humidity:");
Serial.print(humi);
Serial.println();
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err);
Serial.println();
}
delay(DHT11_Delay); //delay for reread
}
void ppd42ns() {
duration = pulseIn(ppd42ns_pin, LOW);
lowpulseoccupancy = lowpulseoccupancy + duration;
if ((millis() - starttime) > sampletime_ms) //if the sampel time == 30s
{
ratio = lowpulseoccupancy / (sampletime_ms * 10.0); // Integer percentage 0=>100
concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curve
pcsPerCF = concentration * 100;
ugm3 = pcsPerCF / 7000;
averagePM10(ugm3);
Serial.print("PM10 = ");
Serial.print(ugm3);
Serial.println("\n");
lowpulseoccupancy = 0;
starttime = millis();
}
}
void averagePM10(float data) {
count += 1;
total += data;
Serial.print("count is ");
Serial.print(count);
Serial.println("");
Serial.print("total is ");
Serial.print(total);
Serial.println("");
if (count >= 10) {
average = total / count;
Serial.print("average = ");
Serial.print(average);
Serial.println("");
Firebase.setFloat("pm10", average);
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
count = 0;
total = 0;
}
}