ESP32 and Android Earthquake Alarm: A Hands-on Guide

ChrisCat Intermediate 1h ago Updated Jul 25, 2026 228 views 1 likes 3 min read

Most basic seismic sensors just trigger a buzzer, but the real value is in the latency between the vibration detection and the mobile notification. By pairing an ESP32 with an Android frontend via MQTT, you can create a system that alerts you the millisecond a threshold is hit, rather than waiting for a slow cloud polling cycle.

The Hardware Stack

To get this running, you need an MPU6050 accelerometer. It's cheap and precise enough to detect significant tremors. The ESP32 handles the logic and WiFi connectivity, while an Android app acts as the monitoring dashboard.

Required Components:

  • ESP32 Development Board
  • MPU6050 (Accelerometer + Gyroscope)
  • Jumper wires and a breadboard
  • An MQTT Broker (like Mosquitto or HiveMQ)
ESP32 and Android Earthquake Alarm: A Hands-on Guide

Step-by-Step Deployment

1. Wiring the Sensor
Connect the MPU6050 to the ESP32 using I2C pins. On most ESP32 boards, use GPIO 21 (SDA) and GPIO 22 (SCL). Power the sensor with 3.3V.

2. The ESP32 Logic
The core of the system is calculating the magnitude of acceleration. You aren't looking for a constant value, but a sudden spike in the G-force vector.

Here is a simplified snippet of how to handle the acceleration threshold in your Arduino IDE:

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <WiFi.h>

Adafruit_MPU6050 mpu;
WiFiClient espClient;
PubSubClient client(espClient);

const float THRESHOLD = 1.5; // G-force threshold for alarm

void setup() {
  Serial.begin(115200);
  if (!mpu.begin()) {
    while (1) yield();
  }
  // Setup WiFi and MQTT connection here
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  // Calculate vector magnitude: sqrt(x^2 + y^2 + z^2)
  float magnitude = sqrt(sq(a.acceleration.x) + sq(a.acceleration.y) + sq(a.acceleration.z));
  
  // Normalize by gravity (~9.8 m/s^2)
  float gForce = magnitude / 9.81;

  if (gForce > THRESHOLD) {
    client.publish("home/earthquake", "ALARM: High Vibration Detected!");
  }
  delay(100);
}

3. Android Integration
Instead of building a full app from scratch, you can use a lightweight MQTT client for Android. Configure the app to subscribe to the home/earthquake topic. Set the Quality of Service (QoS) to 1 to ensure the message is delivered even if the network flickers.

Real-world Performance Tuning

If you just use the code above, you'll get a ton of false positives from someone walking past the sensor. To fix this, I implemented a simple "confirmation window" logic: the alarm only triggers if the threshold is exceeded for 3 consecutive readings (approx 300ms).

Calibration Tip:
The MPU6050 often has a factory offset. Before deploying, run a calibration script to find the "zero" value for your specific sensor's orientation. If the sensor is tilted, the Z-axis will bleed into X and Y, messing up your magnitude calculation.

Is it worth the effort?

Compared to professional seismic equipment, this is a toy. But as a practical tutorial for an AI workflow or IoT deployment, it's great because it teaches you about signal noise and event-driven architecture.

  • Latency: Local MQTT setups usually trigger the Android notification in under 200ms.
  • Reliability: The ESP32's deep sleep mode can be used to make this battery-powered, though you'll lose the real-time monitoring unless you use a hardware interrupt to wake the chip.

For those looking to scale this, you could integrate a LLM agent to analyze the vibration patterns and provide a summary of the event intensity, though that requires piping the data into a time-series database first.
ResourcesToolsTutorial

All Replies (2)

K
KaiDev Expert 9h ago
Hope you're using a capacitor to handle the power spikes or your ESP32 will just reboot every time it actually detects something.
0 Reply
S
SoloSmith Expert 9h ago
Are you seeing much lag with the MQTT broker, or is the notification hitting the phone almost instantly?
0 Reply

Write a Reply

Markdown supported