forked from anaIyzer/EnvDataLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.h
285 lines (238 loc) · 5.1 KB
/
Core.h
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <time.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <SoftwareSerial.h>
#define FIREBASE_HOST "your-project.firebaseio.com"
#define FIREBASE_AUTH "key" //database password
#define WIFI_SSID "U+village"
#define WIFI_PASSWORD "lguplus100"
#define TIMEZONE 9
#define DB_PATH "/ENV-DATA-LOG" // Database path
enum class WorkStatus
{
Success,
FailedToSensor,
FailedToFirebase,
Unknown
};
class Core
{
private:
DHT dht;
float humidity;
float temperature;
long pmcf10;
long pmcf25;
long pmcf100;
long pmat10;
long pmat25;
long pmat100;
SoftwareSerial mySerial;
public:
Core()
: dht()
, humidity(0)
, temperature(0)
, pmcf10(0)
, pmcf25(0)
, pmcf100(0)
, pmat10(0)
, pmat25(0)
, pmat100(0)
, mySerial(D4, D5)
{
}
void setup()
{
Serial.begin(115200);
Serial.println("-----Start-----");
// WiFi
WiFi.mode(WIFI_STA);
Serial.print("[*] connecting to ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("\n[*] local IP: ");
Serial.println(WiFi.localIP());
// Time
configTime(TIMEZONE * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.println("[*] waiting for time");
while (!time(nullptr))
{
Serial.print(".");
delay(1000);
}
delay(1500);
Serial.println("[*] now: " + getNow());
// Firebases
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Serial.println("\n[*] Firebase begin.");
//DHT11
dht.setup(D6);
// PMS3003
mySerial.begin(9600);
}
DHT &getDHT()
{
return dht;
}
SoftwareSerial &getSerial()
{
return mySerial;
}
String getNow()
{
time_t t = time(nullptr);
struct tm *nt = localtime(&t);
String tmp = "";
tmp += String(nt->tm_year + 1900);
tmp += "-";
tmp += String(nt->tm_mon + 1);
tmp += "-";
tmp += String(nt->tm_mday);
tmp += " ";
tmp += String(nt->tm_hour);
tmp += ":";
tmp += String(nt->tm_min);
tmp += ":";
tmp += String(nt->tm_sec);
return tmp;
}
WorkStatus updateAll()
{
WorkStatus retn;
retn = updateDHT11();
if (retn == WorkStatus::Unknown || retn == WorkStatus::FailedToSensor)
{
Serial.println("[-] failed to DHT11 sensor recognition.");
return retn;
}
delay(1000);
retn = updatePMS3003();
if (retn == WorkStatus::Unknown || retn == WorkStatus::FailedToSensor)
{
Serial.println("[-] failed to PMS3003 sensor recognition.");
return retn;
}
StaticJsonBuffer<200> jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["humidity"] = humidity;
root["temperature_C"] = temperature;
root["PM1_0"] = pmat10;
root["PM2_5"] = pmat25;
root["PM10"] = pmat100;
root["time"] = getNow();
String pathName = Firebase.push(DB_PATH, root);
// handle error
if (Firebase.failed())
{
Serial.print("[-] failed to push.");
if (Firebase.error())
{
Serial.print(" error: ");
Serial.print(Firebase.error());
}
Serial.println();
return WorkStatus::FailedToFirebase;
}
Serial.print("[+] ");
Serial.print(getNow());
Serial.print("\t-\tpushed: ");
Serial.print(DB_PATH);
Serial.println("/");
}
WorkStatus updateDHT11()
{
delay(dht.getMinimumSamplingPeriod() + 500);
humidity = dht.getHumidity();
temperature = dht.getTemperature();
String dhtStatus = dht.getStatusString();
if (!dhtStatus.equals("OK"))
{
return WorkStatus::FailedToSensor;
}
return WorkStatus::Success;
}
WorkStatus updatePMS3003()
{
WorkStatus retn = WorkStatus::Unknown;
int count = 0;
unsigned char c;
unsigned char high;
while (mySerial.available())
{
c = mySerial.read();
if((count == 0 && c != 0x42) || (count == 1 && c != 0x4d))
{
//Serial.println("[-] check failed");
retn = WorkStatus::FailedToSensor;
break;
}
if (count > 15)
{
//Serial.println("[+] complete");
retn = WorkStatus::Success;
break;
}
else if (count == 4 || count == 6 || count == 8 || count == 10 || count == 12 || count == 14)
{
high = c;
}
else if (count == 5)
{
pmcf10 = 256 * high + c;
//Serial.print("CF=1, PM1.0=");
//Serial.print(pmcf10);
//Serial.println(" ug/m3");
}
else if(count == 7)
{
pmcf25 = 256 * high + c;
//Serial.print("CF=1, PM2.5=");
//Serial.print(pmcf25);
//Serial.println(" ug/m3");
}
else if(count == 9)
{
pmcf100 = 256 * high + c;
//Serial.print("CF=1, PM10=");
//Serial.print(pmcf100);
//Serial.println(" ug/m3");
}
else if(count == 11)
{
pmat10 = 256 * high + c;
//.print("atmosphere, PM1.0=");
//Serial.print(pmat10);
//Serial.println(" ug/m3");
}
else if(count == 13)
{
pmat25 = 256 * high + c;
//Serial.print("atmosphere, PM2.5=");
//Serial.print(pmat25);
//Serial.println(" ug/m3");
}
else if(count == 15)
{
pmat100 = 256 * high + c;
//Serial.print("atmosphere, PM10=");
//Serial.print(pmat100);
//Serial.println(" ug/m3");
}
count++;
}
while (mySerial.available())
{
mySerial.read();
}
// delay
delay(500);
return retn;
}
};