#define BLYNK_TEMPLATE_ID "TMPL39krsjNT-"
#define BLYNK_TEMPLATE_NAME "Fire Detection"
#define BLYNK_AUTH_TOKEN "tU8gZPgZL7vGC2IO3F6H15M-keaih-2J"
#define BLYNK_PRINT Serial
#define fire 23
#define GREEN 12
#define RED 14
#define buzzer 13
#define OLED_RESET 4
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include<ThingSpeak.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Change your Wifi/ Hotspot Name
char pass[] = ""; // Change your Wifi/ Hotspot Password
WiFiClient client;
BlynkTimer timer;
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
int fire_Val = 0;
const int myChannelId = 2477093;
const char myWriteApiKey[] = "MBN9D48NNXWFZXRC";
int statusCode;
void setup() //Setup function - only function that is run in deep sleep mode
{
Serial.begin(115200); //Start the serial output at 9600 baud
fire_setup();
earthquake_setup();
electrical_setup();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() //Loop function
{
if(WiFi.status()!=WL_CONNECTED)
{
Serial.println("Attempting to Connect");
while(WiFi.status()!=WL_CONNECTED)
{
WiFi.begin(ssid,pass);
Serial.print(".");
delay(5000);
}
}
Serial.println("Connected");
Blynk.run();
timer.run();
pot_exe();
earthquake_sensor();
mySensor();
ThingSpeak.setField(1,fire_Val);
statusCode = ThingSpeak.writeFields(myChannelId,myWriteApiKey);
if(statusCode == 200)
{
Serial.println("Channel Updated");
}
else
{
Serial.println("Channel Not Updated");
}
delay(15000);
}
void fire_setup(){
pinMode(GREEN, OUTPUT);
pinMode(fire, INPUT);
pinMode(RED, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void electrical_setup(){
pinMode(5, OUTPUT);
}
void earthquake_setup(){
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
}
//fire sensor
void mySensor()
{
int fire_Val = digitalRead(fire);
if (fire_Val != LOW)
{
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(buzzer, LOW);
Blynk.virtualWrite(V0, 1);
Serial.print("Fire Level: ");
Serial.println(fire_Val);
led.off();
}
else
{
Serial.println("Fire in the House");
Blynk.logEvent("fire_alert", "Fire Detected");
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
digitalWrite(buzzer, HIGH);
Blynk.virtualWrite(V0, 0);
Serial.print("Fire Level: ");
Serial.println(fire_Val);
led.on();
}
}
//potentiometer loop to indicate the electrical leakage
void pot_exe(){
const int x = analogRead(34);
Serial.println(x);
if (x<2048)
{
digitalWrite(5, LOW);
}
else{
digitalWrite(5, HIGH);
}
ThingSpeak.setField(2,x);
}
void earthquake_sensor(){
sensors_event_t event;
mpu.getAccelerometerSensor()->getEvent(&event);
const float currentX = event.acceleration.x; // Get current X-axis acceleration value
ThingSpeak.setField(3,currentX);
// 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);}