-
Notifications
You must be signed in to change notification settings - Fork 0
/
Health_Care.ino
247 lines (205 loc) · 7.53 KB
/
Health_Care.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
//This example shows how to set stream event callback functions.
//Two events are available from Firebase's Real Time database HTTP stream connection, dataAvailable and streamTimeout.
//Required HTTPClientESP32Ex library to be installed https://github.com/mobizt/HTTPClientESP32Ex
#include <WiFi.h>
#include "FirebaseESP32.h"
#include "DHT.h"
#define FIREBASE_HOST "healthcare-1d270.firebaseio.com" //Do not include https:// in FIREBASE_HOST
#define FIREBASE_AUTH "**********************"
#define WIFI_SSID "tedata"
#define WIFI_PASSWORD "0em04133"
String generalPath="/Garbeg";
String json = "";
String path = "/Sensors";
//******Humiday and temprature sensor*******
#define DHTTYPE DHT11 // DHT 11
// DHT Sensor
const int DHTPin = 15;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
// Temporary variables for temperature sensor
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];
//**************PIR motion *****************
//PIR motion
int ledPin = 26;
int pirPin = 27;
int val = 0;
//*********************Define Firebase Data objects ***********&**********************
FirebaseData firebaseData1;
unsigned long sendDataPrevMillis1;
uint16_t count1;
void streamCallback1(StreamData data)
{
Serial.println("> Stream Data1 available...");
Serial.println("> STREAM PATH: " + data.streamPath());
Serial.println("> PATH: " + data.dataPath());
Serial.println("> TYPE: " + data.dataType());
Serial.print("> VALUE: ");
if (data.dataType() == "int")
Serial.println(data.intData());
else if (data.dataType() == "float")
Serial.println(data.floatData());
else if (data.dataType() == "string")
Serial.println(data.stringData());
else if (data.dataType() == "json")
Serial.println(data.jsonData());
Serial.println();
}
void streamTimeoutCallback1()
{
Serial.println();
Serial.println("> Stream 1 timeout, resume streaming...");
Serial.println();
}
//****************************************************************
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
//PIR motion**********************
//********************************
//Humiday and temprature sensor >>> initialize the DHT sensor
dht.begin();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
Serial.println("------------------------------------");
Serial.println("> Begin stream 1...");
if (!Firebase.beginStream(firebaseData1, path))
{
Serial.println("> FAILED");
Serial.println("> REASON: " + firebaseData1.errorReason());
Serial.println();
}
else
{
Serial.println("> PASSED");
Serial.println("------------------------------------");
Serial.println();
}
Firebase.setStreamCallback(firebaseData1, streamCallback1, streamTimeoutCallback1);
//PIR motion
pinMode (ledPin,OUTPUT);
pinMode (pirPin, INPUT);
}
void calcTempraturAndHumidity(){
//read humidity
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
strcpy(celsiusTemp,"Failed");
strcpy(fahrenheitTemp, "Failed");
strcpy(humidityTemp, "Failed");
}
else{
// Computes temperature values in Celsius + Fahrenheit and Humidity
float hic = dht.computeHeatIndex(t, h, false);
/*dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);>> convert a double (or float) to a string
FLOAT in my case would be temperature
WIDTH is the number of characters to use in the output
PRECISION is the number of characters after the decimal point
BUFFER is where the write the characters to
*/
dtostrf(hic, 6, 2, celsiusTemp);
float hif = dht.computeHeatIndex(f, h);
dtostrf(hif, 6, 2, fahrenheitTemp);
dtostrf(h, 6, 2, humidityTemp);
// You can delete the following Serial.print's, it's just for debugging purposes
Serial.print("Humidity: ");
Serial.print(h);
Serial.println();
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F");
Serial.println();
}
//Check temperature Danger
if(atoi(celsiusTemp)>=37){
json = "{ \"Status\" : \"High temperature\"}";
updateNode(generalPath,json);
}
else if(atoi(celsiusTemp)>40){
json = "{ \"Status\" : \"Very high temperature\"}";
updateNode(generalPath,json);
}
}
//update data on the server
void updateNode(String PathStr,String JsonStr){
//JSON is derived from JavaScript object notation
Serial.println("------------------------------------");
Serial.println("> Update Data 1...");
if (Firebase.updateNode(firebaseData1, PathStr, JsonStr))
{
Serial.println("> PASSED");
Serial.println("> PATH: " + firebaseData1.dataPath());
Serial.println("> TYPE: " + firebaseData1.dataType());
Serial.print("> VALUE: ");
if (firebaseData1.dataType() == "int")
Serial.println(firebaseData1.intData());
else if (firebaseData1.dataType() == "float")
Serial.println(firebaseData1.floatData());
else if (firebaseData1.dataType() == "string")
Serial.println(firebaseData1.stringData());
else if (firebaseData1.dataType() == "json")
Serial.println(firebaseData1.jsonData());
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("> FAILED");
Serial.println("> REASON: " + firebaseData1.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
void loop(){
if(millis() - sendDataPrevMillis1 > 16000){
sendDataPrevMillis1 = millis();
//Temperature and humidity sensor
generalPath=path + "/TemperatureHumidity"+"/Temperature"+"/";
calcTempraturAndHumidity();
json = "{ \"Celsius\" : " + String(celsiusTemp) + "}";
updateNode(generalPath,json);
generalPath=path + "/TemperatureHumidity"+"/Temperature"+"/";
json = "{ \"Fahrenheit\" : " + String(fahrenheitTemp) + "}";
updateNode(generalPath,json);
generalPath=path + "/TemperatureHumidity"+"/Humidity"+"/";
json = "{ \"Humidity\" : " + String(humidityTemp) + "}";
updateNode(generalPath,json);
}
//PIR motion
val = digitalRead(pirPin);
digitalWrite(ledPin,val);
if (val == 1){
generalPath=path + "/PIR_motion"+"/";
json = "{ \"Status\" : " + String(val) + "}";
updateNode(generalPath,json);
digitalWrite(ledPin,LOW);
delay(2000);
val=0;
generalPath=path + "/PIR_motion"+"/";
json = "{ \"Status\" : " + String(val) + "}";
updateNode(generalPath,json);
}
}