-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht11__fireBase_Anonymous_auth_.ino
386 lines (299 loc) · 10 KB
/
dht11__fireBase_Anonymous_auth_.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
/** This example will show how to authenticate as a anonymous user.
*
* You need to enable Anonymous provider.
* In Firebase console, select Authentication, select Sign-in method tab,
* under the Sign-in providers list, enable Anonymous provider.
*
* Warning: this example will create a new anonymous user with
* different UID every time you run this example.
*/
#include <dhtnew.h> // humidity and temperature sensor library
#if defined(ESP32)
#include <WiFi.h> //wifi library
#include <FirebaseESP32.h> //firebase for ESP32 library
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#endif
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
/* 1. Define the WiFi credentials */
#define WIFI_SSID "xxxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxx"
#define API_KEY "xxxxxxxxxxxxx"
/* 3. If work with RTDB, define the RTDB URL */
#define DATABASE_URL "xxxxxxxxxxxxxxxxx" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
#define DEVICE_ID "dev-1"
#define LOCATION "Living Room"
// Led 1 Pins
#define Led_1_Red 25
#define Led_1_Green 26
#define Led_1_Blue 27
// Led 2 Pins
#define Led_2_Red 2
#define Led_2_Green 4
#define Led_2_Blue 5
/* 4. Define the Firebase Data object */
FirebaseData fbdo;
/* 5. Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;
/* 6. Define the FirebaseConfig data for config data */
FirebaseConfig config;
// Json objects containing data
FirebaseJson Tempreature_json;
FirebaseJson Humidity_json;
// Sensor data
float humidity=0.0;
float temperature=0.0;
unsigned long dataMillis = 0;
bool signupOK = false;
// thresholds for temperature & humidity
//these values represent indoor human confort range
float hum_min_threshhold=30.0;
float hum_max_threshhold=60.0;
float temp_min_threshhold=20.0;
float temp_max_threshhold=30.0;
//notifications concerning sensor values
String SensorState = "OK";
String Warning = "NONE";
int Sensor_Error_Code=0;
// declaring string variables containing the data paths
String path="";//base path
String temp_path="";
String hum_path="";
//creating sensor object attached to pin 15
DHTNEW Sensor(15);
void setup()
{
//Starting serial communication with our ESP32 board
Serial.begin(115200);
//initializations
Led_Init();
Sensor_init();
WiFi_init();
FireBase_init();
Json_init();
Serial.println(path);
Serial.println(temp_path);
Serial.println(hum_path);
Serial.println(String(millis()));
}
/**********************************************************************************************************/
void loop()
{
if (millis() - dataMillis > 2000 && signupOK && Firebase.ready())
{
dataMillis = millis();
Update_Sensor_readings();
Update_data();
Serial.println("updated reached");
}
}
/**************************************************************************************************************/
void WiFi_init()
{
//attempting to connect with the wifi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
//while not connected
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
//Print the local IP adress if connection is successfully established
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
}
/******************************************************************************************************************/
void FireBase_init(){
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
/* Assign the API key (required) */
config.api_key = API_KEY;
/* Assign the RTDB URL */
config.database_url = DATABASE_URL;
Firebase.reconnectWiFi(true);
Serial.print("Sign up new user... ");
/* Sign up */
if (Firebase.signUp(&config, &auth, "", ""))
{
Serial.println("ok");
signupOK = true;
}
else
Serial.printf("%s\n", config.signer.signupError.message.c_str());
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
Firebase.begin(&config, &auth);
path = auth.token.uid.c_str(); //<- user uid
path+="/";
// determining the paths
path+=LOCATION;
temp_path=path+"/temperature";
hum_path=path+"/humidity";
}
/****************************************************************************************************************/
void Sensor_init()
{
Sensor.setSuppressError(true);// avoiding spikes and error values for more detailed info check this
}
/*********************************************************************************************************************/
void Led_Init()
{
pinMode(Led_1_Red,OUTPUT);
pinMode(Led_1_Green,OUTPUT);
pinMode(Led_1_Blue,OUTPUT);
pinMode(Led_2_Red,OUTPUT);
pinMode(Led_2_Green,OUTPUT);
pinMode(Led_2_Blue,OUTPUT);
}
/*******************************************************************************************************************/
void Update_Sensor_readings()
{
// if (millis() - Sensor.lastRead() > 2000){
Sensor_Error_Code = Sensor.read();
humidity=Sensor.getHumidity();
temperature= Sensor.getTemperature();
Serial.println("/*****/");
Serial.println(Sensor_Error_Code);
Serial.println("/*****/");
Serial.println("temperature : ");
Serial.print(temperature);
Serial.println("/*****/");
Serial.println("humidity : ");
Serial.print(humidity);
Serial.println("/*****/");
}
}
/********************************************************************************************************************/
void Led_Signal(String Led,byte r,byte g,byte b)
{
if (Led=="Led 1")
{
digitalWrite(Led_1_Red ,r);
digitalWrite(Led_1_Green ,g);
digitalWrite(Led_1_Blue ,b);
}
else if (Led=="Led 2")
{
digitalWrite(Led_2_Red ,r);
digitalWrite(Led_2_Green ,g);
digitalWrite(Led_2_Blue ,b);
}
}
/****************************************************************************************************************/
void Json_init()
{
Tempreature_json.add("Device ID",DEVICE_ID);
Tempreature_json.add("Value",temperature);
Tempreature_json.add("Warning","NONE");
Tempreature_json.add("Sensor State","OK");
//Tempreature_json.add("Min Threshhold",temp_min_threshhold);
//Tempreature_json.add("Max Threshhold",temp_max_threshhold);
Humidity_json.add("Device ID",DEVICE_ID);
Humidity_json.add("Value",humidity);
Humidity_json.add("Warning","NONE");
Humidity_json.add("Sensor State","OK");
//Humidity_json.add("Min Threshhold",hum_min_threshhold);
//Humidity_json.add("Max Threshhold",hum_max_threshhold);
}
void Update_data()
{
// setting the values of temperature and humidity
Tempreature_json.set("Value",temperature);
Humidity_json.set("Value",humidity);
/* the system has 2 rgb leds : one for temperature and one for humidity
* determining Led signaling outputs dependq on values read by the sensor and the thresholds
* Blue led 1 indicates high levels of humidity
* Red led 1 indicates low levels of humidity
* green led 1 indicates that the level of humidity is within human indoor confort range
*/
if (humidity>hum_max_threshhold)
{
Humidity_json.set("Warning","HUMIDITY ABOVE 60 !!" );
Led_Signal("Led 1",0,0,255);
}
else if (humidity<hum_min_threshhold){
Humidity_json.set("Warning","HUMIDITY BELOW 30 !!" );
Led_Signal("Led 1",255,0,0);
}
else {
Humidity_json.set("Warning","NONE");
Led_Signal("Led 1",0,255,0);
}
/*
* Blue led 2 indicates low levels of temperature
* Red led 2 indicates high levels of temperature
* green led 1 indicates that the level of temperature is within human indoor confort range
*/
if (temperature>temp_max_threshhold)
{
Tempreature_json.set("Warning","TEMPERATURE ABOVE 30 !!");
Led_Signal("Led 2",255,0,0);
}
else if (temperature<temp_min_threshhold)
{
Tempreature_json.set("Warning","TEMPERATURE BELOW 20 !!");
Led_Signal("Led 2",0,0,255);
}
else {
Tempreature_json.set("Warning","NONE");
Led_Signal("Led 2",0,255,0);
}
switch (Sensor_Error_Code)
{
case DHTLIB_OK:
SensorState="OK";
break;
case DHTLIB_ERROR_CHECKSUM:
SensorState="Checksum error";
break;
case DHTLIB_ERROR_TIMEOUT_A:
SensorState="Time out A error";
break;
case DHTLIB_ERROR_TIMEOUT_B:
SensorState="Time out B error";
break;
case DHTLIB_ERROR_TIMEOUT_C:
SensorState="Time out C error";
break;
case DHTLIB_ERROR_TIMEOUT_D:
SensorState="Time out D error";
break;
case DHTLIB_ERROR_SENSOR_NOT_READY:
SensorState="Sensor not Ready : check sensor connections ";
break;
case DHTLIB_ERROR_BIT_SHIFT:
SensorState="Bit shift error";
break;
case DHTLIB_WAITING_FOR_READ:
SensorState="Waiting for read";
break;
default:
SensorState="Unknown ";
break;
}
Tempreature_json.set("Sensor State",SensorState);
Humidity_json.set("Sensor State",SensorState);
Serial.println("updating");
Firebase.updateNode(fbdo, temp_path , Tempreature_json);
Firebase.updateNode(fbdo, hum_path , Humidity_json);
//if (!Firebase.setJSON(fbdo, hum_path, Humidity_json))Serial.println("no set json");
}
int get_hum_max_thresh(){
if (Firebase.getInt(fbdo, (hum_path+"/Max Threshhold")) )
{
if (fbdo.dataType() == "int") {
// Serial.println(fbdo.intData());
//Serial.println(fbdo.dataType());
}
} else {
Serial.println(fbdo.errorReason());
Serial.println(".................................");
Serial.println(fbdo.dataType());
}
}