Updates Smoke Monitoring
Updates Smoke Monitoring
h>
#include <HTTPClient.h>
#include <ESP32Servo.h>
// Uncomment and use this alternate URL for testing connectivity with a reliable
endpoint
// const char* webhookUrl = "https://httpbin.org/get";
// Pin Definitions
#define MQ2_PIN 34 // MQ-2 Smoke Sensor analog output connected to GPIO 34
#define RED_LED 4 // Red LED connected to GPIO 4
#define GREEN_LED 2 // Green LED connected to GPIO 2
#define BUZZER 15 // Buzzer connected to GPIO 15
#define SERVO_PIN 13 // Servo Motor control connected to GPIO 13
Servo myServo;
int sensorThreshold = 2000;
int resetThreshold; // Will be set to half of sensorThreshold
unsigned long lastWiFiAttempt = 0;
const unsigned long wifiRetryInterval = 10000; // Try WiFi reconnection every 10
seconds (reduced from 30s)
// Testing variables
bool testModeActive = false;
unsigned long testModeStartTime = 0;
const unsigned long testModeDuration = 10000; // 10 seconds test
// Buzzer variables
unsigned long buzzerToggleTime = 0;
bool buzzerState = false;
const unsigned long buzzerToggleInterval = 500; // Toggle every 500ms for more
noticeable sound
if (httpResponseCode > 0) {
Serial.print("Webhook response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println("Response: " + payload);
lastEmailTime = millis(); // Update last email timestamp on success
emailSent = true;
} else {
Serial.print("Error sending webhook: ");
Serial.println(http.errorToString(httpResponseCode));
Serial.println("Possible reasons for connection refused:");
Serial.println("1. Incorrect webhook URL");
Serial.println("2. Google Apps Script permissions issue");
Serial.println("3. Network firewall blocking the connection");
Serial.println("4. Network requires proxy configuration");
}
http.end(); // Close connection
} else {
Serial.println("WiFi not connected!");
connectToWiFi(); // Try to reconnect immediately
}
}
bool connectToWiFi() {
Serial.println("\n------------------------------");
Serial.print("Connecting to WiFi SSID: ");
Serial.println(ssid);
WiFi.disconnect(true);
delay(1000);
WiFi.begin(ssid, password);
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(GREEN_LED, HIGH); // Show connected status
digitalWrite(RED_LED, LOW); // Ensure RED LED is off
return true;
} else {
Serial.println("\nFailed to connect to WiFi!");
Serial.print("WiFi status code: ");
Serial.println(WiFi.status());
digitalWrite(RED_LED, HIGH); // Indicate failed connection
delay(500);
digitalWrite(RED_LED, LOW);
return false;
}
}
int connectionAttempts = 0;
if (connectToWiFi()) {
// Connection successful
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
Serial.println("WiFi connected successfully!");
return;
}
// Test Servo
Serial.println("Testing SERVO...");
myServo.write(15);
delay(1000);
myServo.write(150);
delay(500);
void setup() {
Serial.begin(115200);
delay(1000); // Give serial monitor time to start up
Serial.println("\n\n===== ESP32 SMOKE DETECTOR SYSTEM =====");
Serial.println("Production-Ready Version with Auto-Reset");
// Initialize servo
myServo.attach(SERVO_PIN);
myServo.write(150); // Default position (window closed)
void loop() {
// Check for Serial commands
if (Serial.available() > 0) {
char command = Serial.read();
if (command == 'T' || command == 't') {
testModeActive = true;
testModeStartTime = millis();
Serial.println("TEST MODE ACTIVATED - Simulating smoke detection for 10
seconds");
} else if (command == 'C' || command == 'c') {
Serial.println("Running component test...");
testComponents();
} else if (command == 'W' || command == 'w') {
Serial.println("Forcing WiFi reconnection...");
ensureWiFiConnection();
} else if (command == 'R' || command == 'r') {
Serial.println("Manual system reset initiated");
resetAlarm();
}
}
if (shouldSendEmail) {
if (testModeActive) {
Serial.println("🚨 [TEST] Smoke Detected! Sending actual email alert...");
} else {
Serial.println("🚨 Smoke Detected! Sending Email via Webhook...");
}
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(GREEN_LED, HIGH); // Keep green LED on when connected
} else {
digitalWrite(GREEN_LED, LOW);
}
}