This circuit is designed to monitor environmental conditions and control devices accordingly. It includes a vibration sensor (SW-420) and a smoke sensor (MQ-2) for detection purposes. The circuit uses an ESP32 microcontroller to process sensor data and control an exhaust fan and a DC motor via solid-state relays (SSRs). The ESP32 also hosts a web server to allow remote monitoring and control of the fan and motor. LED indicators provide a visual status of the system, with different colors representing different levels of alert.
/*
* This Arduino Sketch controls a vibration detector and a smoke detector.
* If vibration is detected, it alerts the user via serial print and web.
* If smoke is detected, it activates an exhaust fan through a solid-state relay.
* The exhaust fan can also be controlled via web, and the DC motor can only
* be controlled via web. It also sets up a web server to monitor the status
* and control the fan and motor. Additionally, it uses LEDs to indicate the
* status: green for no danger, yellow for partial danger, and red for full danger.
*/
#include <WiFi.h>
#include <WebServer.h>
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Pin definitions
const int vibrationSensorPin = 15; // Digital output of SW-420
const int smokeSensorPin = 19; // Analog output of MQ-2
const int relayPin1 = 25; // CH1 of Solid State Relay (Exhaust Fan)
const int relayPin2 = 33; // CH2 of Solid State Relay (DC Motor)
const int greenLEDPin = 26; // Green LED
const int yellowLEDPin = 27; // Yellow LED
const int redLEDPin = 14; // Red LED
// Create a web server object
WebServer server(80);
// Variables to store the state of the relays
bool relay1State = false;
bool relay2State = false;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize sensor pins
pinMode(vibrationSensorPin, INPUT);
pinMode(smokeSensorPin, INPUT);
// Initialize relay pins
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
// Initialize LED pins
pinMode(greenLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
// Ensure relays and LEDs are off initially
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
// Define web server routes
server.on("/", handleRoot);
server.on("/status", handleStatus);
server.on("/control", handleControl);
// Start the server
server.begin();
}
void loop() {
// Handle client requests
server.handleClient();
// Read sensor values
int vibrationValue = digitalRead(vibrationSensorPin);
int smokeValue = analogRead(smokeSensorPin);
// Check if vibration is detected
if (vibrationValue == HIGH) {
Serial.println("Alert: Vibration detected!");
}
// Check if smoke is detected
if (smokeValue > 300) { // Threshold for smoke
// Activate exhaust fan relay
digitalWrite(relayPin1, HIGH);
relay1State = true;
Serial.println("Alert: Smoke detected!");
} else {
// Deactivate exhaust fan relay
digitalWrite(relayPin1, LOW);
relay1State = false;
}
// LED indication logic
if (vibrationValue == HIGH && smokeValue > 300) {
// Red LED for full danger
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW