-
Notifications
You must be signed in to change notification settings - Fork 4
/
EcolibriumAutoconnect.ino
442 lines (371 loc) · 12.1 KB
/
EcolibriumAutoconnect.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//Arav Sharma
//Ecolibrium Remote Sensor
//Air Quality Monitor
#include <WiFiManager.h>
#include <WiFi.h>
#include "ESPAsyncWebServer.h"
#include <SensirionI2CSen5x.h>
#include <SensirionI2CScd4x.h>
#include <Wire.h>
#include <time.h>
SensirionI2CSen5x sen5x;
SensirionI2CScd4x scd4x;
AsyncWebServer server(80);
float massConcentrationPm1p0;
float massConcentrationPm2p5;
float massConcentrationPm4p0;
float massConcentrationPm10p0;
float ambientHumidity;
float ambientTemperature;
float vocIndex;
float noxIndex;
uint16_t co2;
String readScdCo2() {
uint16_t c;
float t;
float h;
scd4x.readMeasurement(c, t, h);
if (c != 0) {
co2 = c;
}
return String(co2);
}
String readPm1p0() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(massConcentrationPm1p0);
}
String readPm2p5() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(massConcentrationPm2p5);
}
String readPm4p0() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(massConcentrationPm4p0);
}
String readPm10p0() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(massConcentrationPm10p0);
}
String readAmbientHumidity() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(ambientHumidity);
}
String readAmbientTemperature() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(ambientTemperature);
}
String readVocIndex() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(vocIndex);
}
String readNoxIndex() {
sen5x.readMeasuredValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex,
noxIndex);
return String(noxIndex);
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://kit.fontawesome.com/d91e44f906.js" crossorigin="anonymous"></script>
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.dht-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>ESP32 Air Quality Server</h2>
<p>
<i class="fas fa-smog" style="color:#000000;"></i>
<span class="dht-labels">PM 1.0</span>
<span id="pm1p0">%PM1P0%</span>
<sup class="units">ug/m3</sup>
</p>
<p>
<i class="fas fa-smog" style="color:#a0a0a0;"></i>
<span class="dht-labels">PM 2.5</span>
<span id="pm2p5">%PM2P5%</span>
<sup class="units">ug/m3</sup>
</p>
<p>
<i class="fas fa-smog" style="color:#afafaf;"></i>
<span class="dht-labels">PM 4.0</span>
<span id="pm4p0">%PM4P0%</span>
<sup class="units">ug/m3</sup>
</p>
<p>
<i class="fas fa-smog" style="color:#f0f0f0;"></i>
<span class="dht-labels">PM 10.0</span>
<span id="pm10p0">%PM10P0%</span>
<sup class="units">ug/m3</sup>
</p>
<p>
<i class="fas fa-project-diagram" style="color:#631fc4;"></i>
<span class="dht-labels">Carbon Dioxide</span>
<span id="co2">%CO2%</span>
<sup class="units">ppm</sup>
</p>
<p>
<i class="fas fa-tint" style="color:#00add6;"></i>
<span class="dht-labels">Ambient Humidity</span>
<span id="ambienthumidity">%AMBIENTHUMIDITY%</span>
<sup class="units">%</sup>
</p>
<p>
<i class="fas fa-thermometer-half" style="color:#bada55;"></i>
<span class="dht-labels">Ambient Temperature</span>
<span id="ambienttemperature">%AMBIENTTEMPERATURE%</span>
<sup class="units">°C</sup>
</p>
<p>
<i class="fas fa-lungs" style="color:#ffb6c1;"></i>
<span class="dht-labels">Voc Index</span>
<span id="vocindex">%VOCINDEX%</span>
<sup class="units">-</sup>
</p>
<p>
<i class="fas fa-grin-squint-tears" style="color:#9b870c;"></i>
<span class="dht-labels">Nox Index</span>
<span id="noxindex">%NOXINDEX%</span>
<sup class="units">-</sup>
</p>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pm1p0").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pm1p0", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pm2p5").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pm2p5", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pm4p0").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pm4p0", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pm10p0").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pm10p0", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("co2").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/co2", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("ambienthumidity").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/ambienthumidity", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("ambienttemperature").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/ambienttemperature", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("vocindex").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/vocindex", true);
xhttp.send();
}, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("noxindex").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/noxindex", true);
xhttp.send();
}, 5000 ) ;
</script>
</html>)rawliteral";
// Replaces placeholder with DHT values
String processor(const String& var){
//Serial.println(var);
// if(var == "TEMPERATURE"){
// return readScdTemp();
// }
// else if(var == "HUMIDITY"){
// return readScdHum();
// }
// else if(var == "CO2"){
// return readScdCo2();
// }
if(var == "PM1P0"){
return readPm1p0();
}
else if(var == "PM2P5"){
return readPm2p5();
}
else if(var == "PM4P0"){
return readPm4p0();
}
else if(var == "PM10P0"){
return readPm10p0();
}
else if(var == "AMBIENTHUMIDITY"){
return readAmbientHumidity();
}
else if(var == "AMBIENTTEMPERATURE"){
return readAmbientTemperature();
}
else if(var == "VOCINDEX"){
return readVocIndex();
}
else if(var == "CO2") {
return readScdCo2();
}
else if(var == "NOXINDEX") {
return readNoxIndex();
}
return String();
}
void setup(){
// Connect to Wi-Fi
Serial.begin(115200);
Serial.println("To connect to the WiFi network, follow these steps:");
Serial.println("1. Open your device's WiFi settings.");
Serial.println("2. Look for a network named 'AutoConnectAP'.");
Serial.println("3. Connect to 'AutoConnectAP'. If it's password-protected, use 'password' as the password.");
Serial.println("4. Wait until your device indicates that it has connected to the network.");
Serial.println("5. Once connected, enter the following address in a web browser: http://192.168.1.218/");
// WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
bool res;
res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
//start sensors
Wire.begin();
sen5x.begin(Wire);
sen5x.deviceReset();
sen5x.setTemperatureOffsetSimple(0.00);
sen5x.startMeasurement();
delay(1000);
scd4x.begin(Wire);
scd4x.startPeriodicMeasurement();
// Route for web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on("/pm1p0", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readPm1p0().c_str());
});
server.on("/pm2p5", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readPm2p5().c_str());
});
server.on("/pm4p0", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readPm4p0().c_str());
});
server.on("/pm10p0", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readPm10p0().c_str());
});
server.on("/co2", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readScdCo2().c_str());
});
server.on("/ambienthumidity", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readAmbientHumidity().c_str());
});
server.on("/ambienttemperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readAmbientTemperature().c_str());
});
server.on("/vocindex", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readVocIndex().c_str());
});
server.on("/noxindex", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readNoxIndex().c_str());
});
// Start server
server.begin();
Serial.println("Paste into web browser:");
Serial.print("http://");
Serial.println(WiFi.localIP());
}
void loop(){
delay(2000);
}