ESP32-Based Accident Detection and GPS Tracking System with GSM Notifications
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit integrates a microcontroller (ESP32), a GPS module (Neo 6M), an accelerometer/gyroscope (InvenSense MPU6050), and a GSM module (sim 800l) to create a system capable of detecting accidents and providing location data. The ESP32 serves as the central processing unit, interfacing with the GPS module for location tracking, the MPU6050 for motion detection, and the sim 800l GSM module for communication purposes.
Component List
ESP32 (30 pin)
- Description: A powerful microcontroller with Wi-Fi and Bluetooth capabilities, featuring a wide range of GPIO pins.
- Purpose: Acts as the central processing unit, managing sensor data, and handling communication with the GPS and GSM modules.
InvenSense MPU6050
- Description: A 6-axis MEMS sensor combining a 3-axis gyroscope and a 3-axis accelerometer.
- Purpose: Used for detecting sudden changes in acceleration, which may indicate an accident.
Neo 6M GPS Module
- Description: A compact GPS module capable of providing accurate location data.
- Purpose: Provides real-time location data to the system.
sim 800l
- Description: A GSM/GPRS module that allows for cellular communication, including SMS and data transfer.
- Purpose: Enables the system to send SMS alerts and communicate over cellular networks.
Wiring Details
ESP32 (30 pin)
RX2connected to sim 800lTXDTX2connected to sim 800lRXDRX0connected to Neo 6M GPS ModuleTX3V3connected to InvenSense MPU6050VCC, sim 800lVCC, Neo 6M GPS ModuleVCCGNDconnected to InvenSense MPU6050GND, sim 800lGND, Neo 6M GPS ModuleGNDD22connected to InvenSense MPU6050SCLD21connected to InvenSense MPU6050SDA
InvenSense MPU6050
VCCconnected to ESP323V3GNDconnected to ESP32GNDSCLconnected to ESP32D22SDAconnected to ESP32D21
Neo 6M GPS Module
VCCconnected to ESP323V3GNDconnected to ESP32GNDTXconnected to ESP32RX0
sim 800l
VCCconnected to ESP323V3GNDconnected to ESP32GNDTXDconnected to ESP32RX2RXDconnected to ESP32TX2
Documented Code
ESP32 (30 pin) Code
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <HTTPClient.h>
// GPS Module
TinyGPSPlus gps;
HardwareSerial gpsSerial(1); // Use UART1 for GPS
// MPU6050 Accelerometer
Adafruit_MPU6050 mpu;
// GSM Module
HardwareSerial gsmSerial(2); // Use UART2 for GSM
const char* apn = "your_apn"; // Replace with your APN
const char* user = "your_user"; // Replace with your APN username
const char* pass = "your_pass"; // Replace with your APN password
const char* phone_number = "your_phone_number"; // Replace with the recipient's phone number
void setup() {
Serial.begin(115200);
// Initialize GPS
gpsSerial.begin(9600, SERIAL_8N1, 16, 17); // RX, TX pins for GPS
Serial.println("GPS module initialized");
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 initialized");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Initialize GSM
gsmSerial.begin(9600, SERIAL_8N1, 26, 27); // RX, TX pins for GSM
Serial.println("GSM module initialized");
// Connect to GSM network
connectGSM();
}
void loop() {
// Read GPS data
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
}
if (gps.location.isUpdated()) {
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
}
// Read accelerometer data
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Check for fall detection (example threshold)
if (a.acceleration.z < -9.0) {
Serial.println("Fall detected!");
sendSMS("Fall detected! Location: " + String(gps.location.lat(), 6) + ", " + String(gps.location.lng(), 6));
}
delay(1000);
}
void connectGSM() {
gsmSerial.println("AT");
delay(1000);
gsmSerial.println("AT+CPIN?");
delay(1000);
gsmSerial.println("AT+CREG?");
delay(1000);
gsmSerial.println("AT+CGATT?");
delay(1000);
gsmSerial.println("AT+CSTT=\"" + String(apn) + "\",\"" + String(user) + "\",\"" + String(pass) + "\"");
delay(1000);
gsmSerial.println("AT+CIICR");
delay(1000);
gsmSerial.println("AT+CIFSR");
delay(1000);
}
void sendSMS(String message) {
gsmSerial.println("AT+CMGF=1"); // Set SMS to text mode
delay(1000);
gsmSerial.println("AT+CMGS=\"" + String(phone_number) + "\"");
delay(1000);
gsmSerial.print(message);
delay(1000);
gsmSerial.write(26); // ASCII code for CTRL+Z to send the SMS
delay(1000);
}
InvenSense MPU6050 Code
// Pseudo code for accident detection
if (abs(acceleration.x) > threshold || abs(acceleration.y) > threshold || abs(acceleration.z) > threshold) {
accident_detected = true;
}
Neo 6M GPS Module Code
double calculateDistance(lat1, lon1, lat2, lon2) {
// Implement Haversine formula to calculate distance
double dLat = (lat2 - lat1) * DEG_TO_RAD;
double dLon = (lon2 - lon1) * DEG_TO_RAD;
// Compute the distance using earth radius and return value
return distance;
}
sim 800l Code
gsm.sendSMS("+1234567890", "Accident Detected! Location: https://maps.google.com/?q=" + String(latitude) + "," + String(longitude));
This documentation provides an overview of the circuit, including a description of each component, wiring details, and the embedded code for the microcontrollers. The code snippets are intended to illustrate the functionality of the system, such as GPS data handling, accelerometer data processing, and GSM communication for sending SMS alerts.