-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDHT_MQ_PWM_firebase.ino
145 lines (110 loc) · 3.38 KB
/
DHT_MQ_PWM_firebase.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
#include <FirebaseESP32.h>
#include <WiFi.h>
#include "DHT.h"
#define FIREBASE_HOST "https://smartsilo-4115a-default-rtdb.firebaseio.com/"
#define WIFI_SSID "AndroidAPD9EE" // Change the name of your WIFI
#define WIFI_PASSWORD "Vedant@1712" // Change the password of your WIFI
#define FIREBASE_Authorization_key "olLWg0GODF6Y5IqQ8FgshqvknrkMUyR2B2W2V9UM"
#define DHTPIN1 13
#define DHTPIN2 15
#define MQ1pin 32
#define MQ2pin 33
#define LED_GPIO 5
#define PWM1_Ch 0
#define PWM1_Res 8
#define PWM1_Freq 1000
#define DHTTYPE DHT22
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
FirebaseData firebaseData1;
FirebaseJson json;
float h1,t1,h2,t2;
float MQ1value;
float MQ2value;
float tavg;
int PWM1_DutyCycle = 0;
void setup() {
Serial.begin(9600);
pinMode(MQ1pin,INPUT);
pinMode(MQ2pin,INPUT);
Serial.println(F("DHTxx test!"));
dht1.begin();
dht2.begin();
ledcAttachPin(LED_GPIO, PWM1_Ch);
ledcSetup(PWM1_Ch, PWM1_Freq, PWM1_Res);
WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_Authorization_key);
if (!Firebase.beginStream(firebaseData1, "Electriculture" ))
{
Serial.println("------------------------------------");
Serial.println("Can't begin stream connection...");
Serial.println("REASON: " + firebaseData1.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("Stream success");
}
}
void loop() {
//delay(2000);
//DHT---------------
h1 = dht1.readHumidity();
t1 = dht1.readTemperature();
h2 = dht2.readHumidity();
t2 = dht2.readTemperature();
if (isnan(h1) || isnan(t1) || isnan(h2) || isnan(t2) ) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Serial.print(F("Humidity 1 : "));
// Serial.print(h1);
// Serial.print(F("% Temperature 1 : "));
// Serial.print(t1);
// Serial.print(F("°C "));
// Serial.print(F("Humidity 2 : "));
// Serial.print(h2);
// Serial.print(F("% Temperature 2 : "));
// Serial.print(t2);
// Serial.print(F("°C "));
Firebase.setFloat(firebaseData1, "/EFarm/Humidity1", h1);
Firebase.setFloat(firebaseData1, "/EFarm/Temperature1", t1);
Firebase.setFloat(firebaseData1, "/EFarm/Humidity2", h2);
Firebase.setFloat(firebaseData1, "/EFarm/Temperature2", t2);
//Relay-------------
tavg=(t1+t2)/2;
Serial.println(tavg);
Serial.println("Fan Speed");
if(26>=tavg){
ledcWrite(PWM1_Ch, 50);
Serial.println("50");
}
else if((26<tavg) && (tavg<28)){
ledcWrite(PWM1_Ch, 150);
Serial.println("150");
}
else if(tavg>=28){
ledcWrite(PWM1_Ch, 255);
Serial.println("255");
}
//MQ135-----------
MQ1value = analogRead(MQ1pin);
// Serial.print("MQ135 sensor no.1 : ");
// Serial.println(MQ1value, DEC);
Firebase.setFloat(firebaseData1, "/EFarm/MQ1value", MQ1value);
MQ2value = analogRead(MQ2pin);
// Serial.print("MQ135 sensor no.2 : ");
// Serial.println(MQ2value, DEC);
Firebase.setFloat(firebaseData1, "/EFarm/MQ2value", MQ2value);
}