-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireSystem.ino
More file actions
144 lines (123 loc) · 3.97 KB
/
Copy pathFireSystem.ino
File metadata and controls
144 lines (123 loc) · 3.97 KB
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
#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_MLX90640.h>
#include "secrets.h" // WiFi credentials and fire alert URL
// ------------------------
// Pin Definitions
// ------------------------
#define RELAY_PIN 34 // GPIO pin connected to relay module
// ------------------------
// System Configuration
// ------------------------
#define FIRE_THRESHOLD 60.0 // Temperature threshold in Celsius
#define ACTUATOR_DURATION 120000 // Actuator run time in milliseconds (2 minutes)
// ------------------------
// Global Variables
// ------------------------
Adafruit_MLX90640 mlx;
float frame[32 * 24]; // 32x24 thermal image frame
bool fireTriggered = false; // Tracks if fire response is active
bool messageSent = false; // Tracks if alert message was sent
unsigned long fireStartTime = 0; // Timestamp when fire was detected
// ------------------------
// Setup Function
// ------------------------
void setup() {
Serial.begin(115200);
// Configure relay pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure relay is off at startup
// Initialize I2C for thermal sensor
Wire.begin();
if (!mlx.begin(MLX90640_I2CADDR_DEFAULT, &Wire)) {
Serial.println("Error: MLX90640 sensor not found.");
while (true); // Halt execution if sensor not found
}
// Configure MLX90640 sensor settings
mlx.setMode(MLX90640_CHESS);
mlx.setResolution(MLX90640_ADC_18BIT);
mlx.setRefreshRate(MLX90640_4_HZ); // Approx. 4 frames per second
// Connect to WiFi network
connectToWiFi();
}
// ------------------------
// Connect to WiFi
// ------------------------
void connectToWiFi() {
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
int retries = 0;
while (WiFi.status() != WL_CONNECTED && retries < 20) {
delay(500);
Serial.print(".");
retries++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nWiFi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("\nFailed to connect to WiFi");
}
}
// ------------------------
// Send HTTP Fire Alert
// ------------------------
void sendFireAlert() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(FIRE_ALERT_URL);
http.addHeader("Content-Type", "application/json");
String payload = "{\"status\": \"fire_detected\", \"device\": \"ESP32-S3\", \"temp\": 60}";
int responseCode = http.POST(payload);
if (responseCode > 0) {
Serial.print("Fire alert sent successfully. HTTP response code: ");
Serial.println(responseCode);
} else {
Serial.print("Error sending alert: ");
Serial.println(http.errorToString(responseCode));
}
http.end();
} else {
Serial.println("WiFi not connected. Cannot send fire alert.");
}
}
// ------------------------
// Main Loop
// ------------------------
void loop() {
// Attempt to read a frame from the thermal sensor
if (!mlx.getFrame(frame)) {
Serial.println("Failed to read thermal frame.");
return;
}
// Check if any pixel exceeds the fire temperature threshold
bool fireDetected = false;
for (int i = 0; i < 32 * 24; i++) {
if (frame[i] >= FIRE_THRESHOLD) {
fireDetected = true;
break;
}
}
// Handle fire detection event
if (fireDetected && !fireTriggered) {
Serial.println("Fire detected. Activating relay.");
digitalWrite(RELAY_PIN, HIGH); // Turn on actuator
fireTriggered = true;
fireStartTime = millis();
if (!messageSent) {
sendFireAlert(); // Send HTTP notification
messageSent = true;
}
}
// Turn off actuator after timeout
if (fireTriggered) {
if (millis() - fireStartTime >= ACTUATOR_DURATION) {
Serial.println("Actuator timeout reached. Deactivating relay.");
digitalWrite(RELAY_PIN, LOW); // Turn off actuator
fireTriggered = false;
messageSent = false;
}
}
delay(250); // Wait between frames (matches 4Hz senso