#include <WiFi.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int buzzerPin = 5; // Define buzzer pin
const float threshold = 0.5; // Define earthquake threshold (adjust as needed)
Adafruit_MPU6050 mpu;
LiquidCrystal_I2C LCD(0x27, 20, 4);
float ax, ay, az;
float ax_prev,ay_prev;
bool earthquakeDetected = false;
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Hi");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (!earthquakeDetected) {
// Check if X and Y axis changes exceed the threshold
if (abs(ax - ax_prev) > threshold || abs(ay - ay_prev) > threshold) {
earthquakeDetected = true;
Serial.println("Earthquake detected!");
digitalWrite(buzzerPin, HIGH);
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("*** EARTHQUAKE! ***");
LCD.setCursor(0, 1);
LCD.println("Seek safe shelter!");
}
} else {
delay(5000);
earthquakeDetected = false;
digitalWrite(buzzerPin, LOW);
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Hi");
}
ax_prev = ax;
ay_prev = ay;
}