Skip to content

Mayank2142/Smart_Helmet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿช– Smart Helmet โ€” Flutter + Firebase + ESP32

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.


๐Ÿ“ Project Structure

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

๐Ÿš€ Setup Instructions

Step 1 โ€” Firebase Project

  1. Go to Firebase Console
  2. Create a new project (e.g., smart-helmet)
  3. Enable Realtime Database (Start in test mode)
  4. Set database rules:
{
  "rules": {
    "smart_helmet": {
      ".read": true,
      ".write": true
    }
  }
}
  1. Register an Android app with package name: com.example.smart_helmet_app
  2. Download google-services.json โ†’ place it at:
    android/app/google-services.json
    

Step 2 โ€” Flutter Firebase Options

Install the FlutterFire CLI and configure:

dart pub global activate flutterfire_cli
cd smart_helmet_app
flutterfire configure --project=YOUR_PROJECT_ID

This auto-generates lib/firebase_options.dart with your real credentials.
Replace the placeholder file already in the project.

Step 3 โ€” Install Flutter Dependencies

flutter pub get

Step 4 โ€” Android Permissions

The AndroidManifest.xml already includes:

  • INTERNET โ€” Firebase connection
  • ACCESS_FINE_LOCATION โ€” GPS for crash location
  • SEND_SMS โ€” Emergency alerts
  • CALL_PHONE โ€” Direct calling
  • POST_NOTIFICATIONS โ€” Push alerts (Android 13+)
  • USE_FULL_SCREEN_INTENT โ€” Crash alert fullscreen

Step 5 โ€” Run on Android Device

flutter run --release

Or open in Android Studio โ†’ Run โ–ถ


๐Ÿ”Œ ESP32 Firmware Setup

Hardware Connections

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

Arduino Library Dependencies

Install via Arduino Library Manager:

  • Firebase_ESP_Client by Mobizt (v4.x)
  • MPU6050_light by rfetick
  • ArduinoJson by bblanchon

Configure Firmware

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

Upload Firmware

  1. Open smart_helmet_firmware.ino in Arduino IDE
  2. Select Board: ESP32 Dev Module
  3. Select correct COM port
  4. Upload โ–ถ

๐Ÿ“ก Firebase Realtime Database Schema

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.


๐Ÿ“ฑ App Features

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

๐Ÿ”ง Crash Detection Algorithm (ESP32)

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

๐Ÿ—๏ธ Architecture

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

๐Ÿ“ฆ Flutter Dependencies

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

๐Ÿ”ฎ Future Enhancements

  • 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

๐Ÿ“„ License

MIT License โ€” Free to use and modify for personal and educational projects.

About

An IoT-based smart helmet system using ESP32, MPU6050 & Firebase for real-time crash detection. Auto-sends GPS-tagged emergency SMS alerts to saved contacts. Features live accelerometer dashboard, IR wear detection to prevent false alarms, and a configurable countdown with bi-directional cloud sync.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors