-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathARDUNIO.ino
341 lines (306 loc) · 9.76 KB
/
ARDUNIO.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
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <PZEM004Tv30.h>
#include <analogWrite.h>
#include "DHT.h"
#include <NTPClient.h>
#include <TimeLib.h>
#include <SPI.h>
#define FIREBASE_HOST "ENTER_YOUR_FIREBASE_URL"
#define FIREBASE_AUTH "ENTER_FIREBASE_KEY"
#define WIFI_SSID "YOUR_SSID"
#define WIFI_PASSWORD "YOUR_PWD"
#define DHTTYPE DHT11
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char monthsOfYear[12][12] = {"January", "February", "March", "April", "May", "June", "July" , "August", "September", "October", "November", "December"};
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
String formattedDate, formattedTime, dayStamp, timeStamp, autoday, withoutsec, months, finalMonth;
int monthint, monthdiff;
int hi, ti, flame_detected, mq6 ,mq135 ,soil, dbdistance,level;
float h ,t;
uint8_t DHTPin = 27;
DHT dht(DHTPin, DHTTYPE);
PZEM004Tv30 pzem(&Serial2);
int in1 = 4;
int in2 = 0;
int in3 = 2;
int in4 = 15;
int fire = 5;
int buzzer = 26;
int trigPin = 18;
int echoPin = 19;
long duration;
int distance;
int voltagei ,frequencyi ;
FirebaseData firebaseData;
WiFiClient client;
void setup()
{
Serial.begin(115200);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(fire, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pzem.resetEnergy();
pzem.setAddress(0x42);
dht.begin();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
timeClient.begin();
timeClient.setTimeOffset(19800);
while(!timeClient.update()) {
timeClient.forceUpdate();
}
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
void loop() {
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy();
float voltage = pzem.voltage();
float frequency = pzem.frequency();
float pf = pzem.pf();
voltagei = (int) voltage;
frequencyi = (int) frequency;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
flame_detected = digitalRead(fire);
mq6 = digitalRead(12);
mq135 = analogRead(35);
soil = analogRead(36);
h = dht.readHumidity();
t = dht.readTemperature();
hi = (int) h;
ti = (int) t;
while(!timeClient.update()) {
timeClient.forceUpdate();
}
formattedDate = timeClient.getFormattedDate();
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
months=dayStamp.substring(5,7);
monthint=months.toInt();
monthdiff=monthint-1;
finalMonth = String(monthsOfYear[monthdiff]);
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
withoutsec=timeStamp.substring(0,5);
autoday=String(daysOfTheWeek[timeClient.getDay()]);
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port1/days"))
{
String p1days=firebaseData.stringData();
p1days.replace('[', ' ');
p1days.replace(']', ' ');
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port1/ontime");
String p1otime=firebaseData.stringData();
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port1/offtime");
String p1oftime=firebaseData.stringData();
String myarrayp1[] = {p1days} ;
for ( int i = 0; i < sizeof(myarrayp1); i++ )
{
if(myarrayp1[i]==autoday)
{
if(p1otime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port1","ON");
}
if(p1oftime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port1","OFF");
}
}
}
}
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port2/days"))
{
String p2days=firebaseData.stringData();
p2days.replace('[', ' ');
p2days.replace(']', ' ');
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port2/ontime");
String p2otime=firebaseData.stringData();
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port2/offtime");
String p2oftime=firebaseData.stringData();
String myarrayp2[] = {p2days} ;
for ( int i = 0; i < sizeof(myarrayp2); i++ )
{
if(myarrayp2[i]==autoday)
{
if(p2otime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port2","ON");
}
if(p2oftime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port2","OFF");
}
}
}
}
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port3/days"))
{
String p3days=firebaseData.stringData();
p3days.replace('[', ' ');
p3days.replace(']', ' ');
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port3/ontime");
String p3otime=firebaseData.stringData();
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port3/offtime");
String p3oftime=firebaseData.stringData();
String myarrayp3[] = {p3days} ;
if(p3days.indexOf(autoday) > 0)
{
if(p3otime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port3","ON");
}
if(p3oftime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port3","OFF");
}
}
}
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port4/days"))
{
String p4days=firebaseData.stringData();
p4days.replace('[', ' ');
p4days.replace(']', ' ');
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port4/ontime");
String p4otime=firebaseData.stringData();
Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/AutomaticOnOff/port4/offtime");
String p4oftime=firebaseData.stringData();
String myarrayp4[] = {p4days} ;
for ( int i = 0; i < sizeof(myarrayp4); i++ )
{
if(myarrayp4[i]==autoday)
{
if(p4otime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port4","ON");
}
if(p4oftime.equals(withoutsec))
{
Firebase.setString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port4","OFF");
}
}
}
}
if( !isnan(current) ){
if(Firebase.getFloat(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/current/"+finalMonth+"/"+dayStamp))
{
float oldVal=firebaseData.floatData();
float newVal=oldVal+(current/1000.000f);
Firebase.setFloat(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/current/"+finalMonth+"/"+dayStamp,newVal);
}
else
{
float newVal=current/1000.000f;
Firebase.setFloat(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/current/"+finalMonth+"/"+dayStamp,newVal);
}
}
if( !isnan(power) ){
if(Firebase.getFloat(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/power/"+finalMonth+"/"+dayStamp))
{
float oldVal=firebaseData.floatData();
float newVal=oldVal+(power/10.0f);
Firebase.setFloat(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/power/"+finalMonth+"/"+dayStamp,newVal);
}
else
{
float newVal=power/10.0f;
Firebase.setFloat(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/power/"+finalMonth+"/"+dayStamp,newVal);
}
}
if( !isnan(voltage) ){
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/voltage",voltagei);
}
if( !isnan(frequency) ){
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/frequency",frequencyi);
}
if( !isnan(pf) ){
Firebase.setFloat(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/electricity/pf",pf);
}
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port1"))
{
String val=firebaseData.stringData();
if(val=="ON")
{
digitalWrite(in1,LOW);
}
else
{
digitalWrite(in1,HIGH);
}
}
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port2"))
{
String val=firebaseData.stringData();
if(val=="ON")
{
digitalWrite(in2,LOW);
}
else
{
digitalWrite(in2,HIGH);
}
}
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port3"))
{
String val=firebaseData.stringData();
if(val=="ON")
{
digitalWrite(in3,LOW);
}
else
{
digitalWrite(in3,HIGH);
}
}
if(Firebase.getString(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/components/port4"))
{
String val=firebaseData.stringData();
if(val=="ON")
{
digitalWrite(in4,LOW);
}
else
{
digitalWrite(in4,HIGH);
}
}
Firebase.getInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/water/distance");
dbdistance = firebaseData.intData();
level = dbdistance - distance;
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/water/level",level);
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/air/temp", ti);
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/air/humidity", hi);
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/gas/gasleak", mq6);
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/air/mq135q", mq135);
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/plant/moisture", soil);
Firebase.setInt(firebaseData, "users/3Oou95cKpPX6OPBduFNTOHx6sO32/fire/fireleak", flame_detected);
if(flame_detected==0)
{
digitalWrite(buzzer, HIGH);
}
else
{
digitalWrite(buzzer, LOW);
}
delay(1000);
}