AI Smart Distance Monitoring and Predictive Object Detection System
ESP32 + IoT + AI Agent + n8n + Telegram Voice Alerts + Google Sheets + ThingSpeak
1. Project Overview
This project continuously monitors object distance using an ultrasonic sensor connected to ESP32. The system uploads sensor data to ThingSpeak Cloud, logs records into Google Sheets, uses n8n automation workflows, and sends Telegram text and voice alerts.
Applications
- Smart Parking
- Industrial Safety
- Intruder Detection
- Warehouse Automation
- Smart Manufacturing
- Vehicle Collision Warning
2. Components Required
| Component | Quantity |
|---|---|
| ESP32 Development Board | 1 |
| HC-SR04 Ultrasonic Sensor | 1 |
| Breadboard | 1 |
| Jumper Wires | 10 |
| WiFi Router | 1 |
| Power Supply | 1 |
3. Circuit Connections
HC-SR04 ESP32 VCC ----> 5V GND ----> GND TRIG ----> GPIO5 ECHO ----> GPIO18 Optional Buzzer: + ----> GPIO23 - ----> GND
4. System Architecture
Ultrasonic Sensor
|
V
ESP32
|
WiFi
|
--------------------------------
| | |
V V V
ThingSpeak n8n Google Sheets
|
V
AI Agent
|
V
Telegram Voice Alerts
5. Flowchart
START Initialize ESP32 Connect WiFi Read Sensor Data Calculate Distance Upload To ThingSpeak Send Data To n8n AI Prediction Distance < Threshold? YES ----> Telegram Alert Store Data In Google Sheets Repeat
6. ESP32 Source Code
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";
#define TRIG 5
#define ECHO 18
void setup()
{
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED)
{
delay(1000);
}
}
void loop()
{
long duration;
float distance;
digitalWrite(TRIG,LOW);
delayMicroseconds(2);
digitalWrite(TRIG,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG,LOW);
duration = pulseIn(ECHO,HIGH);
distance = duration * 0.034 / 2;
Serial.println(distance);
delay(15000);
}
7. ThingSpeak Setup
- Create ThingSpeak Account
- Create New Channel
- Add Fields:
- Distance
- Prediction
- Power Consumption
- Copy Write API Key
- Insert API Key in ESP32 Code
8. Google Sheets Integration
| Timestamp | Distance | Prediction | Power | Alert Status |
|---|---|---|---|---|
| 10:00 | 40 cm | Safe | 1.2W | No |
9. Telegram Bot Setup
- Open Telegram
- Search BotFather
- Create Bot using /newbot
- Copy Bot Token
- Get Chat ID
- Use Token inside n8n Telegram Node
10. n8n Workflow
Webhook Function Node IF Node AI Agent Telegram Google Sheets ThingSpeak
11. AI Prediction Logic
40 35 30 25 20 Prediction: Object Approaching
20 25 30 35 40 Prediction: Object Moving Away
12. Power Consumption Prediction
Power = Voltage × Current Voltage = 5V Current = 0.24A Power = 1.2W
AI predicts higher power usage when alert frequency increases.
13. Voice Alert Automation
Webhook | AI Agent | Text To Speech | MP3 Voice | Telegram Send Voice
Sample Alert:
Warning! Object detected at 20 cm. Immediate attention required.
14. AI Agent Decision Logic
| Risk Level | Action |
|---|---|
| Low | Store Data Only |
| Medium | Telegram Notification |
| High | Telegram Voice Alert |
15. ThingSpeak Dashboard Widgets
- Distance Gauge
- Distance Trend Graph
- Power Prediction Graph
- Alert Counter
- Object Trend Analysis
16. Future Enhancements
- ESP32-CAM Integration
- Object Recognition
- Human Detection
- TensorFlow Lite Edge AI
- LSTM Prediction Models
- Smart City Deployment
17. Deployment Guide
- Assemble Hardware
- Upload ESP32 Firmware
- Configure ThingSpeak
- Configure Google Sheets
- Setup Telegram Bot
- Deploy n8n Workflow
- Test Distance Monitoring
- Verify Alerts and Dashboard
18. Expected Output
Distance : 18 cm Prediction : Object Approaching Rapidly Risk : HIGH Action : Telegram Voice Alert Sent ThingSpeak Updated Google Sheets Updated Predicted Power : 1.6W






