#define BLYNK_TEMPLATE_ID "TMPL39krsjNT-"
#define BLYNK_TEMPLATE_NAME "Fire Detection"
#define BLYNK_AUTH_TOKEN "tU8gZPgZL7vGC2IO3F6H15M-keaih-2J"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Change your Wifi/ Hotspot Name
char pass[] = ""; // Change your Wifi/ Hotspot Password
BlynkTimer timer;
#define fire 23
#define GREEN 12
#define RED 14
#define buzzer 13
#define OLED_RESET 4
int fire_Val = 0;
WidgetLED led(V1);
Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display(OLED_RESET);
const int threshold = 10.0; // Define the acceleration threshold for earthquake detection
float previousX = 0.0; // Store previous X-axis acceleration value
void setup() //Setup function - only function that is run in deep sleep mode
{
Serial.begin(115200); //Start the serial output at 9600 baud
pinMode(GREEN, OUTPUT);
pinMode(fire, INPUT);
pinMode(RED, OUTPUT);
pinMode(buzzer, OUTPUT);
Blynk.begin(auth, ssid, pass);//Splash screen delay
delay(2000);
timer.setInterval(500L, mySensor);
while (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
pinMode(buzzer, OUTPUT);
// Begin OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Use I2C address 0x3C for 128x64 OLED
display.display(); // Clear the display
display.setTextSize(1); // Set text size
display.setTextColor(WHITE); // Set text color to white
}
void loop() //Loop function
{
Blynk.run();
timer.run();
sensors_event_t event;
mpu.getAccelerometerSensor()->getEvent(&event);
float currentX = event.acceleration.x; // Get current X-axis acceleration value
// Detect sudden changes in X-axis acceleration
if (abs(currentX - previousX) > threshold) {
// Display earthquake message on OLED
display.clearDisplay();
display.setCursor(0, 0);
display.println("Earthquake detected!");
display.display();
// Sound the buzzer to indicate an earthquake
tone(13, 1000);
Serial.println("Earthquake detected!");
// Delay for 5 seconds to prevent the buzzer from sounding continuously
delay(5000);
// Stop the buzzer
noTone(13);
// Delay for 5 seconds before clearing the OLED display
delay(5000);
display.clearDisplay(); // Clear the earthquake message after 5 seconds
}
previousX = currentX; // Update previous X-axis acceleration value
// Delay for 250 milliseconds for continuous monitoring
delay(250);
}
void mySensor()
{
fire_Val = digitalRead(fire);
if (fire_Val == LOW)
{
Serial.println("Fire in the House");
Blynk.logEvent("fire_alert", "Fire Detected");
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
digitalWrite(buzzer, HIGH);
Blynk.virtualWrite(V0, 1);
Serial.print("Fire Level: ");
Serial.println(fire_Val);
led.on();
}
else
{
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(buzzer, LOW);
Blynk.virtualWrite(V0, 0);
Serial.print("Fire Level: ");
Serial.println(fire_Val);
led.off();
}
}