AI Surveillance System for Helmet & Triple-Riding Detection
Complete project documentation: ESP32 + AI + IoT + n8n + Telegram + Google Sheets + ThingSpeak
This project is an AI-powered traffic surveillance and IoT monitoring system that detects two-wheeler riders who are not wearing helmets and motorcycles carrying more than two people. It combines computer vision, an ESP32-based IoT device, an automation workflow in n8n, Telegram voice notifications, Google Sheets incident logging, and a ThingSpeak cloud dashboard.
The system can be developed as a college major project, engineering prototype, smart-city demonstration, or traffic-safety monitoring platform.
One important design decision: the camera and AI model should perform the actual helmet and rider detection; the ESP32 should handle IoT sensing, event transmission, and device control. A conventional ESP32 is not powerful enough to run a typical full-size traffic-detection model reliably. An ESP32-CAM may be used for image capture, while a laptop, edge AI computer, or server runs the vision model.
1. Project overview
AI traffic detection
Detect motorcycles, riders, helmets, and the number of people on each motorcycle using a camera and a computer-vision model.ESP32 and IoT webpage
Receive detection events over Wi-Fi, show device connectivity, monitor system status, and optionally activate a buzzer or warning light.Agentic automation with n8n
Process incoming events, apply alert rules, send Telegram notifications, update cloud records, and handle errors.Cloud monitoring and reports
Store incident records in Google Sheets and display numerical IoT data and trends in ThingSpeak.Project objectives
-
Identify riders who appear to be wearing or not wearing helmets.
-
Detect motorcycles carrying three or more people.
-
Generate timestamped traffic-safety events.
-
Transmit event data through Wi-Fi using an ESP32 or an associated edge device.
-
Automate alerts through n8n.
-
Send Telegram text and optional spoken audio alerts.
-
Maintain an incident log in Google Sheets.
-
Display detection statistics and device status in ThingSpeak.
-
Provide a local or web-based monitoring interface.
-
Support testing, event review, and system-performance evaluation.
Important limitation: This is a traffic-safety monitoring prototype, not a certified enforcement device. Camera angles, occlusion, lighting, crowded scenes, and imperfect AI predictions can cause false detections. Do not automatically issue penalties based solely on the model's output.
2. System architecture
The recommended implementation uses four layers:
-
Perception layer: camera, AI model, motorcycle tracking, helmet classification, rider counting.
-
IoT layer: ESP32, Wi-Fi connectivity, status webpage, optional buzzer/LED.
-
Automation layer: n8n webhook, event validation, alert routing, optional AI-generated voice message.
-
Cloud layer: Telegram, Google Sheets, ThingSpeak, and a monitoring webpage.
Architecture flow diagram
flowchart TD
A[Traffic Camera / ESP32-CAM] --> B[Edge Computer or AI Server]
B --> C[AI Detection Model]
C --> D[Motorcycle and Rider Tracking]
D --> E{Safety Rule Engine}
E -->|No helmet| F[Helmet Violation Event]
E -->|3 or more riders| G[Triple Riding Event]
E -->|Both detected| H[Combined Event]
F --> I[ESP32 / Event Gateway]
G --> I
H --> I
I --> J[n8n Webhook]
J --> K[Validate and Deduplicate]
K --> L[Telegram Alert / Voice]
K --> M[Google Sheets Log]
K --> N[ThingSpeak Telemetry]
I --> O[ESP32 IoT Webpage]
N --> P[Cloud Dashboard]
Recommended hardware and software
|
Component |
Purpose |
|---|---|
|
ESP32 DevKit V1 |
Wi-Fi connectivity, IoT status, optional output controls |
|
USB webcam, IP camera, or ESP32-CAM |
Capture traffic video |
|
Laptop, Raspberry Pi-class computer, or edge AI device |
Run the computer-vision model, subject to performance testing |
|
Buzzer and LED |
Optional local warning |
|
Stable power supply |
Power the ESP32 and camera |
|
Wi-Fi router |
Network communication |
|
Python and OpenCV |
Read video and process frames |
|
YOLO-based custom model |
Detect helmets, motorcycles, and riders |
|
Arduino IDE |
Program the ESP32 |
|
n8n |
Automate alerts and cloud updates |
|
Telegram Bot API |
Send notifications and audio files |
|
Google Sheets |
Incident database |
|
ThingSpeak |
Numerical telemetry and charts |
|
HTML/CSS/JavaScript |
Optional browser dashboard |
The AI model needs training or fine-tuning for the classes relevant to your camera view. A general-purpose model that detects people and motorcycles does not automatically establish whether a helmet is being worn correctly.
3. Hardware schematic diagram
The following schematic is a safe baseline for the IoT portion. It does not require the AI camera to be physically connected to the ESP32.
USB / 5V SUPPLY
|
+-----+------+
| ESP32 |
| DevKit V1 |
| |
Wi-Fi Router <| Wi-Fi |
| |
GPIO 26 ------+----[220Ω]----|>|---- GND
| LED
GPIO 27 ------+----[Driver Circuit]---- Buzzer
| |
+------------+
Camera / Webcam ----------> AI Computer
AI Computer --------------> n8n Webhook (HTTPS)
ESP32 --------------------> n8n Webhook (HTTPS)
n8n ----------------------> Telegram
n8n ----------------------> Google Sheets
n8n ----------------------> ThingSpeak
Wiring instructions
-
Connect the LED anode to GPIO 26 through a 220–330 Ω resistor; connect its cathode to GND.
-
Connect an optional buzzer through an appropriate transistor or driver circuit to GPIO 27. Do not drive a high-current buzzer directly from a GPIO.
-
Power the ESP32 from USB or a suitable regulated supply.
-
Connect the camera to the AI computer using its supported USB, Ethernet, Wi-Fi, or video interface.
-
Keep the camera and ESP32 on the same local network if they need to communicate directly.
-
Use a common ground for low-voltage peripherals where required by the circuit.
-
Never connect a 5 V signal directly to an ESP32 GPIO.
Alternative: An ESP32-CAM can capture and stream video, but it should normally act as a camera node rather than run a full traffic-classification model.
4. AI detection logic
The AI subsystem has two independent detection tasks.
A. Helmet detection
The model should recognize a suitable set of classes, for example:
-
motorcycle -
rider -
helmet -
no_helmet
These names are illustrative custom classes, not classes guaranteed to exist in a pretrained model.
A practical processing sequence is:
-
Capture a video frame.
-
Detect motorcycles and riders.
-
Detect helmets and uncovered heads using the trained model.
-
Associate each rider or head with the appropriate motorcycle.
-
Track the same motorcycle across multiple frames.
-
Confirm a helmet event only when the evidence is sufficiently consistent.
-
Record confidence, timestamp, camera ID, and event type.
B. Triple-riding detection
For each tracked motorcycle:
-
Identify the motorcycle bounding box.
-
Identify the people associated with that motorcycle.
-
Count the riders across a stable sequence of frames.
-
Raise a triple-riding event when the count is three or more.
-
Suppress repeated alerts for the same tracked motorcycle during a configured cooldown period.
Do not simply count all people in a frame: pedestrians and people on adjacent motorcycles can cause false positives.
C. Decision logic
def classify_event(helmet_violation, rider_count):
events = []
if helmet_violation:
events.append("NO_HELMET")
if rider_count >= 3:
events.append("TRIPLE_RIDING")
if not events:
return "NORMAL"
return "+".join(events)
This is only the rule-engine logic. The camera model must provide reliable helmet and per-motorcycle rider-count estimates first.
D. Model training workflow
flowchart LR
A[Collect Authorized Images] --> B[Annotate Riders, Helmets, Motorcycles]
B --> C[Split Train / Validation / Test]
C --> D[Train Custom Detection Model]
D --> E[Evaluate Precision and Recall]
E --> F{Acceptable Performance?}
F -->|No| B
F -->|Yes| G[Deploy to Edge Computer]
G --> H[Live Video Inference]
H --> I[Tracking and Event Rules]
A model such as Ultralytics YOLO can provide a starting point for detection and tracking, but the exact model version, dataset, deployment hardware, and license must be selected for your project. See the official YOLO documentation and tracking guide .
5. Software installation and project folder
Install the required tools on the AI computer.
-
Python 3.10 or another version supported by your selected model packages.
-
Arduino IDE with the ESP32 board package.
-
n8n Cloud or a self-hosted n8n instance.
-
A Telegram bot created using BotFather.
-
A Google account for Sheets.
-
A ThingSpeak account and channel.
-
A custom-trained AI model for helmet and rider detection.
Create a project folder with this structure:
helmet-surveillance/
├── ai/
│ ├── detector.py
│ ├── best.pt
│ └── requirements.txt
├── esp32/
│ └── helmet_iot.ino
├── dashboard/
│ └── index.html
├── n8n/
│ └── workflow-design.md
├── data/
│ └── sample_events.json
└── README.md
Install the Python libraries:
python -m venv .venv
# Linux/macOS
source .venv/bin/activate
# Windows PowerShell:
# .venv\Scripts\Activate.ps1
pip install ultralytics opencv-python requests flask
The AI model weights best.pt must be your own compatible trained model. The code below assumes that its class names include motorcycle, rider, helmet, and no_helmet.
6. AI detection and event transmission — Python code
The following is a starter implementation that runs inference on a webcam or video and transmits a sample event to the n8n webhook. It demonstrates the integration interface; production-grade triple-riding detection additionally needs motorcycle-to-rider association, temporal tracking, event deduplication, and validation.
Create ai/detector.py:
import os
import time
import uuid
from datetime import datetime, timezone
import cv2
import requests
from ultralytics import YOLO
MODEL_PATH = os.getenv("MODEL_PATH", "best.pt")
VIDEO_SOURCE = os.getenv("VIDEO_SOURCE", "0")
N8N_WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL", "")
CAMERA_ID = os.getenv("CAMERA_ID", "CAM-01")
CONFIDENCE = 0.45
COOLDOWN_SECONDS = 15
model = YOLO(MODEL_PATH)
last_sent = {}
def send_event(event_type, rider_count, confidence):
event = {
"event_id": str(uuid.uuid4()),
"timestamp": datetime.now(timezone.utc).isoformat(),
"device_id": "AI-GATEWAY-01",
"camera_id": CAMERA_ID,
"event_type": event_type,
"rider_count": int(rider_count),
"confidence": round(float(confidence), 3),
"source": "ai_edge",
"review_status": "pending"
}
if not N8N_WEBHOOK_URL:
print("Webhook not configured; event:", event)
return
try:
response = requests.post(
N8N_WEBHOOK_URL,
json=event,
timeout=5
)
response.raise_for_status()
print("Event accepted:", response.status_code, event)
except requests.RequestException as exc:
print("Webhook delivery failed:", exc)
def main():
source = int(VIDEO_SOURCE) if VIDEO_SOURCE.isdigit() else VIDEO_SOURCE
cap = cv2.VideoCapture(source)
if not cap.isOpened():
raise RuntimeError(f"Cannot open video source: {source}")
while True:
ok, frame = cap.read()
if not ok:
break
result = model.predict(
frame,
conf=CONFIDENCE,
verbose=False
)[0]
# Demonstration only: these counts are global frame counts.
# They are NOT reliable per-motorcycle rider associations.
names = model.names
detected = []
for box in result.boxes:
class_id = int(box.cls.item())
label = str(names[class_id]).lower()
score = float(box.conf.item())
detected.append((label, score))
helmet_violation = any(
label == "no_helmet" for label, _ in detected
)
rider_count = sum(
label == "rider" for label, _ in detected
)
event_type = None
if helmet_violation and rider_count >= 3:
event_type = "NO_HELMET_AND_TRIPLE_RIDING"
elif helmet_violation:
event_type = "NO_HELMET"
elif rider_count >= 3:
event_type = "TRIPLE_RIDING"
# Avoid repeatedly sending the same event every frame.
if event_type:
now = time.monotonic()
if now - last_sent.get(event_type, 0) >= COOLDOWN_SECONDS:
best_conf = max(
(score for label, score in detected
if label in ("no_helmet", "rider")),
default=0.0
)
send_event(event_type, rider_count, best_conf)
last_sent[event_type] = now
cv2.imshow("Helmet Surveillance - Prototype", result.plot())
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Run the detector
Set the environment variables to your actual model, camera, and n8n production webhook URL.
Linux/macOS:
export MODEL_PATH="best.pt"
export VIDEO_SOURCE="0"
export N8N_WEBHOOK_URL="https://YOUR-N8N-HOST/webhook/traffic-event"
python detector.py
Windows PowerShell:
$env:MODEL_PATH="best.pt"
$env:VIDEO_SOURCE="0"
$env:N8N_WEBHOOK_URL="https://YOUR-N8N-HOST/webhook/traffic-event"
python detector.py
This starter intentionally exposes a limitation: global rider counts and isolated no_helmet detections cannot prove which motorcycle is involved. Use per-track object association and a custom helmet dataset before treating events as genuine violations. If your model outputs helmet-wearing classes rather than no_helmet, adjust the event logic to match those classes.
7. ESP32 IoT webpage and firmware
The ESP32's responsibilities are to connect to Wi-Fi, provide a local status page, receive an event summary, and optionally activate a warning LED.
For a clean demonstration, the AI gateway can notify both the ESP32 and n8n. In a more compact design, the ESP32 can forward event data to n8n directly.
ESP32 firmware
Install the ESP32 board support in Arduino IDE. Then create esp32/helmet_iot.ino.
The following code exposes a local webpage and a simple JSON event endpoint. It is a starter for the IoT layer; use a shared secret or stronger authentication before allowing remote event submissions.
#include <WiFi.h>
#include <WebServer.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
WebServer server(80);
const int LED_PIN = 26;
String lastEvent = "System started";
String lastType = "NORMAL";
unsigned long eventCount = 0;
const char PAGE[] PROGMEM = R"HTML(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AI Traffic Safety IoT</title>
<style>
body { font-family: Arial; background:#eef3f8; padding:20px; }
.card { background:white; padding:18px; border-radius:12px;
margin-bottom:14px; box-shadow:0 2px 8px #ccd; }
h1 { color:#183b64; }
</style>
</head>
<body>
<h1>AI Traffic Safety Monitor</h1>
<div class="card">
<h3>ESP32 Device</h3>
<p id="status">Loading...</p>
</div>
<div class="card">
<h3>Latest Event</h3>
<p id="event">Loading...</p>
<p id="count"></p>
</div>
<script>
async function refresh() {
try {
const r = await fetch('/status');
const d = await r.json();
document.getElementById('status').textContent =
'Wi-Fi connected | IP: ' + d.ip;
document.getElementById('event').textContent =
d.event_type + ' | ' + d.last_event;
document.getElementById('count').textContent =
'Events received: ' + d.event_count;
} catch (e) {
document.getElementById('status').textContent =
'Device unavailable';
}
}
refresh();
setInterval(refresh, 3000);
</script>
</body>
</html>
)HTML";
void handleStatus() {
String json = "{";
json += "\"ip\":\"" + WiFi.localIP().toString() + "\",";
json += "\"event_type\":\"" + lastType + "\",";
json += "\"last_event\":\"" + lastEvent + "\",";
json += "\"event_count\":" + String(eventCount);
json += "}";
server.send(200, "application/json", json);
}
void handleEvent() {
if (!server.hasArg("type")) {
server.send(400, "application/json",
"{\"error\":\"missing type\"}");
return;
}
String type = server.arg("type");
// Allow only known event labels.
if (type != "NO_HELMET" &&
type != "TRIPLE_RIDING" &&
type != "NO_HELMET_AND_TRIPLE_RIDING" &&
type != "NORMAL") {
server.send(400, "application/json",
"{\"error\":\"invalid type\"}");
return;
}
lastType = type;
lastEvent = String(millis()) + " ms";
eventCount++;
digitalWrite(LED_PIN, type == "NORMAL" ? LOW : HIGH);
server.send(200, "application/json",
"{\"accepted\":true}");
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Open webpage: http://");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, []() {
server.send_P(200, "text/html", PAGE);
});
server.on("/status", HTTP_GET, handleStatus);
server.on("/event", HTTP_POST, handleEvent);
server.begin();
}
void loop() {
server.handleClient();
}
How to test the webpage
-
Replace the Wi-Fi credentials with your network details.
-
Upload the sketch to the ESP32.
-
Open Serial Monitor at 115200 baud.
-
Copy the IP address printed by the ESP32.
-
Open
http://ESP32-IP/on a device on the same local network. -
Confirm that the device status and event count are displayed.
-
Send a test event using the command below, replacing the address with your ESP32's actual IP.
curl -X POST "http://ESP32-IP/event" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "type=NO_HELMET"
The webpage displays the latest event and the number of events received. This example uses an unauthenticated local HTTP server, so it is suitable only for a trusted lab network. Do not expose it directly to the internet.
8. n8n automation workflow
n8n is the automation engine connecting the AI detection system to messaging and cloud services. Its Webhook node can receive events from external applications and start a workflow.
Workflow diagram
flowchart TD
A[Webhook Trigger] --> B[Validate Required Fields]
B --> C{Valid Event?}
C -->|No| D[Reject or Log Invalid Request]
C -->|Yes| E[Deduplicate event_id]
E --> F{New Event?}
F -->|No| G[Skip Duplicate]
F -->|Yes| H[Switch on event_type]
H --> I[Create Alert Message]
H --> J[Append Google Sheets Row]
H --> K[Update ThingSpeak]
I --> L[Telegram Text Message]
L --> M{Voice Alert Enabled?}
M -->|Yes| N[Generate or Select Audio]
N --> O[Telegram Audio Message]
M -->|No| P[End]
O --> P
J --> Q[Error Handling / Retry]
K --> Q
Step 1 — Create an n8n webhook
-
Open your n8n workspace.
-
Create a new workflow called
AI Traffic Surveillance. -
Add a Webhook node.
-
Set the HTTP method to
POST. -
Set the path to
traffic-event. -
During development, use the Test URL and activate the node's test listener.
-
Send a sample JSON event.
-
Once testing works, activate the workflow and use its Production URL in your AI program.
Example event:
{
"event_id": "demo-0001",
"timestamp": "2026-10-19T01:30:00Z",
"device_id": "AI-GATEWAY-01",
"camera_id": "CAM-01",
"event_type": "NO_HELMET",
"rider_count": 1,
"confidence": 0.94,
"source": "ai_edge",
"review_status": "pending"
}
Step 2 — Validate and route the event
Add an IF node or Code node after the Webhook.
Validation rules:
-
event_idmust be present. -
event_typemust be one of the allowed event types. -
confidencemust be numeric and between 0 and 1. -
rider_countmust be a non-negative integer. -
Reject unexpected payloads.
-
Check
event_idagainst previously processed IDs to prevent duplicate notifications.
For the initial prototype, use an IF node to check that the event ID and event type exist. For production, use a persistent deduplication store and a defined retention period.
Add a Switch node with these cases:
-
NO_HELMET -
TRIPLE_RIDING -
NO_HELMET_AND_TRIPLE_RIDING -
NORMAL
You can route the first three to the alert workflow and send normal events to telemetry only.
Step 3 — Format an alert
Add an Edit Fields (Set) node. Create a field named alert_message with this n8n expression:
{{
"TRAFFIC SAFETY ALERT\n" +
"Event: " + $json.body.event_type + "\n" +
"Camera: " + $json.body.camera_id + "\n" +
"Device: " + $json.body.device_id + "\n" +
"Riders detected: " + $json.body.rider_count + "\n" +
"Confidence: " + $json.body.confidence + "\n" +
"Time: " + $json.body.timestamp + "\n" +
"Status: Pending review"
}}
This expression assumes the incoming fields are nested under body, as commonly returned by the n8n Webhook node. If your node outputs the event at the top level, use $json.event_type, $json.camera_id, and the corresponding top-level paths instead.
Step 4 — Connect the workflow nodes
Webhook
Receives event JSON over HTTP POST.
IF / Code / Switch
Validates, deduplicates, and categorizes the event.
Telegram
Sends text and optionally an audio alert.
Google Sheets
Appends one row for each accepted event.
ThingSpeak
Stores numeric counts and event-type telemetry.
9. Telegram text and voice alerts
Step 1 — Create a Telegram bot
-
Open Telegram and search for the verified
@BotFatheraccount. -
Send
/newbot. -
Follow the prompts to choose a bot name and username.
-
Copy the bot token and keep it private.
-
Open a conversation with the new bot and press Start.
-
Obtain your chat ID using an appropriate Telegram Bot API method or your n8n Telegram credentials.
-
Add the bot to the intended private group if the project needs group alerts, and configure permissions as required.
Step 2 — Configure the Telegram node
In n8n, add the Telegram node and configure:
-
Resource/operation: send a text message.
-
Credentials: your Telegram bot token.
-
Chat ID: your intended recipient or group.
-
Text: the
alert_messagegenerated earlier.
Example notification:
🚨 AI TRAFFIC SAFETY ALERT
Event: TRIPLE_RIDING
Camera: CAM-01
Riders detected: 3
Confidence: 0.93
Time: 2026-10-19T01:30:00Z
Please review the event.
This is an AI-generated detection, not a
confirmed legal violation.
Step 3 — Add a voice notification
A Telegram voice notification can be implemented in two ways:
-
Audio message: Generate a spoken audio file using a text-to-speech service, then send the file through Telegram's audio or voice-message operation.
-
Spoken announcement: Send the event text to a TTS service, download the generated audio, and pass its binary output to the Telegram node.
Example spoken message:
Traffic safety alert. Possible triple riding detected at camera CAM-01. Three riders were detected. Please review the event.
For a simple project, use a TTS provider that returns an MP3 or OGG file. Configure the HTTP Request node to retrieve the audio as binary data, then map that binary property to the Telegram node's audio input. A voice-note-style message may require an OGG/Opus file; a regular audio message can use a supported audio format.
Voice-alert flow
flowchart TD
A[Confirmed Workflow Event] --> B[Create Spoken Message]
B --> C[Text-to-Speech Service]
C --> D{Audio Generated?}
D -->|Yes| E[Download Audio Binary]
E --> F[Telegram Send Audio]
D -->|No| G[Send Text Alert]
F --> H[Log Notification Result]
G --> H
For a demonstration without an external TTS subscription, pre-record two or three generic alert files and select the relevant file based on event_type. The notification must still clearly say that the detection is preliminary.
10. Google Sheets integration
Create a spreadsheet called AI_Traffic_Incidents.
Use these column headings in the first row:
|
Column |
Field |
|---|---|
|
A |
Timestamp |
|
B |
Event ID |
|
C |
Camera ID |
|
D |
Device ID |
|
E |
Event Type |
|
F |
Rider Count |
|
G |
Confidence |
|
H |
Source |
|
I |
Review Status |
|
J |
Alert Status |
In n8n:
-
Add the Google Sheets node.
-
Authenticate your Google account using the supported OAuth credential flow.
-
Select the spreadsheet and worksheet.
-
Choose the append-row operation.
-
Map each column to the corresponding validated webhook field.
-
Set
Alert Statusto the actual result of the notification step, where practical. -
Test that a single incoming event creates exactly one row.
Example incident record:
2026-10-19T01:30:00Z
demo-0001
CAM-01
AI-GATEWAY-01
NO_HELMET
1
0.94
ai_edge
pending
sent
Use the event ID as the unique key for deduplication. Google Sheets is convenient for a student project, but a database is more appropriate for high-volume events and concurrent writes.
11. ThingSpeak IoT dashboard
ThingSpeak is suited to numeric IoT telemetry and time-series visualization. Its channels support multiple numeric fields, making it useful for detection counts and device health.
Configure the channel
Create a private channel named AI Traffic Surveillance.
|
Field |
Suggested value |
|---|---|
|
Field 1 |
Helmet violation count |
|
Field 2 |
Triple-riding count |
|
Field 3 |
Combined-event count |
|
Field 4 |
Total accepted events |
|
Field 5 |
Average confidence × 100 |
|
Field 6 |
ESP32 online (0/1) |
Save the channel ID and Write API Key. Do not publish the write key in browser code or a public repository.
Update ThingSpeak from n8n
Add an HTTP Request node after the event validation step.
-
Method:
POST -
URL:
https://api.thingspeak.com/update -
Body type: form URL-encoded
-
Parameters:
api_key,field1,field2, and the other configured fields.
Example payload for an event:
api_key=YOUR_WRITE_API_KEY
field1=1
field2=0
field3=0
field4=1
field5=94
field6=1
This represents one helmet event with 94% model confidence and an online ESP32. In a production implementation, maintain cumulative counters in persistent storage and send the actual running totals rather than repeatedly sending 1. Also follow the channel's current update-rate limits.
Dashboard layout
AI Traffic Surveillance Dashboard
Helmet events
—
Live channel data
Triple-riding events
—
Live channel data
ESP32 status
Not connected
Example placeholder
Latest alert
Awaiting data
Example placeholder
Conceptual dashboard mock-up only. It is not connected to your ThingSpeak account, and the values are not live readings.
In ThingSpeak, configure time-series charts for Fields 1–4, and use the latest channel values to monitor the system. For richer filtering and incident review, use Google Sheets or a separate web dashboard alongside ThingSpeak.
12. Optional AI agent: making the system agentic
An ordinary automation sends messages according to fixed rules. An agentic implementation can additionally inspect event metadata, prioritize incidents, summarize recurring patterns, and recommend actions.
For this project, use a bounded AI agent rather than allowing an AI model to control every system component autonomously.
flowchart TD
A[Validated Detection Event] --> B[Rule Engine]
B --> C[AI Agent]
C --> D[Summarize Event]
C --> E[Assess Alert Priority]
C --> F[Suggest Follow-up]
D --> G[Telegram Message]
E --> G
F --> H[Operator Review]
B --> I[Google Sheets]
B --> J[ThingSpeak]
Example agent behavior
Input:
{
"event_type": "TRIPLE_RIDING",
"rider_count": 3,
"confidence": 0.93,
"camera_id": "CAM-01"
}
Example agent output:
{
"summary": "Possible triple riding detected.",
"priority": "high",
"recommended_action": "Review the recorded event.",
"requires_human_review": true
}
These are illustrative outputs, not live AI results.
Configure the agent with structured inputs, a limited set of approved tools, and a defined output schema. Do not let it invent detection facts, change stored records without authorization, or independently issue penalties.
13. Complete event-processing flow
This is the recommended end-to-end execution sequence for your final project.
sequenceDiagram
participant C as Camera
participant AI as AI Computer
participant E as ESP32
participant N as n8n
participant T as Telegram
participant S as Google Sheets
participant TS as ThingSpeak
C->>AI: Video frames
AI->>AI: Detect and track riders
AI->>AI: Evaluate safety rules
AI->>E: Optional event notification
AI->>N: HTTPS POST event JSON
N->>N: Validate and deduplicate
N->>T: Send text alert
N->>T: Send audio alert if enabled
N->>S: Append incident row
N->>TS: Update numeric telemetry
E-->>AI: Device status, if requested
14. Step-by-step project implementation plan
-
Build the ESP32 hardware. Connect the development board, LED, optional buzzer driver, and power supply. Verify the board boots and joins Wi-Fi.
-
Develop the IoT webpage. Upload the ESP32 firmware and confirm the status page opens from another device on the local network.
-
Prepare the AI dataset. Collect authorized traffic images or videos representing different lighting, camera angles, helmets, motorcycles, and rider counts. Annotate the relevant objects.
-
Train and evaluate the model. Measure precision, recall, and false-positive rates on a separate test set. Verify that rider counting works per motorcycle, not just per frame.
-
Integrate the event gateway. Send structured JSON from the AI computer to n8n over HTTPS. Use a separate authenticated ESP32 endpoint if the microcontroller needs event data.
-
Build the n8n workflow. Configure the webhook, validation, event routing, deduplication, and error handling.
-
Connect Telegram. Test text notifications first, then configure TTS and audio delivery.
-
Connect Google Sheets. Verify column mappings and confirm that retries do not create duplicate incident records.
-
Connect ThingSpeak. Create numeric fields, write telemetry, and configure time-series charts.
-
Run an end-to-end test. Simulate normal traffic, helmet detections, triple riding, network failures, and duplicate webhook submissions.
15. Testing and expected results
|
Test case |
Expected behavior |
|---|---|
|
Rider wearing a helmet |
No helmet alert if the model classifies the event correctly |
|
Rider without a helmet |
Candidate helmet event recorded and notification routed |
|
Two people on a motorcycle |
No triple-riding alert |
|
Three people on a motorcycle |
Triple-riding candidate event recorded |
|
Helmet and triple riding together |
Combined event category generated |
|
Repeated frames of one event |
Cooldown and deduplication limit repeated alerts |
|
Wi-Fi disconnected |
Device shows a connection problem; queued delivery or recovery should be implemented |
|
n8n unavailable |
Event should be retried or stored locally for later delivery |
|
Telegram failure |
Workflow records the failure and retries where appropriate |
|
Invalid event payload |
Request rejected or quarantined |
|
ThingSpeak update failure |
Error logged without losing the primary incident record |
Measure the following performance indicators during evaluation:
-
Detection precision and recall.
-
False alerts per hour.
-
Triple-riding count accuracy.
-
End-to-end alert latency.
-
Successful webhook delivery rate.
-
Successful Telegram delivery rate.
-
Missing or duplicate spreadsheet records.
-
ESP32 uptime and Wi-Fi reconnection time.
Do not claim a particular detection accuracy or response time until you have measured it on your own test dataset and hardware.
16. Security, privacy, and deployment considerations
-
Use HTTPS for webhook communication and authentication for event submissions.
-
Store Telegram tokens, API keys, Wi-Fi credentials, and AI service credentials in protected configuration or n8n credentials.
-
Do not expose ESP32's local HTTP endpoints directly to the public internet.
-
Avoid retaining identifiable footage unless necessary; set a retention period and restrict access.
-
Obtain appropriate permission before deploying a camera in public or private spaces.
-
Treat confidence scores as model estimates, not proof of a violation.
-
Keep a human review step for consequential decisions.
-
Implement retries and persistent event storage so that network interruptions do not silently discard events.
-
Verify the licenses and commercial-use conditions of the selected AI model and dataset.
17. Project documentation summary
Suggested project report chapters
-
Abstract and introduction
-
Problem statement and objectives
-
Literature review and existing systems
-
Proposed system and block diagram
-
Hardware components and circuit schematic
-
AI model, dataset, training, and evaluation
-
ESP32 firmware and IoT webpage
-
n8n workflow and agentic automation
-
Telegram text and voice notifications
-
Google Sheets and ThingSpeak integration
-
Testing, results, and performance metrics
-
Security, privacy, limitations, and future scope
-
Conclusion and references
18. Official technical references
19. Final recommended design
For a reliable college demonstration, use a webcam or IP camera connected to an AI computer, an ESP32 for the IoT status webpage and optional local warning, n8n for orchestration, Telegram for text and audio alerts, Google Sheets for incident records, and ThingSpeak for numeric monitoring.
This separation of responsibilities is easier to debug and upgrade than attempting to run the entire computer-vision model on an ESP32.
One final detail will help me tailor the complete runnable code to your hardware: Which camera and AI hardware are you planning to use?
1. Camera and AI hardware
2. ESP32 board
3. AI model
4. n8n deployment
Project Summary: AI Helmet & Triple-Riding Detection System
Project title: AI-Powered Traffic Surveillance Using ESP32, Computer Vision, n8n Automation, Telegram Voice Alerts, Google Sheets, and ThingSpeak.
1. Project objective
Develop an AI-powered system that detects:
-
Riders not wearing helmets.
-
Motorcycles carrying three or more people.
-
Traffic-safety events that need review.
The system automatically sends alerts, records incidents, and displays monitoring statistics through IoT and cloud services.
2. System architecture
flowchart TD
A[Camera / ESP32-CAM] --> B[AI Computer Vision]
B --> C[Helmet and Rider Detection]
C --> D[Event Classification]
D --> E[ESP32 IoT Device]
D --> F[n8n Automation]
E --> G[IoT Webpage / LED / Buzzer]
F --> H[Telegram Text Alerts]
F --> I[Telegram Voice Alerts]
F --> J[Google Sheets]
F --> K[ThingSpeak Dashboard]
F --> L[AI Agent Event Summary]
3. Main hardware and software
|
Component |
Function |
|---|---|
|
Camera or ESP32-CAM |
Traffic image capture |
|
Laptop / edge computer |
Runs the AI detection model |
|
ESP32 DevKit |
IoT connectivity and device control |
|
Python + OpenCV + YOLO |
Computer vision |
|
n8n |
Workflow automation |
|
Telegram Bot API |
Text and voice notifications |
|
Google Sheets |
Incident logging |
|
ThingSpeak |
Cloud telemetry and charts |
|
HTML, CSS, JavaScript |
IoT monitoring webpage |
4. How the system works
-
The camera captures traffic video.
-
The AI model detects motorcycles, riders, and helmet status.
-
The event engine identifies potential helmet violations or triple riding.
-
The ESP32 displays device status and can activate a warning LED or buzzer.
-
n8n receives event data through a webhook.
-
Telegram sends a text alert and optionally a generated voice message.
-
Google Sheets stores event details and review status.
-
ThingSpeak displays numerical statistics and trends.
-
An optional AI agent summarizes incidents and recommends human review.
5. Key software modules
-
AI module: Python, OpenCV, custom-trained YOLO model, rider tracking.
-
ESP32 module: Arduino firmware, Wi-Fi, webpage, optional LED and buzzer.
-
Automation module: n8n webhook, event validation, deduplication, notifications.
-
Cloud module: Telegram, Google Sheets, ThingSpeak.
-
Dashboard module: Device status, incident counts, event history.
6. Important implementation note
The recommended design runs computer vision on a laptop or edge computer and uses the ESP32 for IoT tasks. A standard ESP32 generally cannot run a full traffic-detection model reliably. Triple-riding detection also requires associating riders with individual motorcycles rather than simply counting people in a frame.
Expected outcome: An integrated prototype that detects potential traffic-safety violations, sends automated notifications, records incidents, and provides cloud-based monitoring.
Mind Map: AI Helmet & Triple-Riding Detection System
AI Traffic Surveillance System
Helmet Detection + Triple Riding + Agentic IoT
1. AI Vision
-
Camera / CCTV
-
YOLO + OpenCV
-
Helmet detection
-
Rider counting
-
Object tracking
2. ESP32 IoT
-
ESP32 DevKit
-
Wi-Fi connectivity
-
Web status page
-
LED / buzzer
-
Device monitoring
3. n8n Automation
-
Webhook
-
Event validation
-
Deduplication
-
AI agent
-
Error handling
4. Alerts
-
Telegram bot
-
Text notifications
-
Text-to-speech
-
Audio alerts
-
Event summaries
5. Cloud Storage
-
Google Sheets
-
Incident records
-
Event timestamps
-
Confidence scores
-
Review status
6. Dashboard
-
ThingSpeak charts
-
Helmet event counts
-
Triple-riding counts
-
ESP32 status
-
Event trends
End-to-End Workflow
Camera → AI Detection → Event Classification
ESP32 / IoT Gateway → n8n Webhook
Telegram Alerts + Google Sheets + ThingSpeak
Monitoring, Analytics and Human Review
Core event categories
-
No helmet: potential helmet violation detected.
-
Triple riding: three or more riders associated with one motorcycle.
-
Combined event: both conditions detected.
-
Normal: no qualifying event detected.
Recommended development sequence
-
Build the AI detection model.
-
Set up ESP32 hardware and webpage.
-
Connect the AI event output to n8n.
-
Configure Telegram text and voice alerts.
-
Integrate Google Sheets and ThingSpeak.
-
Add AI-agent summaries, error handling, and human review.
Key design principle: Run the main computer-vision model on a laptop or edge AI computer; use the ESP32 for IoT connectivity, device status, and optional physical alerts.

No comments:
Post a Comment