AI + ESP32 + IoT + n8n Automation + AI Agent + Telegram Voice Alerts + Google Sheets + ThingSpeak
1. Full Project Description
2. Main Project Objectives
3. System Architecture
4. Hardware Components List
| Component | Purpose |
|---|---|
| Used for implementing the AI-powered IoT voting system. |
5. Software Requirements
6. Circuit Schematic Diagram
+----------------------+
| ESP32 |
| |
| GPIO 25 <------------ Button 1
| GPIO 26 <------------ Button 2
| GPIO 27 <------------ Button 3
| GPIO 14 <------------ Button 4
| |
| GPIO 21 ------------ SDA
| GPIO 22 ------------ SCL
| |
| GPIO 18 ------------ Buzzer
| GPIO 2 ------------ Auth LED
| GPIO 4 ------------ Vote LED
| GPIO 5 ------------ Error LED
+----------+-----------+
|
+---------------------+---------------------+
| | |
v v v
+-----------+ +-----------+ +-------------+
| OLED | | INA219 | | ESP32-CAM |
| Display | | Sensor | | Camera |
+-----------+ +-----------+ +-------------+
7. Pin Connection Table
| Device | ESP32 Pin |
|---|---|
| Candidate Button 1 | GPIO 25 |
| Candidate Button 2 | GPIO 26 |
| Candidate Button 3 | GPIO 27 |
| Candidate Button 4 | GPIO 14 |
| OLED SDA | GPIO 21 |
| OLED SCL | GPIO 22 |
| Buzzer | GPIO 18 |
| Authentication LED | GPIO 2 |
| Voting LED | GPIO 4 |
| Error LED | GPIO 5 |
8. Complete System Flowchart
START ↓ Initialize ESP32 ↓ Connect Wi-Fi ↓ Initialize Camera ↓ Initialize Sensors ↓ Wait for Voter ↓ Capture Face ↓ AI Face Authentication ↓ Is Face Authorized? ├── NO │ ├── Display Authentication Failed │ ├── Record Failed Attempt │ └── Send Telegram Alert YES ↓ Check Duplicate Voting Status ↓ Already Voted? ├── YES │ ├── Reject Voting Attempt │ └── Send Security Alert NO ↓ Display Candidate Options ↓ Voter Selects Candidate ↓ Confirm Voting Selection ↓ Create Voting Event ↓ Send Event to n8n Webhook ↓ AI Agent Analysis ↓ Google Sheets Logging ↓ ThingSpeak Update ↓ Telegram Text Alert ↓ Telegram Voice Alert ↓ Update IoT Dashboard ↓ END
9. Data Flow Diagram
10. ESP32 Source Code
#include <WiFi.h>
#include <HTTPClient.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
const char* N8N_WEBHOOK_URL =
"https://your-n8n-domain.com/webhook/e-voting";
#define BUTTON_1 25
#define BUTTON_2 26
#define BUTTON_3 27
#define BUTTON_4 14
#define LED_AUTH 2
#define LED_VOTE 4
#define BUZZER 18
bool authenticated = false;
bool voteSubmitted = false;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_3, INPUT_PULLUP);
pinMode(BUTTON_4, INPUT_PULLUP);
pinMode(LED_AUTH, OUTPUT);
pinMode(LED_VOTE, OUTPUT);
pinMode(BUZZER, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi Connected");
Serial.println(WiFi.localIP());
}
void loop() {
if (!authenticated) {
Serial.println("Waiting for AI Face Authentication");
delay(3000);
authenticated = true;
digitalWrite(LED_AUTH, HIGH);
tone(BUZZER, 1000, 300);
Serial.println("Face Authentication Successful");
}
if (authenticated && !voteSubmitted) {
digitalWrite(LED_VOTE, HIGH);
if (digitalRead(BUTTON_1) == LOW) {
submitVote("CANDIDATE_1");
}
if (digitalRead(BUTTON_2) == LOW) {
submitVote("CANDIDATE_2");
}
if (digitalRead(BUTTON_3) == LOW) {
submitVote("CANDIDATE_3");
}
if (digitalRead(BUTTON_4) == LOW) {
submitVote("CANDIDATE_4");
}
}
}
void submitVote(String candidate) {
voteSubmitted = true;
digitalWrite(LED_VOTE, LOW);
tone(BUZZER, 1500, 500);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(N8N_WEBHOOK_URL);
http.addHeader(
"Content-Type",
"application/json"
);
String jsonData = "{";
jsonData += "\"event\":\"VOTE_SUBMITTED\",";
jsonData += "\"candidate\":\"" + candidate + "\",";
jsonData += "\"device\":\"ESP32_STATION_01\",";
jsonData += "\"authentication\":\"FACE_AUTHENTICATED\"";
jsonData += "}";
int responseCode = http.POST(jsonData);
Serial.print("n8n Response Code: ");
Serial.println(responseCode);
http.end();
}
}
11. n8n Workflow Architecture
ESP32
↓
n8n Webhook Trigger
↓
Validate JSON Data
↓
Check Authentication
↓
Check Event Type
↓
AI Agent Analysis
↓
┌───────────────────────────┐
│ │
▼ ▼
Google Sheets ThingSpeak
Logging Cloud Update
│ │
└──────────────┬────────────┘
▼
Telegram Notification
│
▼
Text-to-Speech
│
▼
Telegram Voice Alert
12. n8n Workflow JSON
{
"name": "AI E-Voting IoT Automation",
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "e-voting",
"responseMode": "onReceived"
},
"name": "ESP32 Voting Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2
},
{
"parameters": {
"jsCode": "const data = $json.body || $json;
return [{
json: {
event: data.event,
candidate: data.candidate,
device: data.device,
authentication: data.authentication,
timestamp: new Date().toISOString()
}
}];"
},
"name": "Validate Voting Data",
"type": "n8n-nodes-base.code",
"typeVersion": 2
},
{
"parameters": {
"operation": "append",
"documentId": "GOOGLE_SHEET_ID",
"sheetName": "Votes"
},
"name": "Google Sheets Logging",
"type": "n8n-nodes-base.googleSheets",
"typeVersion": 4
},
{
"parameters": {
"method": "POST",
"url": "https://api.thingspeak.com/update"
},
"name": "ThingSpeak Update",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4
},
{
"parameters": {
"chatId": "TELEGRAM_CHAT_ID",
"text": "New authenticated voting event received."
},
"name": "Telegram Notification",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1
}
]
}
13. Telegram Bot Setup
- Open Telegram.
- Search for BotFather.
- Execute the command /newbot.
- Enter a bot name.
- Enter a unique bot username.
- Copy the generated Bot Token.
- Send a message to your bot.
- Obtain the Telegram Chat ID.
- Add Telegram credentials inside n8n.
- Test a notification workflow.
14. Telegram Notification Examples
FACE AUTHENTICATION SUCCESSFUL Voting Station: ESP32_STATION_01 Status: Authorized voter detected. --------------------------------- VOTE EVENT Candidate: CANDIDATE_1 Station: ESP32_STATION_01 Status: Vote event recorded successfully. --------------------------------- SECURITY ALERT Multiple authentication failures detected. Device: ESP32_STATION_01 Action: System monitoring activated.
15. Telegram Voice Notification Automation
Example voice notification:
Attention. A new authenticated voting event has been recorded at voting station one.
16. Google Sheets Integration
| Column | Description |
|---|---|
| Timestamp | Date and time of event |
| Event Type | Voting or authentication event |
| Candidate | Selected candidate identifier |
| Station ID | ESP32 station identifier |
| Authentication Status | Success or failure |
| Voltage | Measured voltage |
| Current | Measured current |
| Power | Power consumption |
| AI Risk Level | Low, medium or high |
17. ThingSpeak Cloud Dashboard
| ThingSpeak Field | Data |
|---|---|
| Field 1 | Voting Events |
| Field 2 | Authentication Success |
| Field 3 | Authentication Failures |
| Field 4 | Voltage |
| Field 5 | Current |
| Field 6 | Power |
| Field 7 | Energy |
| Field 8 | AI Power Prediction |
18. AI Power Consumption Prediction Logic
Voltage + Current ↓ Power Calculation ↓ Historical Power Data ↓ Average Power ↓ Voting Activity Factor ↓ AI Prediction ↓ Future Power Consumption
The basic electrical power relationship is:
Power = Voltage × Current
For example:
Voltage = 5 Volts Current = 0.25 Amperes Power = 5 × 0.25 Power = 1.25 Watts
19. AI Anomaly Detection
- Too many authentication failures.
- Repeated voting requests.
- Unusual power consumption.
- Unexpected ESP32 device activity.
- Multiple requests from the same device.
- Invalid API requests.
20. PHP IoT Dashboard
<?php
$esp32_status = "ONLINE";
$authentication_status = "ACTIVE";
$total_votes = 245;
$authentication_failures = 3;
$power_consumption = "1.25 W";
$ai_risk_level = "LOW";
?>
<!DOCTYPE html>
<html>
<head>
<title>AI Smart E-Voting Dashboard</title>
</head>
<body>
<h1>AI-Based Smart E-Voting IoT Dashboard</h1>
<p>ESP32 Status: <?php echo $esp32_status; ?></p>
<p>Face Authentication:
<?php echo $authentication_status; ?>
</p>
<p>Total Voting Events:
<?php echo $total_votes; ?>
</p>
<p>Authentication Failures:
<?php echo $authentication_failures; ?>
</p>
<p>Power Consumption:
<?php echo $power_consumption; ?>
</p>
<p>AI Risk Level:
<?php echo $ai_risk_level; ?>
</p>
</body>
</html>
21. Recommended Database Structure
Voter Table
voter_id face_template_hash authentication_status voted_status registration_timestamp
Vote Event Table
event_id anonymous_voter_token candidate_id timestamp station_id
22. Project Folder Structure
AI-E-VOTING-SYSTEM/
│
├── esp32/
│ └── smart_voting_esp32.ino
│
├── face-authentication/
│ ├── face_authentication.py
│ ├── authorized_users/
│ └── model/
│
├── web-dashboard/
│ ├── index.php
│ ├── dashboard.php
│ ├── config.php
│ └── api.php
│
├── n8n/
│ └── ai_e_voting_workflow.json
│
├── database/
│ └── database.sql
│
└── documentation/
├── circuit-diagram
├── flowchart
└── project-report
23. Complete Project Operation
- Power ON the ESP32.
- ESP32 connects to Wi-Fi.
- Camera captures the voter face.
- AI face authentication verifies the voter.
- The system checks duplicate voting status.
- The voting interface displays candidates.
- The voter selects a candidate.
- ESP32 creates a voting event.
- ESP32 sends data to the n8n webhook.
- n8n validates the data.
- AI Agent analyzes the event.
- Google Sheets stores the event.
- ThingSpeak updates the IoT dashboard.
- Telegram sends a notification.
- Text-to-Speech generates a voice message.
- Telegram sends the voice alert.
24. Security Architecture
CAMERA ↓ AI FACE AUTHENTICATION ↓ AUTHENTICATION TOKEN ↓ ESP32 ↓ HTTPS ↓ n8n WEBHOOK ↓ VALIDATION ↓ AI ANALYSIS ↓ CLOUD LOGGING
Recommended security features include HTTPS, API authentication, device authentication, secure Wi-Fi, encrypted credentials, duplicate-event protection, replay protection, timestamps and audit logs.
25. Future Enhancements
26. Final Complete Project Workflow
VOTER ↓ AI FACE AUTHENTICATION ↓ AUTHORIZED VOTER ↓ DUPLICATE VOTE CHECK ↓ VOTING INTERFACE ↓ CANDIDATE SELECTION ↓ ESP32 ↓ n8n AUTOMATION ↓ AI AGENT ↓ GOOGLE SHEETS ↓ THINGSPEAK ↓ TELEGRAM TEXT ALERT ↓ TEXT-TO-SPEECH ↓ TELEGRAM VOICE ALERT ↓ AI POWER PREDICTION ↓ IOT CLOUD DASHBOARD
27. Project Summary
This project combines Artificial Intelligence, Face Authentication, ESP32 IoT technology, n8n workflow automation, AI Agents, Telegram notifications, voice alerts, Google Sheets, ThingSpeak cloud monitoring and AI power consumption prediction into a single intelligent e-voting IoT platform. The system provides a complete workflow from voter authentication to voting event processing, cloud data logging, AI analysis, IoT monitoring and real-time notification automation.

No comments:
Post a Comment