AI-Based Vehicle Speed Monitoring & Automatic Challan System
ESP32 + IoT + AI Agent + n8n + Telegram Voice Alerts + Google Sheets + ThingSpeak
1. Project Overview
This project is an AI-powered smart traffic monitoring system using ESP32, sensors, cloud dashboard, automation workflows, and Telegram alerts.
- Vehicle Speed Detection
- Automatic Challan Generation
- Telegram Notifications
- Voice Alerts
- Google Sheets Logging
- ThingSpeak Cloud Dashboard
- AI Power Consumption Prediction
2. Components List
| Component | Quantity | Purpose |
|---|---|---|
| ESP32 | 1 | Main Controller |
| IR Sensors | 2 | Vehicle Detection |
| Buzzer | 1 | Alert Sound |
| OLED Display | 1 | Speed Display |
| LEDs | 2 | Status Indicators |
3. Working Principle
Two IR sensors are placed at a fixed distance. When a vehicle crosses the first sensor, timer starts. When it crosses the second sensor, timer stops.
Speed Formula
Speed = Distance / Time
Speed(km/h) = (Distance / Time) × 3.6
4. Circuit Connections
| Component | ESP32 Pin |
|---|---|
| IR Sensor 1 | GPIO 14 |
| IR Sensor 2 | GPIO 27 |
| Buzzer | GPIO 26 |
| Green LED | GPIO 25 |
| Red LED | GPIO 33 |
5. System Flowchart
START
↓
Initialize ESP32
↓
Connect WiFi
↓
Detect Vehicle
↓
Calculate Speed
↓
Speed > Limit?
↓
YES
↓
Send Alert to n8n
↓
Telegram Notification
↓
Voice Alert
↓
Google Sheets Update
↓
ThingSpeak Upload
↓
END
6. ESP32 Source Code
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";
String webhook = "YOUR_N8N_WEBHOOK_URL";
#define SENSOR1 14
#define SENSOR2 27
unsigned long startTime;
unsigned long endTime;
float distanceMeters = 1.0;
bool trigger = false;
void setup() {
Serial.begin(115200);
pinMode(SENSOR1, INPUT);
pinMode(SENSOR2, INPUT);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Connecting...");
}
Serial.println("WiFi Connected");
}
void loop() {
if(digitalRead(SENSOR1)==LOW && !trigger){
startTime = millis();
trigger = true;
}
if(digitalRead(SENSOR2)==LOW && trigger){
endTime = millis();
float timeSec = (endTime - startTime)/1000.0;
float speed = (distanceMeters/timeSec)*3.6;
Serial.println(speed);
if(speed > 40){
sendData(speed);
}
trigger = false;
}
}
void sendData(float speed){
HTTPClient http;
http.begin(webhook);
http.addHeader("Content-Type","application/json");
String data = "{\"speed\":\""+String(speed)+"\"}";
http.POST(data);
http.end();
}
7. n8n Workflow
Webhook ↓ Check Speed Limit ↓ Telegram Alert ↓ Voice Notification ↓ Google Sheets Update ↓ ThingSpeak Upload
8. Telegram Bot Setup
- Open Telegram
- Search BotFather
- Create new bot using /newbot
- Copy Bot Token
- Get Chat ID
9. Google Sheets Integration
| Time | Speed | Fine | Status |
|---|---|---|---|
| 10:30 AM | 72 km/h | ₹1000 | Overspeed |
10. ThingSpeak Cloud Dashboard
Upload sensor data to ThingSpeak cloud dashboard for:
- Real-Time Speed Monitoring
- Traffic Analytics
- Power Consumption Tracking
- Violation Statistics
11. AI Power Consumption Prediction
Predicted Power = (sensor_time × current) + (wifi_time × current)
AI predicts traffic load and controls ESP32 sleep mode for power optimization.
12. Voice Notification Automation
Telegram voice alerts are generated using:
- Google Text-to-Speech
- ElevenLabs API
Warning! Overspeed vehicle detected. Speed exceeded legal limit. Automatic challan generated.
13. Automatic Challan Logic
| Speed Range | Fine Amount |
|---|---|
| 40-60 km/h | ₹500 |
| 60-80 km/h | ₹1000 |
| 80+ km/h | ₹2000 |
14. Future Enhancements
- Number Plate Recognition
- ESP32-CAM Integration
- AI Traffic Prediction
- Smart City Dashboard
- Cloud AI Analytics
- GPS Tracking
15. Deployment Guide
- Install sensors roadside
- Connect ESP32 to WiFi
- Deploy n8n workflow
- Configure Telegram bot
- Connect Google Sheets
- Setup ThingSpeak dashboard
- Test vehicle detection
16. Estimated Project Cost
| Item | Cost |
|---|---|
| ESP32 | ₹500 |
| Sensors | ₹300 |
| Display | ₹250 |
| Miscellaneous | ₹500 |
Total Cost: ₹1500 - ₹2500
17. Conclusion
This AI-powered IoT project combines ESP32, automation workflows, Telegram notifications, AI analytics, and cloud dashboards to create an intelligent traffic monitoring and automatic challan system for smart cities.

Comments
Post a Comment