-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollution arduino code.ino
196 lines (164 loc) · 5.9 KB
/
pollution arduino code.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
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
#include <DHT.h>
// Provide the token generation process info.
#include "addons/TokenHelper.h"
// Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
// Insert your network credentials
#define WIFI_SSID "Your wifi name"
#define WIFI_PASSWORD "Your wifi password"
// Insert Firebase project API Key
#define API_KEY "your firebase api key"
// Insert RTDB URL
#define DATABASE_URL "put your firebase realtiime databasee link"
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
// Pin Definitions
#define RAIN_SENSOR_PIN A0
#define MQ2_SENSOR_PIN D0
#define FLAME_SENSOR_PIN D1
#define BUZZER_PIN D2
#define L298N_IN2_PIN D6
#define L298N_IN3_PIN D7
// Threshold Values
#define RAIN_THRESHOLD 1000 // Set your desired rain threshold value
#define MQ2_THRESHOLD 0 // Set your desired MQ2 gas threshold value
#define FLAME_THRESHOLD 0 // Set your desired flame threshold value
#define TEMPERATURE_THRESHOLD 40 // Set your desired temperature threshold value
// DHT Sensor
#define DHT_PIN D5
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(115200);
pinMode(RAIN_SENSOR_PIN, INPUT);
pinMode(MQ2_SENSOR_PIN, INPUT);
pinMode(FLAME_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(L298N_IN2_PIN, OUTPUT);
pinMode(L298N_IN3_PIN, OUTPUT);
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();
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
/* 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);
Firebase.reconnectWiFi(true);
dht.begin();
}
void loop() {
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 5000 || sendDataPrevMillis == 0)) {
sendDataPrevMillis = millis();
// Perform your sensor checks and alerts here
// Read DHT sensor values
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display sensor values on the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.print(" %\t");
// Check temperature threshold
if (temperature > TEMPERATURE_THRESHOLD) {
// Send an alert to Firebase or perform any required action
Firebase.RTDB.setString(&fbdo, "Alert", "High temperature detected");
Serial.println("High temperature detected");
digitalWrite(BUZZER_PIN, HIGH); // Activate the buzzer
} else {
// No alert condition
Firebase.RTDB.setString(&fbdo, "Alert", "No alert");
Serial.println("No alert");
digitalWrite(BUZZER_PIN, LOW); // Deactivate the buzzer
}
// Control L298N based on flame sensor and DHT sensor readings
int flameValue = digitalRead(FLAME_SENSOR_PIN);
if (flameValue == HIGH) {
// Flame detected, turn on IN2
digitalWrite(L298N_IN2_PIN, HIGH);
} else {
// No flame detected, turn off IN2
digitalWrite(L298N_IN2_PIN, LOW);
}
if (humidity > TEMPERATURE_THRESHOLD) {
// High humidity detected, turn on IN3
digitalWrite(L298N_IN3_PIN, HIGH);
} else {
// Humidity within threshold, turn off IN3
digitalWrite(L298N_IN3_PIN, LOW);
}
// Send temperature and humidity values to Firebase
Firebase.RTDB.setFloat(&fbdo, "Temperature", temperature);
Firebase.RTDB.setFloat(&fbdo, "Humidity", humidity);
// Read rain sensor value
int rainValue = analogRead(RAIN_SENSOR_PIN);
// Read MQ2 gas sensor value
int mq2Value = digitalRead(MQ2_SENSOR_PIN);
// Display sensor values on the serial monitor
Serial.print("Rain Sensor: ");
Serial.print(rainValue);
Serial.print("\tMQ2 Sensor: ");
Serial.print(mq2Value);
Serial.print("\tFlame Sensor: ");
Serial.println(flameValue);
if (rainValue <= RAIN_THRESHOLD) {
// Send an alert to Firebase or perform any required action
Firebase.RTDB.setString(&fbdo, "Alert", "Rain detected");
Serial.println("Rain detected");
digitalWrite(BUZZER_PIN, HIGH); // Activate the buzzer
} else {
// No alert condition
Firebase.RTDB.setString(&fbdo, "Alert", "No alert");
Serial.println("No alert");
digitalWrite(BUZZER_PIN, LOW); // Deactivate the buzzer
}
if (mq2Value == 1) {
// Send an alert to Firebase or perform any required action
Firebase.RTDB.setString(&fbdo, "Alert", "Gas detected");
Serial.println("Gas detected");
digitalWrite(BUZZER_PIN, HIGH); // Activate the buzzer
} else {
// No alert condition
Firebase.RTDB.setString(&fbdo, "Alert", "No alert");
Serial.println("No alert");
digitalWrite(BUZZER_PIN, LOW); // Deactivate the buzzer
}
// Send sensor values to Firebase
Firebase.RTDB.setInt(&fbdo, "RainSensor", rainValue);
Firebase.RTDB.setInt(&fbdo, "MQ2Sensor", mq2Value);
Firebase.RTDB.setInt(&fbdo, "FlameSensor", flameValue);
delay(1000); // Delay for 5 seconds before reading sensor values again
}
}