A comprehensive fall detection system using ESP32 wristband sensors, cloud-based AI prediction, and real-time mobile notifications.
This system detects falls using a wristband (ESP32 + MPU6050) and notifies caregivers in real-time through a mobile app. The system uses a trained AI model deployed in the cloud to analyze sensor data and determine if a fall has occurred.
ESP32 Device β Flask API β AI Model β Firebase β Mobile App
- ESP32 collects accelerometer + gyroscope data from MPU6050
- When suspicious activity is detected, ESP32 sends data via HTTP POST to cloud API
- Cloud API loads pre-trained ML model (RandomForest/MLP) and predicts if a fall occurred
- If fall is confirmed β API triggers Firebase Cloud Messaging (FCM) push notification
- Mobile App (Flutter) receives notification even when closed/backgrounded
- App shows fall event history stored in Firestore
fall-detection-system/
βββ backend/ # Flask API server
β βββ main.py # Main Flask application
β βββ requirements.txt # Python dependencies
β βββ .env.example # Environment variables template
β βββ Procfile # Deployment configuration
βββ mobile_app/ # Flutter mobile application
β βββ lib/
β β βββ models/ # Data models
β β βββ services/ # Firebase service
β β βββ screens/ # UI screens
β β βββ widgets/ # Reusable UI components
β β βββ main.dart # App entry point
β βββ pubspec.yaml # Flutter dependencies
βββ README.md # This file
- Device: ESP32 (Wi-Fi), MPU6050 accelerometer/gyroscope
- Backend: Python + Flask, joblib (ML model), Firebase Admin SDK
- Database: Firebase Firestore (event logs & device tokens)
- Notifications: Firebase Cloud Messaging (FCM)
- Mobile App: Flutter (cross-platform, Android/iOS)
- Hosting: Free tier cloud hosting (Render/Railway) + Firebase free tier
-
Navigate to backend directory:
cd backend -
Install dependencies:
pip install -r requirements.txt
-
Configure Firebase:
- Create a Firebase project at https://console.firebase.google.com/
- Generate a service account key (Settings β Service accounts β Generate new private key)
- Copy
.env.exampleto.envand add your Firebase service account key
-
Run the server:
python main.py
-
Navigate to mobile app directory:
cd mobile_app -
Install Flutter dependencies:
flutter pub get
-
Configure Firebase:
- Add your Firebase configuration files (see
firebase-setup.md) - Update Firebase project settings in the app
- Add your Firebase configuration files (see
-
Run the app:
flutter run
- β
/predict- Accepts sensor data and returns fall prediction - β
/register-device- Registers mobile devices for notifications - β
/health- Health check endpoint - β
/fall-events- Retrieves fall event history - β Automatic Firestore logging of all events
- β Real-time FCM notifications for fall alerts
- β Real-time fall event monitoring
- β Background notification handling (app closed/minimized)
- β Local notifications with sound and vibration
- β Fall event history with timestamps
- β Device statistics dashboard
- β Event acknowledgment system
- β Automatic Firebase synchronization
The ESP32 device should send HTTP POST requests to your backend /predict endpoint with this JSON format:
{
"device_id": "esp32_001",
"accelX": 0.123,
"accelY": 0.456,
"accelZ": 0.789,
"gyroX": 1.234,
"gyroY": 5.678,
"gyroZ": 9.012
}Create a .env file in the backend directory:
FIREBASE_SERVICE_ACCOUNT_KEY={"type": "service_account", ...}
FLASK_ENV=production
SECRET_KEY=your-secret-key
PORT=5000-
Enable required services:
- Firestore Database
- Cloud Messaging
- Authentication (optional)
-
Firestore Security Rules:
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /fall_events/{document} { allow read, write: if true; } match /devices/{document} { allow read, write: if true; } } }
- Create a new web service
- Connect your GitHub repository
- Set environment variables:
FIREBASE_SERVICE_ACCOUNT_KEYFLASK_ENV=production
- The service will automatically use the Procfile for deployment
-
Android:
flutter build apk --release
-
iOS:
flutter build ios --release
- ESP32 detects suspicious motion β sends data to API
- API runs ML prediction β if fall detected, stores event in Firestore
- API retrieves FCM tokens from devices collection
- API sends push notification to all registered devices
- Mobile app receives notification (even if closed)
- App shows local notification with sound/vibration
- User can view event details and acknowledge
The system uses a pre-trained ML model (fall_model.pkl) that should be placed in the backend directory. The model expects 6 features:
- accelX, accelY, accelZ (accelerometer data)
- gyroX, gyroY, gyroZ (gyroscope data)
For testing purposes, a dummy RandomForest model is created automatically if no model file is found.
Analyze sensor data for fall detection.
Request Body:
{
"device_id": "string",
"accelX": "number",
"accelY": "number",
"accelZ": "number",
"gyroX": "number",
"gyroY": "number",
"gyroZ": "number"
}Response:
{
"fall": "boolean",
"confidence": "number",
"event_id": "string",
"timestamp": "string",
"notification_sent": "boolean"
}Register a device for FCM notifications.
Request Body:
{
"device_id": "string",
"fcm_token": "string"
}Retrieve fall event history.
Query Parameters:
limit(optional): Number of events to return (default: 50)device_id(optional): Filter by specific device
- Use HTTPS for all API communication
- Implement proper Firebase security rules
- Store sensitive configuration in environment variables
- Regular security updates for all dependencies
- Consider implementing API authentication for production use
-
Firebase initialization fails:
- Check service account key format
- Verify Firebase project configuration
- Ensure required Firebase services are enabled
-
Notifications not received:
- Check FCM token registration
- Verify notification permissions
- Test with Firebase console
-
App crashes on startup:
- Run
flutter clean && flutter pub get - Check Firebase configuration files
- Verify minimum SDK versions
- Run
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support, please create an issue in the GitHub repository or contact the development team.
Note: This system is designed for monitoring purposes and should not be used as the sole method of fall detection for critical safety applications. Always have backup safety measures in place.