A complete IoT-based accident detection and emergency alert system.
The ESP32 detects crashes using the MPU6050 accelerometer and pushes data to
Firebase Realtime Database. The Flutter app monitors in real time and dispatches
GPS-tagged emergency SMS alerts when a crash is detected.
smart_helmet_app/
โโโ lib/
โ โโโ main.dart # App entry point
โ โโโ firebase_options.dart # โ ๏ธ Replace with your own
โ โโโ theme/
โ โ โโโ app_theme.dart # Design system (colors, gradients, shadows)
โ โโโ models/
โ โ โโโ helmet_data.dart # Firebase data model + SystemState enum
โ โ โโโ emergency_contact.dart # Contact model
โ โโโ services/
โ โ โโโ firebase_service.dart # Realtime DB stream & writes
โ โ โโโ location_service.dart # Geolocator wrapper
โ โ โโโ emergency_service.dart # SMS dispatch + call launcher
โ โ โโโ contacts_service.dart # SharedPreferences contact storage
โ โ โโโ notification_service.dart # Local push notifications
โ โโโ screens/
โ โ โโโ splash_screen.dart # Animated boot screen
โ โ โโโ dashboard_screen.dart # Main real-time monitoring view
โ โ โโโ crash_alert_screen.dart # Full-screen red crash alert
โ โ โโโ emergency_contacts_screen.dart
โ โ โโโ settings_screen.dart
โ โโโ widgets/
โ โโโ sensor_card.dart # ax / ay / az display cards
โ โโโ status_indicator.dart # Pulsing status dots
โ โโโ acceleration_chart.dart # fl_chart line chart
โโโ android/ # Android native project
โ โโโ app/
โ โ โโโ build.gradle
โ โ โโโ src/main/
โ โ โโโ AndroidManifest.xml
โ โ โโโ kotlin/.../MainActivity.kt
โ โโโ build.gradle
โ โโโ settings.gradle
โโโ esp32_firmware/
โ โโโ smart_helmet_firmware.ino # ESP32 Arduino sketch
โโโ pubspec.yaml
- Go to Firebase Console
- Create a new project (e.g.,
smart-helmet) - Enable Realtime Database (Start in test mode)
- Set database rules:
{
"rules": {
"smart_helmet": {
".read": true,
".write": true
}
}
}- Register an Android app with package name:
com.example.smart_helmet_app - Download
google-services.jsonโ place it at:android/app/google-services.json
Install the FlutterFire CLI and configure:
dart pub global activate flutterfire_cli
cd smart_helmet_app
flutterfire configure --project=YOUR_PROJECT_IDThis auto-generates lib/firebase_options.dart with your real credentials.
Replace the placeholder file already in the project.
flutter pub getThe AndroidManifest.xml already includes:
INTERNETโ Firebase connectionACCESS_FINE_LOCATIONโ GPS for crash locationSEND_SMSโ Emergency alertsCALL_PHONEโ Direct callingPOST_NOTIFICATIONSโ Push alerts (Android 13+)USE_FULL_SCREEN_INTENTโ Crash alert fullscreen
flutter run --releaseOr open in Android Studio โ Run โถ
| Component | ESP32 Pin |
|---|---|
| MPU6050 SDA | GPIO 21 |
| MPU6050 SCL | GPIO 22 |
| MPU6050 VCC | 3.3V |
| MPU6050 GND | GND |
| IR Sensor OUT | GPIO 34 |
| Buzzer + | GPIO 25 |
| LED Green | GPIO 26 |
| LED Red | GPIO 27 |
| Push Button | GPIO 32 |
Install via Arduino Library Manager:
Firebase_ESP_Clientby Mobizt (v4.x)MPU6050_lightby rfetickArduinoJsonby bblanchon
Edit esp32_firmware/smart_helmet_firmware.ino:
#define WIFI_SSID "YOUR_WIFI_NAME"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
#define FIREBASE_HOST "https://YOUR_PROJECT_ID-default-rtdb.firebaseio.com/"
#define FIREBASE_API_KEY "YOUR_WEB_API_KEY"
#define USER_EMAIL "esp32@smarthelmet.com"
#define USER_PASSWORD "your_password"Create a Firebase user for the ESP32:
- Firebase Console โ Authentication โ Add User
- Use the same email/password configured above
- Open
smart_helmet_firmware.inoin Arduino IDE - Select Board: ESP32 Dev Module
- Select correct COM port
- Upload โถ
smart_helmet/
โโโ data/
โโโ ax : -19.6 โ 19.6 (float, m/sยฒ)
โโโ ay : -19.6 โ 19.6 (float, m/sยฒ)
โโโ az : -19.6 โ 19.6 (float, m/sยฒ)
โโโ impact_force : 0.0 โ 40.0 (float, G)
โโโ helmet_worn : true / false (bool)
โโโ system_state : "monitoring" | "impact_detected" | "crash_confirmed"
โโโ crash_detected : true / false (bool โ triggers app alert)
โโโ esp32_online : true / false (bool โ connection status)
โโโ last_updated : 1711234567890 (int, Unix ms)
The Flutter app streams /smart_helmet/data in real time.
When crash_detected flips to true, the crash alert screen launches instantly.
| Feature | Description |
|---|---|
| ๐ด Real-time Dashboard | ax, ay, az, impact force, system state |
| ๐ก๏ธ Helmet Status | IR sensor โ worn / not worn |
| ๐ก ESP32 Status | Online / offline indicator |
| ๐จ Crash Alert Screen | Full-screen red UI with 30s countdown |
| ๐ GPS Location | Geolocator fetches live phone GPS on crash |
| ๐ฑ SMS Alert | Sends location + Google Maps link to all contacts |
| ๐ Emergency Call | One-tap call to contacts or 112 |
| โ Cancel Alert | User can cancel within countdown window |
| ๐ Acceleration Chart | Live fl_chart line graph (40-point history) |
| ๐ฅ Contact Management | Add/edit/delete emergency contacts |
| โ๏ธ Settings | Permissions, countdown duration, notifications |
| ๐งช Test Mode | Simulate crash alert without real event |
G-force reading per loop:
gMag = โ(axยฒ + ayยฒ + azยฒ) / 9.81
State Machine:
MONITORING โ if gMag โฅ 2.5G AND helmet worn โ IMPACT_DETECTED
IMPACT_DETECTED โ if gMag < 2.5G โ MONITORING (false alarm)
IMPACT_DETECTED โ if persists โฅ 2000ms โ CRASH_CONFIRMED
CRASH_CONFIRMED โ Firebase crash_detected = true โ App alert fires
CRASH_CONFIRMED โ button press โ reset to MONITORING
ESP32 Hardware
โ (MPU6050, IR, Buzzer, LEDs)
โ WiFi + Firebase SDK
โผ
Firebase Realtime Database
โ (smart_helmet/data node)
โ StreamBuilder / onValue
โผ
Flutter App (Android)
โโ FirebaseService โ real-time data stream
โโ DashboardScreen โ display sensor readings
โโ CrashAlertScreen โ red alert UI + countdown
โโ LocationService โ Geolocator GPS
โโ EmergencyService โ SMS + call dispatch
โโ ContactsService โ SharedPreferences storage
| Package | Purpose |
|---|---|
firebase_core |
Firebase initialization |
firebase_database |
Realtime Database stream |
geolocator |
Phone GPS location |
permission_handler |
Runtime permissions |
url_launcher |
SMS / phone call intents |
shared_preferences |
Local contact storage |
google_fonts |
Space Grotesk typography |
fl_chart |
Acceleration line chart |
flutter_local_notifications |
Local push notifications |
intl |
Date/time formatting |
- Firebase Cloud Messaging (FCM) for background push notifications
- Firebase Authentication for multi-user support
- Crash history log with timeline view
- Helmet battery level monitoring (voltage divider)
- Speed tracking via phone GPS during ride
- Geofencing and route recording
- Wear detection via capacitive touch sensor
- BLE connection as backup to WiFi
MIT License โ Free to use and modify for personal and educational projects.