AI Smart Distance Monitoring and Predictive Object Detection System Using ESP32 and IoT

AI Smart Distance Monitoring and Predictive Object Detection System Using ESP32 + IoT + n8n + AI Agent + Telegram Voice Alerts + Google Sheets + ThingSpeak www.svsembedded.com SVSEMBEDDED svsembedded@gmail.com, CONTACT: 9491535690, 7842358459
<?php echo $title; ?>

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

  1. Create ThingSpeak Account
  2. Create New Channel
  3. Add Fields:
    • Distance
    • Prediction
    • Power Consumption
  4. Copy Write API Key
  5. 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

  1. Open Telegram
  2. Search BotFather
  3. Create Bot using /newbot
  4. Copy Bot Token
  5. Get Chat ID
  6. 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

  1. Assemble Hardware
  2. Upload ESP32 Firmware
  3. Configure ThingSpeak
  4. Configure Google Sheets
  5. Setup Telegram Bot
  6. Deploy n8n Workflow
  7. Test Distance Monitoring
  8. 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
For a final-year engineering project, a better structure is usually a complete PHP project with: index.php (Dashboard) config.php (API keys) esp32_receiver.php (Webhook endpoint) telegram_alert.php google_sheets.php thingspeak_update.php ai_prediction.php voice_alert.php database.sql assets/css/style.css assets/js/dashboard.js This modular version looks more professional and is suitable for project submission and deployment.

Comments