-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIO.ino
584 lines (506 loc) · 14.9 KB
/
AIO.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <ESPmDNS.h>
#include <FirebaseESP32.h>
#include <WiFi.h>
#include "DHT.h"
#include "MAX30105.h"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
MAX30105 particleSensor;
//#define FIREBASE_HOST "go-mine-85a32-default-rtdb.firebaseio.com"
//#define FIREBASE_Authorization_key "0ZHoDg8x3hLdjG2pyvb3ntaUStuLV1OPA7bWazOA"
#define FIREBASE_Authorization_key "euDB3Coml1tifCgWCMXayU5zt2DhZSIHqMxNoeVI"
#define FIREBASE_HOST "database-ands-default-rtdb.firebaseio.com"
const char* ssid = "DaGGeR";
const char* password = "12345678";
char auth[] = "wdzSxn6RwBg1NoPIlTVtua4nNqJC5_v6";
#define DHTTYPE DHT11
#define DHTPIN 27
#define ONE_WIRE_BUS 4
#define SOUND_SPEED 0.034
#define MQ2 35
#define GREEN 16
#define RED 17
int sensorValue = 0;
int ir=13;
int led=14;
FirebaseData firebaseData;
FirebaseJson json;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
String temperatureF = "";
String temperatureC = "";
String rtemperature = "";
String rhumidity = "";
String distance = "";
String gas = "";
String bpm = "";
String irs = "";
unsigned long lastTime = 0;
unsigned long timerDelay = 10;
DHT dht(DHTPIN, DHTTYPE);
const int trigPin = 5;
const int echoPin = 18;
long duration;
AsyncWebServer server(80);
String readDHTTemperature() {
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return "00";
}
else {
Serial.print("Mine Temperature: ");
Serial.println(t);
Firebase.setFloat(firebaseData, "/ESP32_APP/TEMPERATURE", t);
// delay(200);
if (t > 27)
{
Blynk.notify("Alert!! Temperature Rising!!");
}
}
return String(t);
}
String readDHTHumidity() {
float h = dht.readHumidity();
if (isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return "00";
}
else {
Serial.print("Mine Humidity: ");
Serial.println(h);
Firebase.setFloat(firebaseData, "/ESP32_APP/HUMIDITY", h);
// delay(200);
if (h > 65)
{
Blynk.notify("Alert!! Humidity Rising!!");
}
}
return String(h);
}
String readDSTemperatureC() {
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
if(tempC == -127.00) {
Serial.println("Failed to read from DS18B20 sensor");
return "00";
} else {
Serial.print("Body Temperature Celsius: ");
Serial.println(tempC);
Firebase.setFloat(firebaseData, "/ESP32_APP/BODY TEMPERATURE in C", tempC);
// delay(200);
if (tempC > 27)
{
Blynk.notify("Alert!! Harmful Body Temperature!!");
}
}
return String(tempC);
}
String readDSTemperatureF() {
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
float tempF = sensors.getTempFByIndex(0);
if(int(tempF) == -196){
Serial.println("Failed to read from DS18B20 sensor");
return "00";
} else {
Serial.print("Body Temperature Fahrenheit: ");
Serial.println(tempF);
Firebase.setFloat(firebaseData, "/ESP32_APP/BODY TEMPERATURE in F", tempF);
// delay(200);
if (tempF > 83)
{
Blynk.notify("Alert!! Harmful Body Temperature!!");
}
}
return String(tempF);
}
String readDistance(){
float distanceCm;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * SOUND_SPEED/2;
if (distanceCm < 1)
{
Serial.println("Ultrasonic Sensor not detected");
return "00";
}
else
{
Serial.print("Water Level: ");
Serial.println(distanceCm);
Firebase.setFloat(firebaseData, "/ESP32_APP/WATER LEVEL in cm", distanceCm);
// delay(200);
if (distanceCm < 5)
{
Blynk.notify("Water is Rising Rapidly !!");
}
}
return String(distanceCm);
}
String sendUptime(){
float sensorValue;
sensorValue = analogRead(MQ2);
Blynk.virtualWrite(V1, sensorValue);
Serial.print("Gas Level: ");
Serial.println(sensorValue);
Firebase.setFloat(firebaseData, "/ESP32_APP/GAS LEVEL", sensorValue);
// delay(200);
if (sensorValue > 1000)
{
Blynk.notify("Gas Detected!");
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
}
else
{
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
}
return String(sensorValue);
}
String readBPM() {
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
// while (1);
return "00";
}
else
{
byte ledBrightness = 0x1F; //Options: 0=Off to 255=50mA
byte sampleAverage = 8; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 3; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
int sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
boolean state = false;
{
float hr;
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange);
hr=(particleSensor.getIR()/1000);
Serial.print("Heart Rate: ");
Serial.println(hr); //Send raw data to plotter
Firebase.setFloat(firebaseData, "/ESP32_APP/HEART RATE", hr);
// delay(200);
if(hr>80)
{
Blynk.notify("Alert!! Abnormal Heartbeat!!");
}
return String(hr);
}
}
}
String readIRS() {
{
int irvalue = HIGH;
irvalue = digitalRead(ir);
//int irvalue = digitalRead(ir);
if(irvalue == LOW)
{
const char* i ="WORN";
Serial.print("Helmet Status: ");
Serial.println(i);
Firebase.setString(firebaseData, "/ESP32_APP/HELMET STATUS", i);
digitalWrite(led, LOW);
return String(i);
}
else
{
const char* i ="NOT WORN";
Serial.print("Helmet Status: ");
Serial.println(i);
Firebase.setString(firebaseData, "/ESP32_APP/HELMET STATUS", i);
digitalWrite(led, HIGH);
Blynk.notify("Alert!! Helmet Opened");
return String(i);
}
}
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
}
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
h2 {padding-left: 50px; }
p {padding-left: 50px; }
.dht-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>WORKER HEALTH ANALYSIS<HR></h2>
<p>
<i class="fas fa-temperature-high" style="color:#059e8a;"></i>
<span class="dht-labels">Mine Temperature<br></span>
<span id="temperature">%TEMPERATURE%</span>
<sup class="units">°C</br></sup>
</p>
<HR>
<p>
<i class="fas fa-tint" style="color:#00add6;"></i>
<span class="dht-labels">Mine Humidity<br></span>
<span id="humidity">%HUMIDITY%</span>
<sup class="units">%</br></sup>
</p>
<HR>
<p>
<i class="fas fa-thermometer-half" style="color:#98f5e8;"></i>
<span class="dht-labels">Body Temperature<br></span>
<span id="temperaturec">%TEMPERATUREC%</span>
<sup class="units">°C</br></sup>
</p>
<HR>
<p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="dht-labels">Body Temperature<br></span>
<span id="temperaturef">%TEMPERATUREF%</span>
<sup class="units">°F</br></sup>
</p>
<HR>
<p>
<i class="fas fa-road" style="color:#0c42ae;"></i>
<span class="dht-labels">Water Level<br></span>
<span id="Distance">%DISTANCE%</span>
<sup class="units">cm</br></sup>
</p>
<HR>
<p>
<i class="fas fa-gas-pump" style="color: #059e8a;"></i>
<span class="dht-labels">Gas Detection<br></span>
<span id="gas">%GAS%</span>
<sup class="units">ppm</br></sup>
</p>
<HR>
<p>
<i class="fas fa-heartbeat" style="color:rgb(245, 16, 16);"></i>
<span class="dht-labels">Heart Rate<br></span>
<span id="bpm">%BPM%</span>
<sup class="units">bpm</br></sup>
</p>
<HR>
<p>
<i class="fas fa-heartbeat" style="color:rgb(245, 16, 16);"></i>
<span class="dht-labels">Helmet Status<br></span>
<span id="irs">%IRS%</span>
<sup class="units">**</br></sup>
</p>
<HR>
<p>
<i class="fa fa-percent" style="color:rgb(57, 191, 235);"></i>
<span class="dht-labels">SPO2<br></span>
<span id="Percentage">--</span>
<sup class="units">%</br></sup>
</p>
<hr>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperature").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperature", true);
xhttp.send();
}, 10000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("humidity").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/humidity", true);
xhttp.send();
}, 10000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperaturec").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperaturec", true);
xhttp.send();
}, 10000) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperaturef").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperaturef", true);
xhttp.send();
}, 10000) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("Distance").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/Distance", true);
xhttp.send();
}, 10000) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("gas").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/gas", true);
xhttp.send();
}, 10000) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("bpm").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/bpm", true);
xhttp.send();
}, 10000) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("irs").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/irs", true);
xhttp.send();
}, 10000) ;
</script>
</html>)rawliteral";
String processor(const String& var){
Serial.println(var);
if(var == "TEMPERATURE"){
return rtemperature;
}
else if(var == "HUMIDITY"){
return rhumidity;
}
else if(var == "TEMPERATUREC"){
return temperatureC;
}
else if(var == "TEMPERATUREF"){
return temperatureF;
}
else if(var == "DISTANCE"){
return distance;
}
else if(var == "GAS"){
return gas;
}
else if(var == "BPM"){
return bpm;
}
else if(var == "IRS"){
return irs;
}
return String();
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
dht.begin();
sensors.begin();
Serial.println();
Firebase.begin(FIREBASE_HOST,FIREBASE_Authorization_key);
// Connect to Wi-Fi
WiFi.begin (ssid, password);
Blynk.begin(auth, ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
if(!MDNS.begin("esp32")) {
Serial.println("Error starting mDNS");
return;
}
Serial.println(WiFi.localIP());
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(MQ2, INPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(ir, INPUT);
pinMode(led, OUTPUT);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", rtemperature.c_str());
});
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", rhumidity.c_str());
});
server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", temperatureC.c_str());
});
server.on("/temperaturef", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", temperatureF.c_str());
});
server.on("/distanceCm", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", distance.c_str());
});
server.on("/gas", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", gas.c_str());
});
server.on("/bpm", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", bpm.c_str());
});
server.on("/irs", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", irs.c_str());
});
server.begin();
}
void loop(){
Blynk.run();
timer.run();
if ((millis() - lastTime) > timerDelay) {
temperatureC = readDSTemperatureC();
temperatureF = readDSTemperatureF();
distance = readDistance();
rhumidity = readDHTHumidity();
rtemperature = readDHTTemperature();
gas = sendUptime();
bpm = readBPM();
irs = readIRS();
lastTime = millis();
}
}