Thursday, 28 May 2026

AI-Powered Driver Drowsiness Detection and Accident Prevention System

๐Ÿš— AI-Powered Driver Drowsiness Detection & Accident Prevention System (ESP32 + IoT + n8n + Telegram + Google Sheets + ThingSpeak + AI Agentic Workflow)
Below is a complete, structured, step-by-step documentation for your project: ๐Ÿš— AI-Powered Driver Drowsiness Detection & Accident Prevention System (ESP32 + IoT + n8n + Telegram + Google Sheets + ThingSpeak + AI Agentic Workflow) 1. ๐Ÿ“Œ Project Overview This system detects driver drowsiness in real time using AI and sensors and triggers instant multi-channel alerts using: ๐Ÿ“ฑ Telegram (text + voice alerts) ๐Ÿ“Š Google Sheets (logging) ☁️ ThingSpeak (IoT dashboard) ๐Ÿ”„ n8n automation workflows (AI agentic orchestration) ๐Ÿค– ESP32 microcontroller (edge device) ๐ŸŽฏ Goal: Prevent road accidents by detecting: Eye closure (PERCLOS method or IR sensor) Head tilt / nodding Sudden inactivity / fatigue patterns 2. ๐Ÿงฐ Components List Hardware ESP32 Dev Board IR Eye Blink Sensor / Camera module (OV2640 optional) MPU6050 (Accelerometer + Gyroscope) Buzzer (Alarm) Vibration motor (optional) OLED Display (optional) Power bank / 5V supply Software / Cloud n8n Automation Server (self-hosted or cloud) Telegram Bot API Google Sheets API ThingSpeak IoT Cloud Python (optional AI processing) Arduino IDE / PlatformIO 3. ⚡ System Architecture Sensors (Eye + Head movement) ↓ ESP32 (Edge Processing) ↓ WiFi n8n Webhook Trigger ↓ ┌───────────────┬────────────────┬─────────────────┐ │ Telegram Bot │ Google Sheets │ ThingSpeak │ │ Voice Alerts │ Data Logging │ Dashboard │ └───────────────┴────────────────┴─────────────────┘ ↓ AI Agent (n8n / Python) ↓ Risk Score + Alert Decision 4. ๐Ÿ”„ Flowchart (System Logic) START ↓ Read Eye Blink + Head Movement ↓ Compute Drowsiness Score ↓ Is Score > Threshold? ├── NO → Continue Monitoring └── YES → ↓ Trigger ESP32 Alarm ↓ Send data to n8n webhook ↓ AI Agent evaluates risk ↓ Send: → Telegram message + voice alert → Google Sheets log entry → ThingSpeak update END LOOP 5. ๐Ÿ”Œ Circuit Schematic (Connections) ESP32 Pin Mapping IR Eye Sensor VCC → 3.3V GND → GND OUT → GPIO 34 MPU6050 VCC → 3.3V GND → GND SDA → GPIO 21 SCL → GPIO 22 Buzzer → GPIO 25 → GND Vibration Motor (optional) GPIO 26 → transistor → motor 6. ๐Ÿ’ป ESP32 Source Code (Arduino) #include #include #include #define EYE_SENSOR 34 #define BUZZER 25 const char* ssid = "YOUR_WIFI"; const char* password = "YOUR_PASS"; String n8n_url = "https://your-n8n-webhook-url"; void setup() { Serial.begin(115200); pinMode(EYE_SENSOR, INPUT); pinMode(BUZZER, OUTPUT); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } } void loop() { int eyeValue = analogRead(EYE_SENSOR); Serial.println(eyeValue); if (eyeValue < 1000) { // drowsy threshold example digitalWrite(BUZZER, HIGH); sendToN8N(eyeValue); } else { digitalWrite(BUZZER, LOW); } delay(500); } void sendToN8N(int value) { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin(n8n_url); http.addHeader("Content-Type", "application/json"); String payload = "{\"eye_status\":" + String(value) + "}"; http.POST(payload); http.end(); } } 7. ๐Ÿ”„ n8n Workflow (JSON) Workflow Steps: Webhook trigger AI Function node Telegram node Google Sheets node HTTP request to ThingSpeak { "nodes": [ { "name": "Webhook", "type": "n8n-nodes-base.webhook" }, { "name": "AI Risk Analyzer", "type": "n8n-nodes-base.function", "parameters": { "functionCode": "const val = $json.eye_status;\nreturn [{ risk: val < 1000 ? 'HIGH' : 'LOW' }];" } } ] } (Full n8n workflows can be extended with drag-and-drop UI) 8. ๐Ÿค– Telegram Bot Setup Step 1: Create Bot Open Telegram → search: BotFather Send: /newbot Get API token Step 2: Get Chat ID Search: @userinfobot Copy chat ID Step 3: n8n Telegram Node Bot Token → paste API key Chat ID → your ID Message Example: ๐Ÿšจ Drowsiness Alert! Driver fatigue detected. Immediate attention required. 9. ๐Ÿ“Š Google Sheets Integration Steps: Open Google Sheets Create columns: Time | Eye Value | Risk Level | Action In Google Cloud: Enable Google Sheets API Create Service Account Share sheet with service email In n8n: Add “Google Sheets Node” Append row on trigger 10. ☁️ ThingSpeak Setup Step 1: Create channel: Field1 → Eye value Field2 → Risk level Step 2: API Key from: Channel → API Keys ESP32 / n8n send: https://api.thingspeak.com/update?api_key=XXXX&field1=value 11. ๐Ÿง  AI Drowsiness Detection Logic Method 1 (Simple Threshold) Eye sensor value < threshold → drowsy Method 2 (Better AI Model) Train ML model using: Eye blink rate Eye closure duration Head tilt angle Example Model: Logistic Regression Random Forest Lightweight TensorFlow Lite (ESP32-compatible) 12. ๐Ÿ”Š Voice Notification System Method: Use Telegram voice message or TTS API Option A (n8n + TTS API) Convert text → speech file Send as audio in Telegram Option B: Telegram voice message node 13. ⚙️ Automation Logic (n8n AI Agent Style) IF risk = HIGH: → Send Telegram alert → Trigger buzzer via ESP32 webhook → Log to Google Sheets → Update ThingSpeak → Send voice alert ELSE: → Only log data 14. ๐Ÿš€ Future Enhancements Advanced AI: CNN-based eye detection (camera module) Real-time face landmark detection (MediaPipe) Vehicle Integration: CAN bus integration Automatic braking system trigger Cloud AI: AWS IoT Core integration Azure Digital Twin model Smart Features: Driver identity recognition Fatigue prediction over time Accident prediction model 15. ๐Ÿ“ฆ Deployment Guide Step 1 Upload ESP32 code Step 2 Deploy n8n server (Docker recommended) Step 3 Connect Telegram bot Step 4 Enable Google Sheets API Step 5 Create ThingSpeak dashboard Step 6 Test full pipeline: Simulate eye closure Check alerts flow 16. ๐Ÿงช Testing Checklist ESP32 connects to WiFi ✔ Eye sensor reading changes ✔ Buzzer triggers ✔ n8n receives webhook ✔ Telegram alert sent ✔ Google Sheets logs ✔ ThingSpeak updates ✔ 17. ๐Ÿ“Š Final System Summary Feature Technology Edge device ESP32 Detection IR / ML / MPU6050 Automation n8n Messaging Telegram Bot Data logging Google Sheets Cloud IoT ThingSpeak AI logic Rule-based / ML If you want, I can also next provide: ✅ 1. ๐Ÿงฉ Full Circuit Diagram (Text + Image Description) ๐Ÿ”Œ Final Hardware Architecture ┌────────────────────┐ │ ESP32 DevKit │ └─────────┬──────────┘ │ ┌────────────────────┼────────────────────┐ │ │ │ ┌────▼────┐ ┌──────▼──────┐ ┌─────▼─────┐ │ IR Eye │ │ MPU6050 │ │ Buzzer │ │ Sensor │ │ Gyro+Accel │ │ Alert │ └────┬────┘ └──────┬──────┘ └─────┬─────┘ │ │ │ GPIO34 GPIO21/22 GPIO25 WiFi (Internet) │ ▼ ┌──────────────────────────┐ │ n8n Server │ └─────────┬────────────────┘ │ ┌──────────────┼───────────────┐ ▼ ▼ ▼ Telegram Bot Google Sheets ThingSpeak (Alert + Voice) Logging Dashboard ๐Ÿ–ผ️ If you want real diagram image: I can generate a clean PNG/SVG circuit diagram (professional Fritzing style). Just say: ๐Ÿ‘‰ “Make PCB-style diagram” ✅ 2. ๐Ÿ”„ n8n Workflow (READY IMPORT JSON) Save this as: drowsiness_n8n_workflow.json ๐Ÿ“ฅ FULL IMPORTABLE WORKFLOW { "name": "Driver Drowsiness Detection System", "nodes": [ { "parameters": { "path": "drowsiness", "method": "POST" }, "name": "Webhook", "type": "n8n-nodes-base.webhook", "typeVersion": 1, "position": [250, 300] }, { "parameters": { "functionCode": "const eye = $json.eye_status;\nconst risk = eye < 1000 ? 'HIGH' : 'LOW';\nreturn [{ eye, risk }];" }, "name": "AI Risk Engine", "type": "n8n-nodes-base.function", "typeVersion": 1, "position": [500, 300] }, { "parameters": { "chatId": "YOUR_CHAT_ID", "text": "๐Ÿšจ Drowsiness Alert!\nRisk Level: {{$json.risk}}\nEye Value: {{$json.eye}}" }, "name": "Telegram Alert", "type": "n8n-nodes-base.telegram", "typeVersion": 1, "position": [750, 200] }, { "parameters": { "operation": "append", "sheetId": "YOUR_SHEET_ID", "range": "Sheet1!A:D", "data": "={{[$now, $json.eye, $json.risk, 'Alert Sent']}}" }, "name": "Google Sheets Logger", "type": "n8n-nodes-base.googleSheets", "typeVersion": 1, "position": [750, 400] }, { "parameters": { "url": "https://api.thingspeak.com/update", "method": "GET", "queryParametersUi": { "parameter": [ { "name": "api_key", "value": "YOUR_API_KEY" }, { "name": "field1", "value": "={{$json.eye}}" } ] } }, "name": "ThingSpeak Update", "type": "n8n-nodes-base.httpRequest", "typeVersion": 1, "position": [1000, 300] } ], "connections": { "Webhook": { "main": [[{"node": "AI Risk Engine", "type": "main", "index": 0}]] }, "AI Risk Engine": { "main": [ [{"node": "Telegram Alert", "type": "main", "index": 0}], [{"node": "Google Sheets Logger", "type": "main", "index": 0}], [{"node": "ThingSpeak Update", "type": "main", "index": 0}] ] } } } ✅ 3. ๐Ÿง  Advanced ML Model (Python + Dataset) ๐Ÿ“Š Dataset Structure driver_data.csv eye_closure blink_rate head_tilt drowsy 0.2 18 5 0 0.8 5 25 1 ๐Ÿค– Training Model (Random Forest) import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier import joblib # Load dataset data = pd.read_csv("driver_data.csv") X = data[['eye_closure', 'blink_rate', 'head_tilt']] y = data['drowsy'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) model = RandomForestClassifier(n_estimators=100) model.fit(X_train, y_train) accuracy = model.score(X_test, y_test) print("Accuracy:", accuracy) joblib.dump(model, "drowsiness_model.pkl") ๐Ÿ”ฎ Real-time Prediction (ESP32 backend or server) import joblib import numpy as np model = joblib.load("drowsiness_model.pkl") def predict(eye, blink, tilt): data = np.array([[eye, blink, tilt]]) result = model.predict(data) return "DROWSY" if result[0] == 1 else "NORMAL" ✅ 4. ๐Ÿ“ฑ Mobile Dashboard (Flutter Alternative) ๐Ÿš€ Option A: Flutter App Features: Live drowsiness status Graph (ThingSpeak API) Alert history Driver score ๐Ÿ“ฑ Flutter UI Code (Basic) import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Dashboard(), ); } } class Dashboard extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Driver Monitor")), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text("Drowsiness Status: NORMAL", style: TextStyle(fontSize: 20)), SizedBox(height: 20), ElevatedButton( onPressed: () {}, child: Text("Refresh Data"), ) ], ), ), ); } } ๐Ÿš€ Option B: No-Code Dashboard (Recommended) Use: Blynk IoT ThingSpeak IoT Platform Features: Drag-drop widgets Live graph Mobile alerts Easy ESP32 integration ✅ 5. ๐Ÿ—‚️ GitHub-Ready Project Structure driver-drowsiness-ai-iot/ │ ├── esp32/ │ ├── drowsiness.ino │ ├── n8n-workflow/ │ ├── drowsiness_n8n_workflow.json │ ├── ml-model/ │ ├── train_model.py │ ├── predict.py │ ├── driver_data.csv │ ├── drowsiness_model.pkl │ ├── flutter-app/ │ ├── lib/ │ │ ├── main.dart │ ├── docs/ │ ├── circuit_diagram.png │ ├── system_architecture.png │ ├── flowchart.png │ ├── cloud-setup/ │ ├── thingspeak_guide.md │ ├── telegram_setup.md │ ├── google_sheets_setup.md │ ├── README.md └── LICENSE ๐Ÿ“„ README.md (Sample) # AI Driver Drowsiness Detection System Tech Stack: - ESP32 - n8n Automation - Telegram Bot - ThingSpeak IoT - Google Sheets - Machine Learning (Random Forest) Features: - Real-time drowsiness detection - AI-powered risk prediction - Multi-channel alerts - Cloud dashboard monitoring Setup: 1. Upload ESP32 code 2. Import n8n workflow 3. Configure Telegram bot 4. Connect Google Sheets 5. Enable ThingSpeak channel

No comments:

Post a Comment

AI-Powered Home Automation Using Voice and Face Recognition

๐Ÿ  AI-Powered Home Automation Using Voice & Face Recognition (ESP32 + Agentic IoT + n8n + Telegram + Google Sheets + ThingSpeak) ๐Ÿ  AI-...