-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFire_Detection_Alarm_System.ino
116 lines (97 loc) · 3.52 KB
/
Fire_Detection_Alarm_System.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
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <DHT.h>
// Firebase settings
#define FIREBASE_HOST "https://firebase.com" // Firebase host URL"
#define FIREBASE_AUTH "Firebase_authentication_token" // Firebase authentication token
// WiFi credentials
#define WIFI_SSID "WIFI_NAME" // Your WiFi SSID
#define WIFI_PASSWORD "WIFI_PASSWORD" // Your WiFi password
// Sensor and alarm pin definitions
#define DHTPIN 13 // DHT11 data pin connected to GPIO 13
#define DHTTYPE DHT11 // Define DHT type
#define MQ135_PIN 34 // MQ-135 analog pin connected to GPIO 34
#define ALARM_PIN 14 // Alarm module pin connected to GPIO 14
DHT dht(DHTPIN, DHTTYPE);
FirebaseData firebaseData;
FirebaseAuth firebaseAuth;
FirebaseConfig firebaseConfig;
// Fire detection threshold values
const int smokeThreshold = 1200; // Adjust based on sensitivity of MQ-135
const int tempThreshold = 50; // Temperature threshold for fire detection
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(MQ135_PIN, INPUT);
pinMode(ALARM_PIN, OUTPUT);
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize Firebase
firebaseConfig.host = FIREBASE_HOST;
firebaseConfig.signer.tokens.legacy_token = FIREBASE_AUTH; // Use `token.token` if you have a custom token
Firebase.begin(&firebaseConfig, &firebaseAuth);
Firebase.reconnectWiFi(true);
// Check Firebase connection status
if (!Firebase.ready()) {
Serial.println("Firebase initialization failed. Check configuration and credentials.");
} else {
Serial.println("Connected to Firebase");
}
// Enable debug messages for Firebase
}
void loop() {
// Reading temperature and smoke levels
float temperature = dht.readTemperature();
int smokeLevel = analogRead(MQ135_PIN);
// Check if data is valid
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Determine if fire is detected
bool fireDetected = (smokeLevel > smokeThreshold) || (temperature > tempThreshold);
// Trigger alarm if fire is detected
if (fireDetected) {
digitalWrite(ALARM_PIN, LOW); // Turn alarm ON
} else {
digitalWrite(ALARM_PIN, HIGH); // Turn alarm OFF
}
// Update Firebase
if (Firebase.ready()) {
if (Firebase.setFloat(firebaseData, "/Temperature", temperature)) {
Serial.println("Temperature data sent to Firebase");
} else {
Serial.print("Failed to send Temperature data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.setInt(firebaseData, "/SmokeLevel", smokeLevel)) {
Serial.println("Smoke Level data sent to Firebase");
} else {
Serial.print("Failed to send Smoke Level data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.setBool(firebaseData, "/FireDetected", fireDetected)) {
Serial.println("Fire Detection status sent to Firebase");
} else {
Serial.print("Failed to send Fire Detection status: ");
Serial.println(firebaseData.errorReason());
}
} else {
Serial.print("Firebase is not ready: ");
Serial.println(firebaseData.errorReason());
}
// Debugging output
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Smoke Level: ");
Serial.println(smokeLevel);
Serial.print("Fire Detected: ");
Serial.println(fireDetected);
// Delay for stability
delay(2000);
}