Monday, 5 October 2026

AI Battery State of Charge Estimation System

AI Battery State-of-Charge Estimation & Agentic IoT System

Below is a complete reference design for an ESP32-based AI Battery State-of-Charge (SOC) monitoring system integrating:

  • ESP32

  • Battery voltage/current/temperature sensing

  • SOC estimation

  • Optional AI/ML SOC correction

  • Local ESP32 IoT webpage

  • n8n automation

  • n8n AI Agent

  • Telegram text + voice alerts

  • Google Sheets data logging

  • ThingSpeak cloud dashboard

  • Fault/anomaly detection

  • Historical analysis

  • Remote monitoring

The architecture is deliberately designed so that the ESP32 remains capable of measuring and estimating SOC locally, while n8n acts as the cloud automation and AI-agent layer.

n8n is suitable for this because it provides workflow automation, integrations and AI functionality. n8n Documentation


1. Project Title

AI-Powered Battery State-of-Charge Estimation and Agentic IoT Monitoring System Using ESP32, n8n, Telegram Voice Alerts, Google Sheets and ThingSpeak


2. Abstract

This project implements an intelligent battery-monitoring system using an ESP32 microcontroller.

The ESP32 continuously measures:

  • Battery voltage

  • Battery current

  • Battery temperature

  • Estimated State of Charge (SOC)

  • Charging/discharging status

  • Battery power

  • Energy consumed

  • Energy returned during charging

The measurements are processed locally by the ESP32 using a hybrid SOC estimation algorithm based on:

  1. Battery voltage

  2. Coulomb counting

  3. Battery current

  4. Temperature compensation

  5. Optional AI/ML correction

The ESP32 publishes the measurements through Wi-Fi to an n8n automation server.

n8n acts as the central Agentic IoT orchestration layer.

The n8n workflow can:

  • Receive ESP32 measurements

  • Validate sensor data

  • Calculate additional battery parameters

  • Store measurements in Google Sheets

  • Update ThingSpeak

  • Send Telegram notifications

  • Generate Telegram voice alerts

  • Invoke an AI Agent

  • Detect abnormal battery behavior

  • Explain the reason for an alert

  • Recommend an action

  • Maintain alert history

  • Respond to Telegram commands

The result is an end-to-end IoT architecture:

Battery → Sensors → ESP32 → Wi-Fi → n8n → AI Agent → Cloud/Telegram/Dashboard


3. Main Objectives

The system has the following objectives.

Primary objectives

  • Measure battery voltage accurately.

  • Measure charge/discharge current.

  • Measure battery temperature.

  • Estimate battery SOC.

  • Display real-time battery information.

  • Store historical measurements.

  • Detect abnormal battery conditions.

  • Automatically notify the user.

  • Provide voice alerts.

  • Provide cloud visualization.

  • Use an AI Agent for intelligent interpretation.

Secondary objectives

The system can also:

  • Estimate remaining energy.

  • Detect unusually high current.

  • Detect rapid SOC loss.

  • Detect overheating.

  • Detect sensor failures.

  • Detect battery disconnection.

  • Detect charging problems.

  • Generate maintenance recommendations.


4. Important Design Principle

The AI Agent should not be the only source of battery safety decisions.

A robust architecture is:

                 ┌──────────────────────────────┐
                 │     HARD REAL-TIME LAYER     │
                 │                              │
Battery ────────►│ ESP32 + Sensors              │
                 │                              │
                 │ Voltage                      │
                 │ Current                      │
                 │ Temperature                  │
                 │ SOC estimator                │
                 │ Safety thresholds            │
                 └──────────────┬───────────────┘
                                │
                                ▼
                 ┌──────────────────────────────┐
                 │      CLOUD/AI LAYER          │
                 │                              │
                 │ n8n                          │
                 │ AI Agent                     │
                 │ Historical analysis          │
                 │ Recommendations              │
                 └──────────────┬───────────────┘
                                │
              ┌─────────────────┼──────────────────┐
              ▼                 ▼                  ▼
        Telegram            Google Sheets      ThingSpeak
        Text/Voice          Data Logging       Dashboard

The ESP32 should therefore continue to protect the system using deterministic thresholds even if:

  • Wi-Fi fails

  • n8n fails

  • the AI API is unavailable

  • Telegram is unavailable

  • the Internet connection is lost


5. Recommended Battery Configuration

For the reference implementation, use:

4S Li-ion battery pack

Nominal voltage:

4 × 3.7 V = 14.8 V

Typical voltage range:

Fully charged ≈ 16.8 V
Nominal ≈ 14.8 V
Discharged region ≈ 12 V

Use a proper 4S BMS.

Do not connect an unprotected lithium battery pack to the experimental circuit.

The same architecture can be adapted for:

  • 1S Li-ion

  • 2S Li-ion

  • 3S Li-ion

  • 4S Li-ion

  • LiFePO4

  • Lead-acid

  • AGM

  • other battery chemistries

However, the voltage/SOC lookup curve must be changed according to the chemistry.


6. System Block Diagram

                         BATTERY PACK
                    ┌────────────────────┐
                    │                    │
                    │  4S Li-ion + BMS   │
                    │                    │
                    └───────┬───────┬────┘
                            │       │
                            │       │
                     Voltage       Current
                      Sensor       Sensor
                            │       │
                            ▼       ▼
                    ┌─────────────────────┐
                    │       ESP32         │
                    │                     │
                    │ ADC                 │
                    │ INA219/INA226       │
                    │ DS18B20             │
                    │                     │
                    │ SOC Algorithm       │
                    │ Energy Calculation  │
                    │ Fault Detection     │
                    └──────────┬──────────┘
                               │
                             Wi-Fi
                               │
                               ▼
                    ┌─────────────────────┐
                    │        n8n          │
                    │                     │
                    │ Webhook             │
                    │ Validation          │
                    │ AI Agent            │
                    │ Rules Engine        │
                    │ Automation           │
                    └─────┬──────┬────────┘
                          │      │
              ┌───────────┘      └─────────────┐
              ▼                                ▼
       Google Sheets                       ThingSpeak
       Historical Data                     Cloud Charts
              │
              ▼
          AI Analysis
              │
              ▼
       Telegram Bot
       ┌──────────────┐
       │ Text Alert   │
       │ Voice Alert  │
       └──────────────┘

7. Hardware Architecture

7.1 Main components

Recommended components:

Component Purpose
ESP32 DevKit Main controller
INA219 or INA226 Current/voltage monitoring
Voltage divider Battery voltage measurement
DS18B20 Battery temperature
4S BMS Battery protection
12 V/5 V buck converter ESP32 power
Battery Device under test
Resistors Voltage divider
Capacitors ADC filtering
LEDs Local status
Push button Optional reset/configuration
OLED Optional local display

8. Recommended Sensor Arrangement

A practical arrangement is:

Battery +
   │
   │
   ▼
BMS
   │
   │
   ├──────────────► Voltage measurement
   │
   ▼
INA219 / INA226
   │
   │
   ▼
Load +

Temperature sensor:

DS18B20
   │
   └──── attached thermally to battery pack

ESP32:

             ┌──────────────────┐
Voltage ────►│                  │
Current ────►│      ESP32       │──── Wi-Fi
Temperature ►│                  │
             └──────────────────┘

9. Schematic Diagram

A simplified electrical schematic is:

                    4S BATTERY
                 ┌───────────────┐
                 │               │
 BAT+ ───────────┤ +           - ├───────────────┐
                 │               │               │
                 └───────────────┘               │
                       │                         │
                       │                         │
                       ▼                         │
                  ┌─────────┐                    │
                  │  4S BMS │                    │
                  └────┬────┘                    │
                       │                          │
                 PACK+ │                          │ PACK-
                       │                          │
                       ▼                          │
                  INA219/226                     │
                 ┌────────────┐                  │
                 │            │                  │
                 │ VIN+ VIN-  │                  │
                 │            │                  │
                 └─────┬──────┘                  │
                       │                          │
                       ▼                          │
                      LOAD+                       │
                                                  │
                      LOAD- ──────────────────────┘


INA219/226
   │
   ├── VCC ───── ESP32 3.3 V
   ├── GND ───── ESP32 GND
   ├── SDA ───── GPIO 21
   └── SCL ───── GPIO 22


Battery voltage divider:

Battery +
    │
    │
   R1
    │
    ├──────────── GPIO34 / ADC
    │
   R2
    │
   GND


DS18B20:

ESP32 GPIO 4 ───── DATA
                   │
                  4.7k
                   │
                  3.3V

DS18B20:
VCC ─────────────── 3.3V
GND ─────────────── GND
DATA ────────────── GPIO4

10. Voltage Divider Calculation

ESP32 ADC input must remain within the allowed ADC range.

For a maximum battery voltage of approximately:

Vbattery(max) = 16.8 V

A suitable divider is:

R1 = 100 kΩ
R2 = 22 kΩ

Output voltage:

Vadc = Vbattery × R2 / (R1 + R2)

Therefore:

Vadc = 16.8 × 22 / 122

approximately:

Vadc ≈ 3.03 V

This provides reasonable headroom for a 3.3 V ADC system.

For production hardware, verify the actual ESP32 ADC characteristics and calibrate the divider using a precision multimeter.


11. Current Measurement

INA219/INA226 can be used for current monitoring.

Example:

Battery → Current Sensor → Load

The sensor measures:

Voltage
Current
Power

Power is:

P = V × I

For example:

V = 15.2 V
I = 2.0 A

P = 15.2 × 2
P = 30.4 W

12. Temperature Measurement

Use a DS18B20 temperature sensor.

The temperature is important because battery behavior depends strongly on temperature.

The ESP32 records:

temperature = 31.5 °C

The AI layer can detect:

Temperature increasing rapidly

rather than only:

Temperature > fixed limit

That makes the system more intelligent.


13. What Is SOC?

SOC means:

State of Charge

It represents the estimated remaining usable battery capacity.

For example:

SOC = 100 %

means approximately full.

SOC = 50 %

means approximately half of the usable capacity remains.

SOC = 10 %

means the battery is nearly depleted.

SOC is an estimate, not a direct physical measurement.


14. SOC Estimation Methods

Three methods are useful.

Method 1 — Voltage-based SOC

The battery voltage is mapped to a SOC curve.

Example:

Voltage       SOC

16.8 V       100 %
16.4 V        90 %
16.0 V        80 %
15.6 V        65 %
15.2 V        50 %
14.8 V        35 %
14.4 V        20 %
13.2 V        10 %
12.0 V         0 %

These numbers are illustrative only and must be replaced with a curve appropriate to the actual battery.

Voltage-only SOC is simple but inaccurate when the battery is under load.


15. Coulomb Counting

Coulomb counting tracks current over time.

The basic equation is:

SOCnew = SOCold - I × Δt / Capacity

where:

I = battery current
Δt = elapsed time
Capacity = battery capacity in Ah

For a 10 Ah battery:

I = 2 A
Δt = 1 hour

Consumed capacity = 2 Ah

SOC decrease = 2 / 10 × 100

SOC decrease = 20 %

Coulomb counting provides good short-term tracking but accumulates error over time.


16. Hybrid SOC Algorithm

The recommended algorithm combines:

Voltage
+
Current
+
Temperature
+
Coulomb counting

Conceptually:

             Voltage
                │
                ▼
        ┌───────────────┐
Current ─► Coulomb      │
        │ Counter       │
        └───────┬───────┘
                │
Temperature ───►│
                │
                ▼
        ┌────────────────┐
        │ Hybrid SOC     │
        │ Estimator      │
        └───────┬────────┘
                │
                ▼
              SOC %

17. AI Enhancement

The AI Agent should not simply receive:

SOC = 32 %

and guess.

Instead, provide it with a structured observation:

{
  "battery_voltage": 14.82,
  "battery_current": 3.42,
  "temperature": 36.8,
  "soc": 31.7,
  "power": 50.7,
  "charging": false,
  "soc_change_5min": -4.2,
  "temperature_change_5min": 3.5,
  "device": "BATTERY-01"
}

The AI Agent can then reason about:

  • rapid SOC loss

  • overheating

  • excessive current

  • abnormal voltage sag

  • sensor failure

  • possible battery degradation


18. AI Agent Role

The AI Agent can receive battery telemetry and produce:

{
  "severity": "WARNING",
  "alert": true,
  "reason": "Battery SOC is falling rapidly while current remains high.",
  "recommendation": "Reduce the load and inspect the battery.",
  "voice_message": "Warning. Battery charge is falling rapidly. Please reduce the load."
}

The AI Agent is therefore an interpretation and orchestration layer, not the primary electrical protection mechanism.


19. Complete Data Flow

┌───────────┐
│  Battery  │
└─────┬─────┘
      │
      ▼
┌───────────────────────┐
│ Voltage/current/temp  │
│ Sensors               │
└──────────┬────────────┘
           │
           ▼
┌───────────────────────┐
│ ESP32                 │
│                       │
│ Sensor acquisition    │
│ Filtering             │
│ SOC calculation       │
│ Energy calculation    │
│ Local safety rules    │
└──────────┬────────────┘
           │ JSON/HTTPS
           ▼
┌───────────────────────┐
│ n8n Webhook           │
└──────────┬────────────┘
           │
           ▼
┌───────────────────────┐
│ Data validation       │
└──────────┬────────────┘
           │
           ├───────────────► Google Sheets
           │
           ├───────────────► ThingSpeak
           │
           ▼
┌───────────────────────┐
│ AI Agent              │
│                       │
│ Analyze telemetry     │
│ Detect anomalies      │
│ Classify severity     │
│ Recommend action      │
└──────────┬────────────┘
           │
           ▼
      ┌────┴─────┐
      │ Alert?   │
      └────┬─────┘
           │
       YES │
           ▼
┌───────────────────────┐
│ Telegram              │
│                       │
│ Text alert            │
│ Voice alert           │
└───────────────────────┘

20. ESP32 Software Architecture

The ESP32 firmware contains these modules:

main.cpp
│
├── Wi-Fi Manager
│
├── Battery Sensor
│   ├── INA219/INA226
│   ├── Voltage ADC
│   └── DS18B20
│
├── SOC Estimator
│
├── Energy Counter
│
├── Fault Detector
│
├── HTTP Client
│
└── Local Web Server

21. ESP32 JSON Telemetry

The ESP32 sends:

{
  "device_id": "BATTERY-01",
  "voltage": 15.72,
  "current": 2.31,
  "temperature": 32.4,
  "soc": 72.5,
  "power": 36.31,
  "energy_wh": 142.6,
  "charging": false,
  "wifi_rssi": -57,
  "uptime": 8421
}

22. ESP32 Arduino Code

Install these Arduino libraries:

WiFi
WebServer
HTTPClient
ArduinoJson
Wire
Adafruit INA219
OneWire
DallasTemperature

Example firmware:

#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// --------------------------------------------------
// Wi-Fi
// --------------------------------------------------

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASSWORD = "YOUR_PASSWORD";

// n8n webhook
const char* N8N_URL =
  "https://YOUR_N8N_DOMAIN/webhook/battery";

// --------------------------------------------------
// Pins
// --------------------------------------------------

#define BATTERY_ADC_PIN 34
#define TEMP_PIN 4

// Voltage divider
const float R1 = 100000.0;
const float R2 = 22000.0;

// Battery capacity
const float BATTERY_CAPACITY_AH = 10.0;

// --------------------------------------------------
// Objects
// --------------------------------------------------

WebServer server(80);

Adafruit_INA219 ina219;

OneWire oneWire(TEMP_PIN);
DallasTemperature tempSensor(&oneWire);

// --------------------------------------------------
// Battery state
// --------------------------------------------------

float batteryVoltage = 0.0;
float batteryCurrent = 0.0;
float batteryTemperature = 0.0;
float batteryPower = 0.0;

float soc = 100.0;
float energyWh = 0.0;

unsigned long lastMeasurement = 0;
unsigned long lastUpload = 0;

// --------------------------------------------------
// Read battery voltage
// --------------------------------------------------

float readBatteryVoltage()
{
  int raw = analogRead(BATTERY_ADC_PIN);

  float adcVoltage =
      ((float)raw / 4095.0) * 3.3;

  float battery =
      adcVoltage * (R1 + R2) / R2;

  return battery;
}

// --------------------------------------------------
// Voltage based SOC
// --------------------------------------------------

float voltageSOC(float voltage)
{
  // Example curve for demonstration.
  // Replace with experimentally measured
  // curve for your battery.

  if (voltage >= 16.8) return 100.0;
  if (voltage >= 16.4) return 90.0;
  if (voltage >= 16.0) return 80.0;
  if (voltage >= 15.6) return 65.0;
  if (voltage >= 15.2) return 50.0;
  if (voltage >= 14.8) return 35.0;
  if (voltage >= 14.4) return 20.0;
  if (voltage >= 13.2) return 10.0;

  return 0.0;
}

// --------------------------------------------------
// Hybrid SOC
// --------------------------------------------------

float calculateSOC(float voltage,
                   float current,
                   float dtHours)
{
  // Coulomb counting

  float deltaSOC =
      (current * dtHours /
       BATTERY_CAPACITY_AH) * 100.0;

  // Positive current = discharge
  soc -= deltaSOC;

  soc = constrain(soc, 0.0, 100.0);

  // Voltage correction.
  //
  // Stronger correction when current is low,
  // because loaded battery voltage is less
  // representative of open-circuit voltage.

  if (fabs(current) < 0.3)
  {
    float voltageSOCValue =
        voltageSOC(voltage);

    soc =
      0.85 * soc +
      0.15 * voltageSOCValue;
  }

  return soc;
}

// --------------------------------------------------
// Read sensors
// --------------------------------------------------

void readSensors()
{
  batteryVoltage =
      readBatteryVoltage();

  batteryCurrent =
      ina219.getCurrent_mA() / 1000.0;

  batteryPower =
      batteryVoltage *
      batteryCurrent;

  tempSensor.requestTemperatures();

  batteryTemperature =
      tempSensor.getTempCByIndex(0);
}

// --------------------------------------------------
// Local fault detection
// --------------------------------------------------

String detectFault()
{
  if (batteryTemperature >= 55.0)
    return "OVER_TEMPERATURE";

  if (batteryVoltage < 12.0)
    return "LOW_VOLTAGE";

  if (batteryCurrent > 10.0)
    return "OVER_CURRENT";

  if (soc < 10.0)
    return "LOW_SOC";

  return "NORMAL";
}

// --------------------------------------------------
// Send data to n8n
// --------------------------------------------------

void sendToN8N()
{
  if (WiFi.status() != WL_CONNECTED)
    return;

  HTTPClient http;

  http.begin(N8N_URL);

  http.addHeader(
      "Content-Type",
      "application/json");

  StaticJsonDocument<512> doc;

  doc["device_id"] = "BATTERY-01";
  doc["voltage"] = batteryVoltage;
  doc["current"] = batteryCurrent;
  doc["temperature"] = batteryTemperature;
  doc["soc"] = soc;
  doc["power"] = batteryPower;
  doc["energy_wh"] = energyWh;

  doc["charging"] =
      batteryCurrent < -0.1;

  doc["fault"] =
      detectFault();

  doc["wifi_rssi"] =
      WiFi.RSSI();

  doc["uptime"] =
      millis() / 1000;

  String payload;

  serializeJson(doc, payload);

  int response =
      http.POST(payload);

  Serial.print("n8n HTTP response: ");
  Serial.println(response);

  http.end();
}

// --------------------------------------------------
// Webpage
// --------------------------------------------------

String htmlPage()
{
  String html = R"rawliteral(

<!DOCTYPE html>

<html>

<head>

<meta name="viewport"
content="width=device-width, initial-scale=1">

<title>AI Battery Monitor</title>

<style>

body {
  font-family: Arial;
  background:#101820;
  color:white;
  text-align:center;
}

.card {
  background:#1d2b36;
  margin:15px;
  padding:20px;
  border-radius:15px;
}

.value {
  font-size:32px;
  color:#00e5ff;
}

.warning {
  color:#ffcc00;
}

.danger {
  color:#ff4444;
}

</style>

</head>

<body>

<h1>AI Battery Monitor</h1>

<div class="card">
<h2>Battery Voltage</h2>
<div class="value">
)rawliteral";

  html += String(batteryVoltage, 2);

  html += R"rawliteral(
 V
</div>
</div>

<div class="card">
<h2>Current</h2>
<div class="value">
)rawliteral";

  html += String(batteryCurrent, 2);

  html += R"rawliteral(
 A
</div>
</div>

<div class="card">
<h2>Temperature</h2>
<div class="value">
)rawliteral";

  html += String(batteryTemperature, 1);

  html += R"rawliteral(
 °C
</div>
</div>

<div class="card">
<h2>State of Charge</h2>
<div class="value">
)rawliteral";

  html += String(soc, 1);

  html += R"rawliteral(
 %
</div>
</div>

<div class="card">
<h2>Status</h2>
<div class="value">
)rawliteral";

  html += detectFault();

  html += R"rawliteral(
</div>
</div>

</body>

</html>

)rawliteral";

  return html;
}

// --------------------------------------------------
// Web server
// --------------------------------------------------

void handleRoot()
{
  server.send(
      200,
      "text/html",
      htmlPage());
}

// --------------------------------------------------
// Setup
// --------------------------------------------------

void setup()
{
  Serial.begin(115200);

  Wire.begin();

  ina219.begin();

  tempSensor.begin();

  analogReadResolution(12);

  WiFi.begin(
      WIFI_SSID,
      WIFI_PASSWORD);

  Serial.print("Connecting WiFi");

  while (
      WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  Serial.println();

  Serial.print("ESP32 IP: ");
  Serial.println(WiFi.localIP());

  server.on(
      "/",
      handleRoot);

  server.begin();

  lastMeasurement = millis();
  lastUpload = millis();
}

// --------------------------------------------------
// Main loop
// --------------------------------------------------

void loop()
{
  server.handleClient();

  unsigned long now = millis();

  if (now - lastMeasurement >= 1000)
  {
    float dtHours =
        (now - lastMeasurement)
        / 3600000.0;

    lastMeasurement = now;

    readSensors();

    soc =
        calculateSOC(
          batteryVoltage,
          batteryCurrent,
          dtHours);

    energyWh +=
        batteryPower * dtHours;

    Serial.println("--------------------");

    Serial.print("Voltage: ");
    Serial.println(batteryVoltage);

    Serial.print("Current: ");
    Serial.println(batteryCurrent);

    Serial.print("Temperature: ");
    Serial.println(batteryTemperature);

    Serial.print("SOC: ");
    Serial.println(soc);

    Serial.print("Fault: ");
    Serial.println(detectFault());
  }

  if (now - lastUpload >= 15000)
  {
    lastUpload = now;

    sendToN8N();
  }
}

23. Important ESP32 Code Note

The example voltage/SOC table is deliberately a starting point.

Do not use it as a battery-management safety curve.

For an actual product, characterize the battery experimentally.

Measure:

Voltage
Current
Temperature
Discharged Ah

over several discharge cycles.

Then generate a battery-specific SOC curve.


24. Local IoT Webpage

The ESP32 creates a webpage such as:

http://192.168.1.50/

The page displays:

┌──────────────────────────────────┐
│       AI BATTERY MONITOR         │
├──────────────────────────────────┤
│                                  │
│ Voltage             15.72 V      │
│                                  │
│ Current              2.31 A      │
│                                  │
│ Temperature         32.4 °C      │
│                                  │
│ SOC                  72.5 %      │
│                                  │
│ Power                36.3 W      │
│                                  │
│ Status               NORMAL      │
│                                  │
└──────────────────────────────────┘

25. Improved Web Dashboard

For a more professional dashboard, use JavaScript polling:

ESP32
   │
   ▼
/api/status
   │
   ▼
JavaScript
   │
   ├── Voltage gauge
   ├── Current gauge
   ├── SOC gauge
   ├── Temperature
   └── Status indicator

Example API response:

{
  "voltage": 15.72,
  "current": 2.31,
  "temperature": 32.4,
  "soc": 72.5,
  "power": 36.3,
  "status": "NORMAL"
}

26. n8n Architecture

The n8n workflow should be divided into logical stages.

Webhook
   │
   ▼
Validate Data
   │
   ▼
Normalize Data
   │
   ├──────────────► Google Sheets
   │
   ├──────────────► ThingSpeak
   │
   ▼
Rule Engine
   │
   ▼
AI Agent
   │
   ▼
Decision
   │
   ├── NORMAL → Log only
   │
   ├── WARNING → Telegram
   │
   └── CRITICAL → Telegram + Voice

n8n's Telegram integration provides Telegram automation capabilities, while unsupported operations can also be accessed through HTTP/API calls. n8n Documentation


27. n8n Workflow Nodes

Create the following nodes.

Node 1

Webhook

Name:

Battery Telemetry Webhook

HTTP method:

POST

Path:

battery

Your ESP32 sends:

POST /webhook/battery

Node 2

Code / Function

Name:

Validate Telemetry

Example:

const d = $json;

const required = [
  "device_id",
  "voltage",
  "current",
  "temperature",
  "soc"
];

for (const key of required) {
  if (d[key] === undefined) {
    throw new Error(`Missing field: ${key}`);
  }
}

if (d.voltage < 0 || d.voltage > 100) {
  throw new Error("Invalid voltage");
}

if (d.soc < 0 || d.soc > 100) {
  throw new Error("Invalid SOC");
}

return [{
  json: {
    ...d,
    timestamp: new Date().toISOString(),
    valid: true
  }
}];

28. Data Normalization Node

Calculate:

power
SOC category
temperature status
battery status

Example:

const d = $json;

const power =
  Number(d.voltage) *
  Number(d.current);

let socStatus = "NORMAL";

if (d.soc <= 10) {
  socStatus = "CRITICAL";
}
else if (d.soc <= 20) {
  socStatus = "WARNING";
}

let temperatureStatus = "NORMAL";

if (d.temperature >= 55) {
  temperatureStatus = "CRITICAL";
}
else if (d.temperature >= 45) {
  temperatureStatus = "WARNING";
}

return [{
  json: {
    ...d,
    power,
    socStatus,
    temperatureStatus
  }
}];

29. Google Sheets

Create a spreadsheet:

AI Battery Monitoring

Worksheet:

Telemetry

Columns:

Timestamp
Device ID
Voltage
Current
Temperature
SOC
Power
Energy Wh
Charging
Fault
WiFi RSSI
AI Severity
AI Recommendation

Example:

2026-10-06 06:00
BATTERY-01
15.72
2.31
32.4
72.5
36.3
142.6
FALSE
NORMAL
-57
NORMAL
No action required

This creates a simple historical database.


30. ThingSpeak Integration

ThingSpeak can store numerical IoT data and provide charts.

ThingSpeak supports REST APIs for writing channel data. MathWorks+1

Create a channel called:

AI Battery Monitor

Configure:

Field 1 = Battery Voltage
Field 2 = Current
Field 3 = Temperature
Field 4 = SOC
Field 5 = Power
Field 6 = Energy
Field 7 = Battery Status

The ThingSpeak REST endpoint can be updated using HTTP POST/GET. MathWorks


31. ThingSpeak n8n HTTP Request

Create an n8n:

HTTP Request

Method:

POST

URL:

https://api.thingspeak.com/update.json

Parameters:

api_key = YOUR_WRITE_API_KEY

field1 = {{$json.voltage}}

field2 = {{$json.current}}

field3 = {{$json.temperature}}

field4 = {{$json.soc}}

field5 = {{$json.power}}

field6 = {{$json.energy_wh}}

ThingSpeak returns the created entry information when the update succeeds; a failed update returns 0. MathWorks


32. ThingSpeak Dashboard

The cloud dashboard can contain:

┌────────────────────────────────────┐
│       AI BATTERY CLOUD             │
├────────────────────────────────────┤
│                                    │
│ SOC                                │
│ ███████████████████░░░ 72 %        │
│                                    │
│ Voltage                            │
│ ────────────────╮                 │
│                 ╰────────           │
│                                    │
│ Current                            │
│ ────────────╮                     │
│             ╰────────               │
│                                    │
│ Temperature                        │
│ ────────────────                   │
│                                    │
└────────────────────────────────────┘

ThingSpeak supports both REST and MQTT approaches for channel updates; REST is particularly useful when you need HTTP request/response behavior. MathWorks


33. AI Agent

The n8n AI Agent receives the normalized battery data.

Example prompt:

You are an intelligent battery monitoring agent.

Analyze the battery telemetry.

Your responsibilities are:

1. Determine whether the battery is NORMAL,
   WARNING, or CRITICAL.

2. Look for:
   - Low SOC
   - Rapid SOC decrease
   - High temperature
   - Excessive current
   - Abnormal voltage
   - Voltage sag
   - Sensor failure
   - Charging anomalies

3. Do not invent sensor values.

4. Do not claim that a battery is safe when
   sensor information is insufficient.

5. Return valid JSON only.

Return:

{
  "severity": "NORMAL|WARNING|CRITICAL",
  "alert": true/false,
  "reason": "...",
  "recommendation": "...",
  "voice_message": "..."
}

34. Example AI Input

{
  "device_id": "BATTERY-01",
  "voltage": 13.4,
  "current": 5.8,
  "temperature": 48.2,
  "soc": 14.3,
  "power": 77.72,
  "charging": false,
  "soc_change_5min": -11.4
}

35. Example AI Output

{
  "severity": "WARNING",
  "alert": true,
  "reason": "SOC is low and decreasing rapidly while the battery is supplying a high load.",
  "recommendation": "Reduce the load and recharge the battery.",
  "voice_message": "Warning. Battery charge is low and decreasing rapidly. Please reduce the load and recharge the battery."
}

36. AI Agent Decision Flow

             Battery Data
                  │
                  ▼
          ┌───────────────┐
          │ AI Agent      │
          └───────┬───────┘
                  │
       ┌──────────┼───────────┐
       │          │           │
       ▼          ▼           ▼
    NORMAL     WARNING     CRITICAL
       │          │           │
       ▼          ▼           ▼
    Log only   Telegram    Telegram
               message     message
                              │
                              ▼
                         Voice Alert

37. Rule Engine

The AI should work together with deterministic rules.

Example:

SOC < 20%
       ↓
WARNING

SOC < 10%
       ↓
CRITICAL

Temperature > 45°C
       ↓
WARNING

Temperature > 55°C
       ↓
CRITICAL

Current > configured maximum
       ↓
CRITICAL

The exact thresholds must be configured for the particular battery, BMS and load.


38. Rapid SOC Drop Detection

One of the useful AI features is detecting:

SOC = 80%

followed shortly by:

SOC = 65%

while the current is normal.

The AI Agent can investigate:

Possible causes:
- incorrect SOC calibration
- battery degradation
- sensor error
- unexpected load
- voltage sag

39. Telegram Integration

Create a Telegram bot using BotFather.

The bot receives:

/battery
/status
/soc
/temperature
/history
/help

The Telegram Bot API supports sending voice messages using sendVoice. Telegram Core


40. Telegram Text Alert

Example:

🔋 BATTERY ALERT

Device: BATTERY-01

SOC: 14.3%
Voltage: 13.40 V
Current: 5.80 A
Temperature: 48.2 °C

Severity: WARNING

Reason:
SOC is low and decreasing rapidly.

Recommendation:
Reduce the load and recharge the battery.

41. Telegram Voice Alert

The n8n workflow can generate speech from:

voice_message

For example:

Warning. Battery charge is low and decreasing rapidly.
Please reduce the load and recharge the battery.

The generated audio is then passed to Telegram as a voice message.

Telegram's API specifically distinguishes voice messages from ordinary audio files. Telegram Core


42. Voice Alert Architecture

AI Agent
   │
   ▼
voice_message
   │
   ▼
Text-to-Speech
   │
   ▼
MP3/OGG audio
   │
   ▼
n8n Binary Data
   │
   ▼
Telegram Send Voice
   │
   ▼
User's phone

A TTS provider can be connected through an n8n integration or HTTP Request node.


43. Telegram Example Conversation

USER:
 /battery

BOT:
🔋 Battery Status

SOC: 72.5%
Voltage: 15.72 V
Current: 2.31 A
Temperature: 32.4°C
Power: 36.3 W

Status: NORMAL

Then:

USER:
/temperature

BOT:

🌡 Battery Temperature

Current: 32.4°C
Status: NORMAL

44. AI Telegram Conversation

A more advanced system can allow natural-language questions.

USER:

Why is my battery draining quickly?

The AI Agent can receive recent telemetry and answer:

The battery has dropped approximately
11% in the last 5 minutes.

The current load is 5.8 A and the
temperature is 48.2°C.

The most likely reason is high load
combined with low remaining capacity.

Recommendation:
Reduce the load and recharge the battery.

45. Complete n8n Workflow

Recommended workflow:

                  ┌──────────────┐
                  │ ESP32        │
                  └──────┬───────┘
                         │
                         ▼
                 ┌───────────────┐
                 │ Webhook       │
                 └──────┬────────┘
                        │
                        ▼
                 ┌───────────────┐
                 │ Validate      │
                 └──────┬────────┘
                        │
                        ▼
                 ┌───────────────┐
                 │ Normalize     │
                 └──────┬────────┘
                        │
           ┌────────────┼──────────────┐
           │            │              │
           ▼            ▼              ▼
     Google Sheets  ThingSpeak     Database
           │            │              │
           └────────────┼──────────────┘
                        │
                        ▼
                 ┌───────────────┐
                 │ Rule Engine   │
                 └──────┬────────┘
                        │
                        ▼
                 ┌───────────────┐
                 │ AI Agent      │
                 └──────┬────────┘
                        │
                        ▼
                 ┌───────────────┐
                 │ Parse JSON     │
                 └──────┬────────┘
                        │
                        ▼
                  ┌────────────┐
                  │ Alert?     │
                  └─────┬──────┘
                        │
               ┌────────┴────────┐
               │                 │
              NO                YES
               │                 │
               ▼                 ▼
             Finish       Telegram Text
                                 │
                                 ▼
                             TTS Engine
                                 │
                                 ▼
                           Telegram Voice

46. Recommended n8n Nodes

A practical workflow can contain:

1. Webhook
2. Code – Validate Data
3. Code – Calculate Derived Values
4. Google Sheets – Append Row
5. HTTP Request – ThingSpeak
6. IF – Safety Threshold
7. AI Agent
8. Structured Output Parser
9. IF – Alert Required
10. Telegram – Send Message
11. HTTP Request/TTS
12. Telegram – Send Voice
13. Respond to Webhook

47. AI Agent Tools

The AI Agent can be given tools such as:

Tool 1:
Get latest battery status

Tool 2:
Get recent historical data

Tool 3:
Get Google Sheets history

Tool 4:
Send Telegram notification

Tool 5:
Update device configuration

Tool 6:
Get ThingSpeak data

This is what makes the architecture more agentic rather than simply a fixed automation workflow.


48. Agentic IoT Architecture

Traditional IoT:

Sensor
  ↓
Cloud
  ↓
Dashboard

Agentic IoT:

Sensor
  ↓
ESP32
  ↓
Telemetry
  ↓
AI Agent
  ↓
Observe
  ↓
Reason
  ↓
Decide
  ↓
Act
  ↓
Notify / Log / Recommend

For example:

OBSERVE:
SOC = 12%
Temperature = 49°C
Current = 6A

REASON:
Low SOC + high temperature + high load

DECIDE:
WARNING

ACT:
Send Telegram notification
Generate voice alert
Record event

49. Battery Fault Classification

The system can classify:

NORMAL
LOW_SOC
HIGH_TEMPERATURE
OVER_CURRENT
LOW_VOLTAGE
RAPID_SOC_DROP
SENSOR_ERROR
COMMUNICATION_ERROR
POSSIBLE_BATTERY_DEGRADATION

50. Sensor Failure Detection

Suppose:

Voltage = 15.7 V
Current = 0 A
Temperature = -127°C

The ESP32/AI layer should recognize:

Temperature sensor failure

instead of reporting:

Battery temperature = -127°C

Similarly:

Voltage = 0 V
Current = 0 A

could indicate:

Battery disconnected

rather than necessarily meaning:

Battery is empty

51. Battery Degradation Detection

Over multiple cycles, record:

Cycle number
Maximum capacity
Minimum voltage
Temperature
Internal resistance estimate
SOC error

Suppose:

Cycle 1  → 10.0 Ah
Cycle 50 → 9.2 Ah
Cycle 100 → 8.4 Ah
Cycle 150 → 7.8 Ah

The AI Agent can identify:

Capacity is decreasing over time.
Possible battery degradation.

52. Internal Resistance Estimation

If load current changes rapidly:

ΔV
───
ΔI

can provide an approximate resistance:

R ≈ ΔV / ΔI

For example:

Before load:
V1 = 15.8 V

After load:
V2 = 15.2 V

Current change:
ΔI = 5 A

R ≈ 0.6 / 5
R ≈ 0.12 Ω

This should be treated as an estimate because wiring resistance, sensor delay, battery temperature and dynamic battery behavior also influence the measurement.


53. AI Anomaly Detection

Historical features can include:

SOC
Voltage
Current
Temperature
Power
Voltage sag
SOC derivative
Temperature derivative
Estimated resistance

The AI layer can detect patterns such as:

Normal:
SOC slowly decreases
Temperature stable
Voltage stable

versus:

Abnormal:
SOC rapidly decreases
Temperature rises rapidly
Voltage sag increases

54. Optional Machine-Learning SOC Model

For a more advanced academic/project implementation, collect thousands of samples.

Dataset:

timestamp
voltage
current
temperature
power
previous_soc
time_delta
measured_capacity
true_soc

Train a regression model:

Inputs:

Voltage
Current
Temperature
Power
Time
Previous SOC

          ↓

ML Model

          ↓

Estimated SOC

55. ML Training Pipeline

Battery Test
     │
     ▼
Data Logger
     │
     ▼
CSV Dataset
     │
     ▼
Python
     │
     ▼
Data Cleaning
     │
     ▼
Feature Engineering
     │
     ▼
ML Training
     │
     ▼
Validation
     │
     ▼
Model
     │
     ├── ESP32 TinyML
     │
     └── n8n/cloud inference

56. Example Python Training Code

A simple prototype can use Random Forest regression:

import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
import joblib

# Load dataset

df = pd.read_csv("battery_dataset.csv")

features = [
    "voltage",
    "current",
    "temperature",
    "power"
]

X = df[features]
y = df["true_soc"]

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42
)

model = RandomForestRegressor(
    n_estimators=200,
    random_state=42
)

model.fit(X_train, y_train)

prediction = model.predict(X_test)

mae = mean_absolute_error(
    y_test,
    prediction
)

print("MAE:", mae)

joblib.dump(
    model,
    "battery_soc_model.pkl"
)

The result can be evaluated using:

MAE
RMSE
R²

57. AI SOC Architecture

An advanced system can combine:

Coulomb SOC
       │
       ├─────────┐
       │         │
Voltage SOC      │
       │         │
       └────┬────┘
            │
            ▼
       ML Correction
            │
            ▼
        Final SOC

For example:

Coulomb SOC = 67%
Voltage SOC = 63%
ML estimate = 65%

Final SOC = 65%

The exact fusion algorithm should be validated against experimentally measured capacity.


58. Why Hybrid SOC Is Better

Voltage-only:

Simple
but load-sensitive

Coulomb-only:

Good short-term tracking
but accumulates error

AI-only:

Can model nonlinear behavior
but requires good training data

Hybrid:

Voltage
+
Coulomb counting
+
Temperature
+
ML correction

provides a much stronger engineering approach.


59. Complete Communication Architecture

             BATTERY
                │
                ▼
             ESP32
                │
          HTTPS / JSON
                │
                ▼
             n8n
                │
     ┌──────────┼────────────┐
     │          │            │
     ▼          ▼            ▼
 Google      ThingSpeak     AI
 Sheets                     Agent
                              │
                         ┌────┴─────┐
                         │          │
                         ▼          ▼
                     Telegram     TTS
                         │          │
                         └────┬─────┘
                              ▼
                       Voice Alert

60. Security

Do not hard-code production secrets into publicly shared firmware.

Avoid:

const char* API_KEY = "my-real-key";

in code that will be uploaded to GitHub.

Use:

Environment variables
Secret manager
n8n credentials
Device-specific credentials

At minimum:

HTTPS
Webhook authentication
Telegram bot token protection
ThingSpeak API key protection
Wi-Fi credentials protection

61. ESP32 → n8n Authentication

A better approach than an open webhook is:

ESP32
  │
  │ Authorization: Bearer DEVICE_SECRET
  ▼
n8n

The ESP32 sends:

Authorization: Bearer YOUR_DEVICE_SECRET
Content-Type: application/json

n8n verifies the secret before accepting telemetry.


62. Example ESP32 Authentication

Add:

http.addHeader(
  "Authorization",
  "Bearer YOUR_DEVICE_SECRET"
);

Then n8n validates it.

For production systems, use device-specific credentials rather than one global token.


63. Telegram Security

Only allow authorized users.

The workflow should verify:

Telegram chat_id

against an allowlist.

Example:

AUTHORIZED_CHAT_ID_1
AUTHORIZED_CHAT_ID_2

If an unknown user sends:

/status

the bot should respond:

Unauthorized user.

64. Google Sheets Security

Do not make the spreadsheet public unless the project specifically requires it.

Recommended:

Private Sheet
      ↓
n8n authenticated account
      ↓
Append telemetry

65. ThingSpeak Security

Keep the Write API Key private.

Use:

ESP32 → n8n → ThingSpeak

rather than exposing the ThingSpeak write key unnecessarily on the ESP32.

ThingSpeak provides separate channel/API mechanisms for reading and writing channel data. MathWorks


66. Testing Procedure

Test 1 — ESP32 boot

Expected:

ESP32 starts
Wi-Fi connects
IP address displayed

Test 2 — Voltage

Use a multimeter.

Compare:

Multimeter = 15.72 V
ESP32 = 15.70 V

Calculate error:

Error =
|15.72 - 15.70| / 15.72 × 100

Test 3 — Current

Use a calibrated current meter.

Compare:

Reference = 2.30 A
ESP32 = 2.31 A

67. Temperature Test

Place the sensor on the battery.

Compare against a trusted thermometer.

Test:

Room temperature
Warm battery
High-load condition

68. SOC Calibration Test

Start from a known full battery.

Record:

100%

Then discharge using a controlled load.

Record:

95%
90%
85%
...
10%
5%

Calculate actual consumed Ah.

Generate the battery-specific SOC curve.


69. n8n Testing

Send test JSON manually:

{
  "device_id": "TEST-01",
  "voltage": 13.2,
  "current": 5.5,
  "temperature": 49,
  "soc": 12,
  "power": 72.6,
  "energy_wh": 120,
  "charging": false
}

Expected:

Webhook
 ↓
Validation
 ↓
Google Sheets
 ↓
ThingSpeak
 ↓
AI Agent
 ↓
WARNING
 ↓
Telegram

70. Critical Alert Test

Send:

{
  "device_id": "TEST-01",
  "voltage": 11.8,
  "current": 8.2,
  "temperature": 58,
  "soc": 5
}

Expected:

Severity = CRITICAL

Then:

Telegram text
+
Telegram voice

should be generated.


71. Network Failure Test

Turn off Wi-Fi.

Expected:

ESP32 continues measuring
ESP32 continues calculating SOC
Local webpage may remain available if local network is available
Cloud upload fails

The firmware should not crash because the cloud service is unavailable.


72. n8n Failure Test

Stop n8n.

Expected:

ESP32 continues operating

When n8n returns:

ESP32 reconnects
Telemetry resumes

A production version can add local buffering in ESP32 flash/SD card.


73. Recommended Offline Buffer

Advanced ESP32 architecture:

Sensor
 ↓
SOC
 ↓
RAM buffer
 ↓
Wi-Fi available?
 ├── YES → Upload
 └── NO  → Store locally

Then:

Wi-Fi returns
     ↓
Upload buffered records

74. Final Project Flowchart

              START
                │
                ▼
        Initialize ESP32
                │
                ▼
        Connect to Wi-Fi
                │
                ▼
        Initialize Sensors
                │
                ▼
       Read Battery Voltage
                │
                ▼
       Read Battery Current
                │
                ▼
       Read Temperature
                │
                ▼
      Calculate Battery Power
                │
                ▼
        Calculate SOC
                │
                ▼
        Check Local Faults
                │
                ▼
         Update Webpage
                │
                ▼
       Send Telemetry to n8n
                │
                ▼
        n8n Validates Data
                │
                ▼
        Store Google Sheets
                │
                ▼
        Update ThingSpeak
                │
                ▼
          AI Agent
                │
                ▼
        Analyze Condition
                │
                ▼
        ┌───────┴────────┐
        │                │
      NORMAL           ALERT
        │                │
        ▼                ▼
       Log         Telegram Text
                         │
                         ▼
                       TTS
                         │
                         ▼
                  Telegram Voice
                         │
                         ▼
                       END

75. Complete Hardware BOM

Recommended prototype BOM:

1 × ESP32 DevKit
1 × INA219 or INA226 module
1 × DS18B20 temperature sensor
1 × 4S Li-ion battery pack
1 × 4S BMS
1 × 100 kΩ resistor
1 × 22 kΩ resistor
1 × 4.7 kΩ resistor
1 × DC-DC buck converter
1 × Fuse
1 × Prototype PCB
1 × OLED display (optional)
1 × enclosure

For high-current batteries, use a properly rated current sensor, wiring, connector, fuse and protection hardware. Do not route high battery current through a small breadboard.


76. Software BOM

Arduino IDE
ESP32 Arduino Core

Libraries:
- WiFi
- WebServer
- HTTPClient
- ArduinoJson
- Wire
- Adafruit INA219
- OneWire
- DallasTemperature

Cloud:
- n8n
- Google Sheets
- ThingSpeak
- Telegram Bot

Optional:
- AI model/API
- TTS provider
- Python
- scikit-learn
- joblib

77. Final System Architecture

                         ┌────────────────────┐
                         │   4S BATTERY/BMS   │
                         └─────────┬──────────┘
                                   │
                    ┌──────────────┼──────────────┐
                    │              │              │
                    ▼              ▼              ▼
                Voltage         Current       Temperature
                 Sensor          Sensor          DS18B20
                    │              │              │
                    └──────────────┼──────────────┘
                                   ▼
                         ┌──────────────────┐
                         │      ESP32       │
                         │                  │
                         │ Sensor Reading   │
                         │ SOC Estimation   │
                         │ Energy           │
                         │ Fault Detection  │
                         │ Web Server       │
                         └────────┬─────────┘
                                  │
                                HTTPS
                                  │
                                  ▼
                         ┌──────────────────┐
                         │       n8n        │
                         │                  │
                         │ Webhook          │
                         │ Validation       │
                         │ Automation       │
                         │ AI Agent         │
                         └───────┬──────────┘
                                 │
             ┌───────────────────┼────────────────────┐
             │                   │                    │
             ▼                   ▼                    ▼
      ┌─────────────┐     ┌─────────────┐     ┌──────────────┐
      │Google Sheets│     │ ThingSpeak  │     │  AI Agent    │
      │             │     │             │     │              │
      │History      │     │Cloud Charts │     │Reasoning     │
      │Data Logger  │     │Telemetry    │     │Anomaly       │
      └─────────────┘     └─────────────┘     └──────┬───────┘
                                                      │
                                                      ▼
                                               ┌─────────────┐
                                               │ Decision    │
                                               └──────┬──────┘
                                                      │
                                             ┌────────┴────────┐
                                             │                 │
                                           NORMAL             ALERT
                                             │                 │
                                             ▼                 ▼
                                           LOG          Telegram Text
                                                               │
                                                               ▼
                                                           TTS Engine
                                                               │
                                                               ▼
                                                        Telegram Voice

78. Expected Final Output

When the system is operating normally:

ESP32
 │
 ├─ Voltage: 15.72 V
 ├─ Current: 2.31 A
 ├─ Temperature: 32.4 °C
 ├─ SOC: 72.5 %
 └─ Power: 36.3 W

        ↓

n8n

        ↓

Google Sheets ✓
ThingSpeak ✓
AI Agent ✓
Telegram: No alert

When a problem occurs:

ESP32
 │
 ├─ Voltage: 13.4 V
 ├─ Current: 5.8 A
 ├─ Temperature: 48.2 °C
 └─ SOC: 14.3 %

        ↓

n8n

        ↓

AI Agent

        ↓

WARNING

        ↓

Telegram Text
        +
Telegram Voice

        ↓

"Warning. Battery charge is low
and decreasing rapidly. Please
reduce the load and recharge
the battery."

79. Project Advantages

This architecture combines five different technologies:

Embedded system

ESP32 measures and processes the physical battery.

IoT

Wi-Fi transfers telemetry to the cloud.

Automation

n8n connects all services.

Artificial intelligence

The AI Agent interprets telemetry and historical behavior.

Voice notification

Telegram delivers the warning as both text and voice.

Therefore, the project is more than a conventional battery monitor.

It is an:

AI-powered Agentic IoT Battery Monitoring and Decision-Support System.


80. Suggested Project/Thesis Chapter Structure

For an academic project report, use:

Chapter 1
Introduction

Chapter 2
Literature Survey

Chapter 3
System Requirements

Chapter 4
Hardware Design

Chapter 5
Battery SOC Estimation

Chapter 6
ESP32 Firmware

Chapter 7
IoT Web Interface

Chapter 8
n8n Automation

Chapter 9
AI Agent Design

Chapter 10
Telegram Voice Alert System

Chapter 11
Google Sheets Integration

Chapter 12
ThingSpeak Cloud Dashboard

Chapter 13
Testing and Results

Chapter 14
Performance Analysis

Chapter 15
Limitations

Chapter 16
Future Scope

Chapter 17
Conclusion

81. Future Enhancements

The project can be upgraded with:

✓ TinyML SOC estimation
✓ LSTM battery prediction
✓ Remaining Useful Life (RUL)
✓ Battery State of Health (SOH)
✓ Internal resistance estimation
✓ Cell-level monitoring
✓ CAN bus BMS integration
✓ MQTT
✓ OTA ESP32 firmware updates
✓ Mobile application
✓ Voice commands
✓ AI-generated maintenance reports
✓ Predictive failure detection
✓ Solar charging integration
✓ EV battery monitoring
✓ Multi-battery monitoring

For example:

Battery SOC
     +
Battery SOH
     +
Temperature
     +
Current
     +
Historical cycles
     ↓
AI Predictive Model
     ↓
Remaining Useful Life

82. Important Engineering Limitation

This project is suitable for:

  • educational projects

  • IoT prototypes

  • research

  • demonstrations

  • battery telemetry

  • non-safety-critical monitoring

It should not replace the battery manufacturer's BMS or certified protection circuitry.

The BMS remains responsible for electrical protection such as:

Overcharge
Over-discharge
Overcurrent
Short circuit
Cell imbalance
Temperature protection

The ESP32/n8n/AI system should be considered a monitoring and decision-support system, unless the entire protection architecture is engineered and certified for the intended application.


83. Final One-Line Architecture

The complete project can be summarized as:

BATTERY
   ↓
V/I/T SENSORS
   ↓
ESP32
   ↓
HYBRID SOC ESTIMATION
   ↓
Wi-Fi / HTTPS
   ↓
n8n
   ↓
AI AGENT
   ↓
┌────────────┬─────────────┬───────────────┐
│            │             │               │
Google       ThingSpeak    Telegram       AI
Sheets       Dashboard     Text/Voice      Analysis

This gives you a complete ESP32 + AI + Agentic IoT + n8n + Telegram Voice + Google Sheets + ThingSpeak battery SOC platform.

Current implementation notes

A few pieces are worth treating as production-hardening steps rather than copy/paste defaults:

  • The SOC lookup table in the firmware must be calibrated for your exact battery chemistry, capacity and discharge conditions.

  • For lithium batteries, keep the physical BMS independent of the ESP32/software layer.

  • ThingSpeak's REST API supports channel writes and historical reads, so it works well as the cloud visualization/history layer. MathWorks+1

  • Telegram's current Bot API supports sendVoice, which is the appropriate mechanism for Telegram-style voice messages. Telegram Core

  • n8n has a native Telegram integration, while HTTP Request nodes can be used for services that don't have a dedicated node. n8n Documentation

If you want to turn this into a fully buildable project, the next useful step is to define the exact battery—e.g. 12 V lead-acid, 4S Li-ion 14.8 V/16.8 V, or 4S LiFePO4—because that determines the correct SOC curve, sensor range, thresholds and calibration procedure.

Project Summary

AI-Powered Battery State-of-Charge (SOC) Estimation & Agentic IoT System is an ESP32-based smart battery-monitoring platform that combines embedded sensing, AI, cloud dashboards and automated notifications.

Core workflow

Battery
   ↓
Voltage + Current + Temperature Sensors
   ↓
ESP32
   ↓
Hybrid SOC Estimation
   ↓
Wi-Fi / HTTPS
   ↓
n8n Automation
   ↓
AI Agent
   ↓
┌──────────────┬──────────────┬──────────────┐
Google Sheets  ThingSpeak     Telegram
Data Logging   Dashboard      Text + Voice

Main functions

  • ESP32 measures battery voltage, current and temperature.

  • SOC algorithm combines voltage and coulomb counting, with optional ML correction.

  • Local IoT webpage displays real-time voltage, current, temperature, SOC, power and status.

  • n8n receives telemetry and automates the complete workflow.

  • AI Agent analyzes battery conditions and identifies anomalies such as low SOC, rapid discharge, overheating and abnormal voltage.

  • Google Sheets stores historical battery data.

  • ThingSpeak provides cloud visualization and historical graphs.

  • Telegram sends automatic text alerts.

  • Text-to-Speech + Telegram provides voice warnings.

  • Local ESP32 safety rules continue operating even if Wi-Fi, n8n or AI services fail.

Example alert

🔋 BATTERY ALERT

SOC: 14.3%
Voltage: 13.40 V
Current: 5.80 A
Temperature: 48.2 °C

Severity: WARNING

Reason:
Battery SOC is low and decreasing rapidly.

Recommendation:
Reduce the load and recharge the battery.

Advanced capabilities

The project can later be expanded to:

  • AI/ML SOC prediction

  • State of Health (SOH)

  • Remaining Useful Life (RUL)

  • Battery degradation detection

  • Internal-resistance estimation

  • Cell-level monitoring

  • Predictive failure detection

  • CAN-BMS integration

  • OTA ESP32 updates

  • Voice commands

  • Multi-battery monitoring

In short: the project transforms a conventional ESP32 battery monitor into an AI-powered Agentic IoT battery-management and notification system, with real-time monitoring, historical data, intelligent analysis and automated Telegram voice alerts.

 

Real-Time Solar Panel Monitoring Using IoT, n8n & AI Voice Alerts | ThingSpeak + Telegram + Gmail Automation + Web Dash Board Alerts

1.SolarPulse AI: An IoT-Enabled Intelligent Framework for Real-Time Solar PV Performance Monitoring and Multi-Channel AI Alerting.
2.Real-Time Solar Panel Monitoring Using IoT, n8n Cloud and AI Voice Alerts.
3.AI-Powered IoT Solar PV Monitoring and Multi-Channel Alert Automation System.
4.An IoT-Based Intelligent Solar PV Performance Monitoring and Fault Alert System.
5.Smart Solar Panel Monitoring Using IoT, AI and n8n Cloud Automation.
6.SolarSense AI: Intelligent IoT-Based Solar PV Performance Analytics and Alert System.
7.An IoT-Enabled AI Framework for Real-Time Solar Photovoltaic Monitoring and Automated Notifications.
8.SolarWatch AI: Predictive Solar PV Monitoring with Intelligent Voice and Mobile Alerts.
9.Real-Time Solar PV Performance Analytics and Adaptive Alert Generation Using IoT and AI.
10.SolarGuard AI: Intelligent Solar Panel Health Monitoring and Multi-Channel Alert System.
11.AI-Assisted Real-Time Monitoring and Intelligent Fault Notification of Solar Photovoltaic Systems.
12.Smart Solar Energy Monitoring Using IoT, ThingSpeak, n8n and AI Voice Notifications.
13.SolarVision AI: Real-Time Solar PV Analytics with Automated Multi-Channel Notifications.
14.An IoT-Based Multi-Parameter Solar PV Monitoring System with AI-Generated Voice Alerts.
15.SolarIQ: Intelligent IoT-Based Solar Performance Monitoring and Fault Detection System.
16.Real-Time Solar Panel Voltage, Current, Power and Temperature Monitoring Using IoT and AI.
17.SunTrack AI: IoT-Based Smart Solar PV Monitoring and Automated Alert System.
18.SolarShield: Intelligent Solar Panel Health and Performance Monitoring Using IoT and AI.
19.An Intelligent Cloud-Connected Framework for Solar PV Monitoring, Diagnostics and Automated Alerts.
20.SolarSentinel: Autonomous AI-Based Solar Panel Health Monitoring and Alert Intelligence.
21.AI-Powered Solar PV Performance Monitoring with n8n Cloud and Multi-Channel Notifications.
22.IoT Solar Panel Monitoring with AI Alerts | Real-Time Voltage, Current and Power Tracking.
23.SolarNex: A Self-Monitoring IoT Architecture for Intelligent Photovoltaic Systems.
24.SunPulse AI: An Intelligent IoT Platform for Real-Time Solar Energy Performance Monitoring.
25.Smart Solar PV Monitoring and Set-Point Detection Using IoT, AI and Cloud Automation.
26.SolarGuardian AI: Intelligent Multi-Channel PV Anomaly Detection and Response System.
27.Real-Time Solar Panel Performance Monitoring with ThingSpeak, n8n, Telegram and Gmail.
28.SolarMind: Context-Aware AI Alerting for Real-Time Solar Energy Infrastructure.
29.IoT-Based Solar PV Monitoring with Automated Gmail, Telegram and AI Voice Alerts.
30.Advanced Solar Panel Performance Monitoring Using IoT, Cloud Analytics and AI Automation.
31.SolarPulse 360: AI-Driven Solar PV Performance, Environmental and Fault Monitoring.
32.Intelligent Solar Energy Monitoring Using IoT Sensors, Cloud Analytics and AI Notifications.
33.AI-Enabled Solar PV Anomaly Detection and Automated Multi-Channel Alert Generation.
34.Smart Solar Panel Performance Tracking Using IoT, Google Sheets, ThingSpeak and n8n.
35.Real-Time Solar Energy Monitoring and Intelligent Alert Automation Using n8n Cloud.
36.Solar PV Fault and Set-Point Detection Using IoT, AI and Automated Voice Notifications.
37.IoT-Based Solar Energy Monitoring with Real-Time Dashboard and AI-Powered Alerts.
38.AI-Driven Solar PV Diagnostics and Multi-Channel Notification Framework Using IoT.
39.Intelligent Solar Panel Monitoring and Predictive Alert System Using IoT and AI.
40.Cloud-Connected Solar PV Monitoring System with AI Voice, Telegram and Gmail Alerts.
41.Smart Solar Monitoring 360: IoT-Based Performance, Environmental and Alert Management.
42.AI-Powered Real-Time Solar PV Monitoring and Automated Notification System Using n8n.
43.Complete IoT Solar Monitoring Framework with ThingSpeak, Google Sheets and AI Alerts.
44.Solar PV Performance Intelligence Platform Using IoT Sensors, Cloud Automation and AI.
45.Real-Time Solar Panel Health Monitoring and Automated Fault Alerts Using IoT and AI.
46.IoT-Based Intelligent Solar Energy Analytics with Set-Point Detection and AI Voice Alerts.
47.Next-Generation Solar PV Monitoring System Using IoT, AI and Multi-Channel Automation.
48.Autonomous Solar Panel Performance Monitoring and Intelligent Alert Management Using IoT.
49.AI-Driven IoT Solar PV Monitoring with Real-Time Analytics and Multi-Channel Notifications.
50.Smart Solar PV Performance Monitoring and AI Alert Automation Using IoT, n8n and ThingSpeak.

Sunday, 4 October 2026

AI Automated Irrigation System using Crop Prediction

Absolutely . I can structure this as a complete final-year/academic IoT project documentation for an:

AI-Powered Automated Irrigation System Using Crop Prediction

ESP32 + Sensors + AI Agent + n8n Automation + Telegram Voice Alerts + Google Sheets + ThingSpeak Cloud Dashboard

The system can automatically monitor soil and environmental conditions, predict the crop/irrigation requirement, control a water pump through an ESP32, log data to Google Sheets/ThingSpeak, and use an AI Agent through n8n to generate intelligent alerts and recommendations.

I can provide the full project in the following structure:

1. Project Architecture

                  ┌─────────────────────────┐
                  │       FARM / FIELD      │
                  │                         │
                  │ Soil Moisture Sensor    │
                  │ Temperature Sensor      │
                  │ Humidity Sensor         │
                  │ Rain Sensor             │
                  │ Water Level Sensor      │
                  └───────────┬─────────────┘
                              │
                              ▼
                    ┌──────────────────┐
                    │      ESP32       │
                    │                  │
                    │ Sensor Reading   │
                    │ Decision Logic   │
                    │ Wi-Fi            │
                    └────────┬─────────┘
                             │
                 ┌───────────┼───────────────┐
                 │           │               │
                 ▼           ▼               ▼
          ┌──────────┐ ┌───────────┐ ┌─────────────┐
          │ Water    │ │ ThingSpeak│ │    n8n      │
          │ Pump     │ │ Dashboard │ │ Automation  │
          └──────────┘ └───────────┘ └──────┬──────┘
                                            │
                             ┌──────────────┼──────────────┐
                             ▼              ▼              ▼
                       ┌──────────┐  ┌───────────┐  ┌──────────┐
                       │ AI Agent │  │  Google   │  │ Telegram │
                       │          │  │  Sheets   │  │ Alerts   │
                       └────┬─────┘  └───────────┘  └────┬─────┘
                            │                            │
                            ▼                            ▼
                     Crop/Irrigation              Text / Voice
                       Analysis                     Notification

2. Main Objective

The objective is to develop an intelligent automated irrigation system that determines when irrigation is required by combining:

  • Soil moisture

  • Temperature

  • Relative humidity

  • Rain detection

  • Water-tank level

  • Crop information

  • Crop growth stage

  • AI-based prediction

  • Historical irrigation data

Instead of continuously running the pump, the ESP32 and automation system determine whether irrigation is actually required.

Basic principle

Sensor Data
     ↓
ESP32
     ↓
Internet
     ↓
n8n Workflow
     ↓
AI Agent
     ↓
Analyze Crop + Environment
     ↓
Irrigation Decision
     ↓
ESP32
     ↓
Pump ON/OFF
     ↓
Cloud Logging
     ↓
Telegram Alert

3. Proposed Features

Hardware

  • ESP32 development board

  • Capacitive soil-moisture sensor

  • DHT22/DHT11 temperature-humidity sensor

  • Rain sensor

  • Water-level sensor

  • Relay module

  • DC water pump

  • External pump power supply

  • Optional flow sensor

  • Optional LCD/OLED

  • Wi-Fi connection

Software

  • Arduino IDE

  • ESP32 Arduino framework

  • n8n

  • Telegram Bot

  • Google Sheets

  • ThingSpeak

  • AI/LLM API

  • Optional web dashboard


4. Overall System Flow

                    START
                      │
                      ▼
              ESP32 initializes
                      │
                      ▼
                Connect Wi-Fi
                      │
                      ▼
             Read all sensors
                      │
                      ▼
       ┌───────────────────────────┐
       │ Soil moisture sufficiently │
       │ high?                      │
       └─────────────┬─────────────┘
                     │
               YES   │   NO
                │    │
                ▼    ▼
             Pump   Check
             OFF    weather/
                    rain/tank
                     │
                     ▼
              Send data to n8n
                     │
                     ▼
                AI Agent
                     │
          ┌──────────┴──────────┐
          │                     │
      Irrigation              No
       required             irrigation
          │                     │
          ▼                     ▼
       Pump ON                Pump OFF
          │                     │
          └──────────┬──────────┘
                     ▼
               Record data
                     │
              ┌──────┴──────┐
              ▼             ▼
        Google Sheets   ThingSpeak
              │
              ▼
          Telegram
              │
              ▼
       Voice/Text Alert
              │
              ▼
           Repeat

5. Hardware Block Diagram

                     ┌───────────────┐
                     │     ESP32     │
                     │               │
                     │ GPIO / ADC    │
                     │ Wi-Fi         │
                     └───────┬───────┘
                             │
       ┌─────────────────────┼──────────────────────┐
       │                     │                      │
       ▼                     ▼                      ▼
┌────────────┐       ┌──────────────┐       ┌─────────────┐
│ Soil       │       │ DHT11/DHT22  │       │ Rain Sensor │
│ Moisture   │       │ Temp/Humidity │       │             │
└────────────┘       └──────────────┘       └─────────────┘
       │
       │
       ▼
┌───────────────┐
│ Water Level   │
│ Sensor        │
└───────────────┘

                     ESP32
                       │
                       ▼
                 ┌──────────┐
                 │  Relay   │
                 └────┬─────┘
                      │
                      ▼
                 ┌──────────┐
                 │   Pump   │
                 └────┬─────┘
                      │
                      ▼
                    FIELD

6. Example Electrical Schematic

A typical low-voltage prototype can be wired approximately as follows:

                 ESP32
          ┌──────────────────┐
          │                  │
          │ 3.3V ─────────────── Sensor VCC
          │ GND  ─────────────── Sensor GND
          │                  │
          │ GPIO34 ◄──────────── Soil Moisture AO
          │ GPIO4  ◄──────────── DHT DATA
          │ GPIO35 ◄──────────── Rain Sensor
          │ GPIO32 ◄──────────── Water Level
          │                  │
          │ GPIO26 ───────────── Relay IN
          │                  │
          └──────────────────┘
                       │
                       ▼
                  ┌─────────┐
                  │ RELAY   │
                  │ MODULE  │
                  └────┬────┘
                       │
                       ▼
              ┌────────────────┐
              │ External       │
              │ Pump Supply    │
              └───────┬────────┘
                      │
                      ▼
                   DC PUMP

Important: The pump should normally have its own correctly rated power supply. Do not power a pump directly from an ESP32 GPIO pin. For mains-powered pumps, use an appropriately rated isolated switching arrangement and have the mains portion installed/tested by a qualified person.


7. Suggested GPIO Assignment

Component ESP32 Pin
Soil moisture analog output GPIO 34
DHT22 data GPIO 4
Rain sensor GPIO 35
Water-level sensor GPIO 32
Relay GPIO 26
Optional flow sensor GPIO 27
OLED SDA GPIO 21
OLED SCL GPIO 22

GPIO assignments can be changed depending on the ESP32 board and sensor modules used.


8. How the AI Component Works

The AI should not blindly control the pump.

Instead, the ESP32 collects measurements and sends a structured data packet.

Example:

{
  "soil_moisture": 31,
  "temperature": 34.2,
  "humidity": 48,
  "rain_detected": false,
  "water_level": 72,
  "crop": "Tomato",
  "growth_stage": "Flowering"
}

n8n receives this information.

The AI Agent analyzes the data:

Soil moisture = 31%
Temperature = 34.2°C
Humidity = 48%
Rain = No
Tank = 72%
Crop = Tomato
Stage = Flowering

It could return a structured decision such as:

{
  "irrigation_required": true,
  "duration_minutes": 8,
  "priority": "high",
  "reason": "Low soil moisture and high temperature",
  "alert_required": true
}

The n8n workflow can then validate this response before sending a pump command.


9. Crop Prediction Module

The crop-prediction component can operate at two levels.

Level 1 — Crop selection

The user supplies or selects:

Crop:
Tomato

or the AI predicts a likely crop from available agricultural/environmental information.

Level 2 — Crop-specific irrigation prediction

Different crops have different water requirements.

For example:

Crop
  ↓
Growth Stage
  ↓
Soil Moisture
  ↓
Temperature
  ↓
Humidity
  ↓
Rain Forecast/Detection
  ↓
Historical Irrigation
  ↓
AI Prediction
  ↓
Recommended Irrigation

For an academic project, I recommend making crop type + growth stage explicit inputs rather than claiming that an LLM itself is a scientifically validated crop classifier.


10. n8n Automation Architecture

The n8n workflow can be designed as:

                    ESP32 HTTP Request
                           │
                           ▼
                    ┌─────────────┐
                    │ Webhook     │
                    └──────┬──────┘
                           │
                           ▼
                    ┌─────────────┐
                    │ Validate    │
                    │ Sensor Data │
                    └──────┬──────┘
                           │
                           ▼
                    ┌─────────────┐
                    │ Google      │
                    │ Sheets Log  │
                    └──────┬──────┘
                           │
                           ▼
                    ┌─────────────┐
                    │ AI Agent    │
                    └──────┬──────┘
                           │
                           ▼
                    ┌─────────────┐
                    │ Parse AI    │
                    │ Decision    │
                    └──────┬──────┘
                           │
                           ▼
                  ┌──────────────────┐
                  │ Safety Validation│
                  └────────┬─────────┘
                           │
                  ┌────────┴────────┐
                  │                 │
                YES                 NO
                  │                 │
                  ▼                 ▼
             ESP32 Pump          Pump OFF
               Command               │
                  │                  │
                  └────────┬─────────┘
                           ▼
                    ┌─────────────┐
                    │ ThingSpeak  │
                    │ Update      │
                    └──────┬──────┘
                           │
                           ▼
                    ┌─────────────┐
                    │ Telegram    │
                    │ Notification│
                    └─────────────┘

11. n8n Nodes

A practical workflow can contain:

  1. Webhook

  2. Set/Edit Fields

  3. IF – Validate Sensor Values

  4. Google Sheets – Append Row

  5. AI Agent

  6. Structured Output Parser

  7. IF – Irrigation Required

  8. HTTP Request – ESP32

  9. ThingSpeak HTTP Request

  10. Telegram

  11. Google Sheets – Update Result

  12. Error/Alert branch


12. AI Agent Prompt

The AI Agent should receive structured sensor data rather than an unstructured paragraph.

Example system instruction:

You are an agricultural irrigation decision assistant.

Analyze the supplied crop, growth stage, soil moisture,
temperature, humidity, rainfall status, water level and
historical irrigation information.

Your job is to recommend whether irrigation is required.

Never recommend irrigation when:
1. The water tank is critically low.
2. Rain is currently detected.
3. Sensor values are invalid.
4. The system reports a hardware fault.

Return ONLY valid JSON using this schema:

{
  "irrigation_required": true,
  "duration_minutes": 5,
  "priority": "low",
  "reason": "string",
  "alert_required": true
}

Do not invent sensor values.
Do not directly claim that irrigation is scientifically optimal.
Treat your answer as a recommendation subject to safety validation.

This is an important architectural improvement: the AI makes a recommendation, while deterministic safety logic has the final authority over the pump.


13. ESP32 → n8n Data

The ESP32 can send an HTTP POST request:

POST /webhook/irrigation
Content-Type: application/json

with:

{
  "device_id": "ESP32_FIELD_01",
  "soil_moisture": 28,
  "temperature": 33.5,
  "humidity": 51,
  "rain": false,
  "water_level": 78,
  "crop": "Tomato",
  "growth_stage": "Flowering"
}

14. ESP32 Arduino Code

Below is a starter implementation for the ESP32.

#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22

#define SOIL_PIN 34
#define RAIN_PIN 35
#define WATER_LEVEL_PIN 32

#define RELAY_PIN 26

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASSWORD = "YOUR_PASSWORD";

const char* N8N_URL =
  "https://YOUR-N8N-DOMAIN/webhook/irrigation";

DHT dht(DHTPIN, DHTTYPE);

void setup() {

  Serial.begin(115200);

  pinMode(RELAY_PIN, OUTPUT);

  // Pump OFF initially
  digitalWrite(RELAY_PIN, LOW);

  dht.begin();

  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");
}

void loop() {

  int soilRaw = analogRead(SOIL_PIN);
  int rainRaw = analogRead(RAIN_PIN);
  int waterRaw = analogRead(WATER_LEVEL_PIN);

  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("DHT sensor error");
    delay(5000);
    return;
  }

  // These values must be calibrated for the actual sensors.
  int soilMoisture =
      map(soilRaw, 4095, 1500, 0, 100);

  soilMoisture = constrain(soilMoisture, 0, 100);

  int waterLevel =
      map(waterRaw, 1000, 3000, 0, 100);

  waterLevel = constrain(waterLevel, 0, 100);

  bool rainDetected = rainRaw < 1500;

  Serial.println("------ SENSOR DATA ------");

  Serial.print("Soil: ");
  Serial.println(soilMoisture);

  Serial.print("Temperature: ");
  Serial.println(temperature);

  Serial.print("Humidity: ");
  Serial.println(humidity);

  Serial.print("Rain: ");
  Serial.println(rainDetected);

  Serial.print("Water Level: ");
  Serial.println(waterLevel);

  if (WiFi.status() == WL_CONNECTED) {

    HTTPClient http;

    http.begin(N8N_URL);

    http.addHeader(
      "Content-Type",
      "application/json"
    );

    String json = "{";

    json += "\"device_id\":\"ESP32_FIELD_01\",";
    json += "\"soil_moisture\":" +
            String(soilMoisture) + ",";
    json += "\"temperature\":" +
            String(temperature) + ",";
    json += "\"humidity\":" +
            String(humidity) + ",";
    json += "\"rain\":" +
            String(rainDetected ? "true" : "false") + ",";
    json += "\"water_level\":" +
            String(waterLevel) + ",";
    json += "\"crop\":\"Tomato\",";
    json += "\"growth_stage\":\"Flowering\"";

    json += "}";

    Serial.println(json);

    int responseCode =
      http.POST(json);

    Serial.print("HTTP Response: ");
    Serial.println(responseCode);

    String response =
      http.getString();

    Serial.println(response);

    http.end();
  }

  delay(60000);
}

15. Important Sensor Calibration

Do not assume the map() values above represent your actual sensors.

For the soil sensor, record:

Completely dry soil → ADC value
Wet soil             → ADC value

For example:

Dry = 3500
Wet = 1500

Then calibrate:

int moisture = map(
    soilRaw,
    3500,
    1500,
    0,
    100
);

The exact values depend on the sensor, soil and ESP32 ADC configuration.


16. Pump-Control Safety

A better architecture is:

                 AI Recommendation
                        │
                        ▼
               ┌─────────────────┐
               │ Safety Rules    │
               └────────┬────────┘
                        │
       ┌────────────────┼────────────────┐
       │                │                │
       ▼                ▼                ▼
 Tank OK?          Rain absent?     Sensor valid?
       │                │                │
       └────────────────┼────────────────┘
                        ▼
                 ALL CONDITIONS OK
                        │
                        ▼
                    Pump ON

Never allow an LLM response such as:

{"irrigation_required":true}

to directly energize the pump without validation.


17. Telegram Alert System

When irrigation starts:

🌱 IRRIGATION ALERT

Crop: Tomato
Growth Stage: Flowering

Soil Moisture: 28%
Temperature: 33.5°C
Humidity: 51%

Rain: No
Water Level: 78%

AI Recommendation:
Irrigation Required

Pump:
ON

Duration:
5 minutes

When irrigation finishes:

✅ IRRIGATION COMPLETED

Crop: Tomato

Pump Runtime: 5 minutes

System Status:
NORMAL

Data has been recorded in
Google Sheets and ThingSpeak.

18. Telegram Voice Alert

For a voice notification, the conceptual n8n flow is:

AI Decision
    ↓
Generate Alert Text
    ↓
Text-to-Speech Service
    ↓
Audio File
    ↓
Telegram Bot
    ↓
Send Voice/Audio Message

Example spoken message:

"Irrigation alert. Soil moisture is low for the tomato crop. The system recommends five minutes of irrigation."

This makes the project particularly useful for a farmer who may not continuously monitor a dashboard.


19. Google Sheets Database

Create columns such as:

Timestamp Device Crop Stage Soil Temp Humidity Rain Water AI Decision Pump Duration
2026-10-04 10:00 ESP32-01 Tomato Flowering 28 33.5 51 No 78 Irrigate ON 5
2026-10-04 11:00 ESP32-01 Tomato Flowering 46 32.1 55 No 73 No irrigation OFF 0

This gives you a historical dataset for later analysis and model development.


20. ThingSpeak Dashboard

ThingSpeak can be used for numerical visualization.

Possible channels:

Field 1 → Soil Moisture
Field 2 → Temperature
Field 3 → Humidity
Field 4 → Water Level
Field 5 → Rain Status
Field 6 → Pump Status
Field 7 → Irrigation Duration

Dashboard:

┌──────────────────────────────────────────┐
│       SMART IRRIGATION DASHBOARD         │
├──────────────────────────────────────────┤
│ Soil Moisture       ███████░░░  28%       │
│ Temperature                     33.5°C    │
│ Humidity                        51%       │
│ Water Tank                     78%        │
│ Rain                            NO        │
│ Pump                            ON        │
├──────────────────────────────────────────┤
│ Crop: Tomato                              │
│ Stage: Flowering                          │
│ AI: Irrigation Recommended                │
└──────────────────────────────────────────┘

21. Webpage / IoT Dashboard

You can also create a custom webpage:

              SMART FARM AI
        ─────────────────────────

        🌱 Crop: TOMATO
        🌿 Stage: FLOWERING

        Soil Moisture
        ███████░░░░ 28%

        Temperature
        33.5 °C

        Humidity
        51 %

        Tank Level
        78 %

        Rain
        ❌ NO

        Pump
        🟢 ON

        AI Recommendation
        ─────────────────
        Irrigation required

        Duration: 5 minutes

        ┌────────────────────────┐
        │ VIEW HISTORICAL DATA   │
        └────────────────────────┘

22. Complete Communication Architecture

                         INTERNET
                            │
             ┌──────────────┼──────────────┐
             │              │              │
             ▼              ▼              ▼
          ThingSpeak       n8n          Web UI
                            │
                 ┌──────────┼───────────┐
                 │          │           │
                 ▼          ▼           ▼
             AI Agent   Google Sheets Telegram
                 │                      │
                 │                      ▼
                 │                 Voice Alert
                 │
                 ▼
             Decision
                 │
                 ▼
              ESP32
                 │
                 ▼
               Relay
                 │
                 ▼
               Pump

23. Agentic IoT Concept

The project becomes more than simple IoT when the AI Agent can:

OBSERVE
   ↓
Sensor data
   ↓
ANALYZE
   ↓
AI Agent
   ↓
REASON
   ↓
Determine irrigation recommendation
   ↓
ACT
   ↓
ESP32 / Pump
   ↓
OBSERVE AGAIN
   ↓
Verify result
   ↓
LOG
   ↓
Notify farmer

This creates an observe → reason → act → verify loop.


24. Example Agent Conversation

Farmer

Farmer:
How is the field?

AI Agent

AI:
The tomato field currently has 28% soil moisture.
The temperature is 33.5°C and no rain is detected.

The water tank is 78% full.

I recommend irrigation for approximately
5 minutes, subject to the system safety checks.

Farmer

Farmer:
Start irrigation.

Agent

AI:
The request was received.

Safety conditions:
✓ Tank level sufficient
✓ No rain detected
✓ Sensors responding

Irrigation can be started.

System

ESP32
   ↓
Relay ON
   ↓
Pump ON

Agent

AI:
Irrigation completed.

Pump runtime: 5 minutes.

The event has been recorded.

25. Manual Telegram Control

You can optionally implement commands:

/start
/status
/soil
/pump_on
/pump_off
/irrigation
/history
/ai

Example:

Farmer → /status

Bot →
🌱 Field Status

Soil: 31%
Temperature: 32.8°C
Humidity: 54%
Tank: 76%
Rain: No
Pump: OFF

AI:
Irrigation may be required soon.

For safety, manual /pump_on should still pass through maximum-runtime and sensor/tank checks.


26. n8n Workflow Logic

Pseudo-code:

RECEIVE SENSOR DATA

IF soil moisture < crop threshold
    AND rain = false
    AND water level > minimum
    AND sensor values valid

        SEND DATA TO AI

        AI → irrigation_required?

        IF YES
            duration = AI duration

            LIMIT duration to safe maximum

            SEND COMMAND TO ESP32

            LOG EVENT

            SEND TELEGRAM ALERT

        ELSE
            LOG "No irrigation"

ELSE
    Pump OFF
    LOG reason

27. Fault Detection

The system should also identify:

Sensor failure
Wi-Fi failure
Low tank level
Unexpected pump state
Invalid AI response
Unexpected soil readings
Rain detected
ESP32 offline

Example:

🚨 SYSTEM FAULT

Soil moisture sensor returned
an invalid reading.

Pump operation has been disabled.

Please inspect the sensor.

28. Recommended Database/Data Model

A complete record can contain:

{
  "timestamp": "...",
  "device_id": "ESP32_FIELD_01",
  "crop": "Tomato",
  "growth_stage": "Flowering",
  "soil_moisture": 28,
  "temperature": 33.5,
  "humidity": 51,
  "rain": false,
  "water_level": 78,
  "ai_recommendation": "irrigate",
  "irrigation_duration": 5,
  "pump_status": "ON",
  "system_status": "NORMAL"
}

29. Project Development Phases

Phase 1 — Hardware

ESP32
 ↓
Soil Sensor
 ↓
DHT Sensor
 ↓
Rain Sensor
 ↓
Relay
 ↓
Pump

First prove that local sensing and pump control work.

Phase 2 — Internet

ESP32
 ↓
Wi-Fi
 ↓
HTTP
 ↓
n8n

Phase 3 — Cloud

ESP32
 ↓
n8n
 ├── Google Sheets
 └── ThingSpeak

Phase 4 — AI

n8n
 ↓
AI Agent
 ↓
Structured decision

Phase 5 — Telegram

n8n
 ↓
Telegram
 ├── Text
 └── Voice

Phase 6 — Automation

Sensor
 ↓
AI
 ↓
Safety
 ↓
Pump
 ↓
Verification
 ↓
Notification

30. Testing Plan

Test Input Expected Result
Dry soil Low moisture Irrigation recommendation
Wet soil High moisture Pump remains OFF
Rain Rain detected Pump OFF
Low tank Tank below limit Pump OFF + alert
Normal temperature Normal conditions Normal operation
Sensor failure Invalid reading Pump disabled
Wi-Fi failure Network unavailable Local safe state
Telegram Alert event Notification delivered
Google Sheets Sensor event Row created
ThingSpeak Sensor event Fields updated
AI failure Invalid AI output Safe fallback
Manual OFF Telegram command Pump stops

31. Expected Results

The completed system should:

  • Monitor field conditions continuously.

  • Measure soil moisture automatically.

  • Monitor temperature and humidity.

  • Detect rain.

  • Monitor available water.

  • Identify the selected crop and growth stage.

  • Generate an AI-assisted irrigation recommendation.

  • Apply deterministic safety rules.

  • Control the pump automatically.

  • Store historical data.

  • Display cloud graphs.

  • Send Telegram notifications.

  • Generate optional voice alerts.

  • Allow remote monitoring.

  • Provide a foundation for future predictive irrigation models.


32. Advantages

Traditional irrigation

Farmer
  ↓
Manual observation
  ↓
Manual pump
  ↓
Water consumption

Proposed system

Sensors
  ↓
ESP32
  ↓
Cloud
  ↓
AI Agent
  ↓
Safety validation
  ↓
Automatic irrigation
  ↓
Cloud logging
  ↓
Telegram alert

Advantages include:

  • Reduced unnecessary irrigation

  • Remote monitoring

  • Automated operation

  • Historical data collection

  • Crop-aware recommendations

  • Early fault notification

  • Voice-based alerts

  • Expandability to multiple fields


33. Limitations

For an academically honest project report, include these:

  1. AI recommendations depend on the quality of sensor data.

  2. Soil-moisture sensors require calibration.

  3. A generic AI model is not automatically an agronomically validated irrigation model.

  4. Internet connectivity may fail.

  5. Crop-water requirements vary by soil, climate and growth stage.

  6. The prototype should be validated against real agricultural measurements before being used for production irrigation.

  7. Pump control requires appropriate electrical and mechanical safety measures.


34. Future Enhancements

The project can later be upgraded with:

Weather API
     ↓
Rain Forecast
     ↓
AI Agent

and:

Historical Data
      ↓
Machine Learning Model
      ↓
Crop Water Requirement
      ↓
Prediction

Other upgrades:

  • Multiple ESP32 field nodes

  • Solar power

  • LoRa/LoRaWAN

  • Flow-rate monitoring

  • Fertilizer automation

  • Disease detection using camera

  • Leaf-image analysis

  • Weather prediction

  • Digital twin

  • Mobile application

  • Multi-crop support

  • Reinforcement-learning irrigation optimization


35. Final System Diagram

                         ┌──────────────────┐
                         │       FARM       │
                         │                  │
                         │ Soil Sensor      │
                         │ Temp/Humidity    │
                         │ Rain Sensor      │
                         │ Water Level      │
                         └────────┬─────────┘
                                  │
                                  ▼
                         ┌──────────────────┐
                         │      ESP32       │
                         │                  │
                         │ Sensor Processing│
                         │ Wi-Fi            │
                         │ Pump Interface   │
                         └───────┬──────────┘
                                 │
                         Internet / HTTP
                                 │
                                 ▼
                         ┌──────────────────┐
                         │       n8n        │
                         │ Automation       │
                         └────────┬─────────┘
                                  │
                     ┌────────────┼────────────┐
                     │            │            │
                     ▼            ▼            ▼
              ┌───────────┐ ┌──────────┐ ┌───────────┐
              │ AI Agent  │ │ Google   │ │ ThingSpeak│
              │           │ │ Sheets   │ │ Dashboard │
              └─────┬─────┘ └──────────┘ └───────────┘
                    │
                    ▼
             ┌───────────────┐
             │ Safety Rules  │
             └───────┬───────┘
                     │
             ┌───────┴────────┐
             │                │
             ▼                ▼
        Irrigation          No Water
         Required            Needed
             │                │
             ▼                ▼
          ESP32             ESP32
             │                │
             ▼                ▼
          Relay             OFF
             │
             ▼
           PUMP
             │
             ▼
           FIELD

                    n8n
                     │
                     ▼
                ┌──────────┐
                │ Telegram │
                └────┬─────┘
                     │
              ┌──────┴──────┐
              ▼             ▼
             Text          Voice
             Alert         Alert

36. Recommended Project Title

For a college/project report, I would use:

“AI-Powered Automated Irrigation System Using Crop Prediction and Agentic IoT with ESP32, n8n Automation, Telegram Voice Alerts, Google Sheets and ThingSpeak Cloud Monitoring”

Short title

“AI-Based Smart Irrigation System Using ESP32 and n8n”

Core technologies

ESP32
+
IoT Sensors
+
Wi-Fi
+
n8n
+
AI Agent
+
Telegram
+
Google Sheets
+
ThingSpeak
=
Agentic Smart Irrigation System

If you want this as a complete project report, the next logical version is to turn this into a chapter-by-chapter document containing Abstract, Introduction, Literature Survey, Problem Statement, Objectives, Proposed Methodology, complete circuit/schematic, component specifications, ESP32 source code, n8n workflow configuration, AI prompt, Telegram bot setup, Google Sheets setup, ThingSpeak setup, database format, flowcharts, UML/use-case diagram, sequence diagram, testing tables, results, advantages, limitations, future scope, conclusion, and viva questions/answers.

 

Project Summary

AI-Powered Automated Irrigation System Using Crop Prediction is a smart agriculture project that combines ESP32, IoT sensors, AI Agent, n8n automation, Telegram alerts, Google Sheets, and ThingSpeak to automate irrigation.

Core workflow

Soil/Weather Sensors
        ↓
      ESP32
        ↓
     Wi-Fi
        ↓
      n8n
        ↓
    AI Agent
        ↓
Safety Validation
        ↓
   Pump ON/OFF
        ↓
Google Sheets + ThingSpeak
        ↓
Telegram Text/Voice Alert

Main functions

  • Measures soil moisture, temperature, humidity, rain and water level.

  • Uses crop type and growth stage to make irrigation recommendations.

  • ESP32 communicates with the n8n automation server.

  • n8n sends sensor information to an AI Agent.

  • AI recommends whether irrigation is required and suggests a duration.

  • Deterministic safety rules verify the AI recommendation before the pump operates.

  • Relay controls the irrigation pump.

  • Data is stored in Google Sheets for historical analysis.

  • ThingSpeak provides cloud-based graphs and monitoring.

  • Telegram sends real-time text and optional voice alerts.

  • The system can support remote status checking and manual commands.

  • Historical data can later be used to develop a dedicated machine-learning crop/irrigation prediction model.

Key architecture

┌──────────────┐
│ Farm Sensors │
└──────┬───────┘
       ↓
┌──────────────┐
│    ESP32     │
└──────┬───────┘
       ↓
┌──────────────┐
│     n8n      │
└──────┬───────┘
       ↓
┌──────────────┐
│   AI Agent   │
└──────┬───────┘
       ↓
┌──────────────┐
│ Safety Logic │
└──────┬───────┘
       ↓
┌──────────────┐
│ Pump / Relay │
└──────────────┘

       n8n
        ├── Google Sheets
        ├── ThingSpeak
        └── Telegram
              ├── Text
              └── Voice

Main components

Hardware: ESP32, capacitive soil-moisture sensor, DHT22/DHT11, rain sensor, water-level sensor, relay, water pump and suitable power supply.

Software: Arduino IDE, ESP32 firmware, n8n, AI/LLM API, Telegram Bot, Google Sheets and ThingSpeak.

Important design principle

The AI should recommend irrigation, not directly control the pump. Sensor validation, tank-level checks, rain detection, maximum pump runtime and other safety rules should be enforced independently before the ESP32 activates the pump.

Final concept:

Sense → Analyze → Predict → Validate → Irrigate → Log → Notify → Verify

This makes the project an Agentic IoT smart irrigation system rather than simply an automatic moisture-based pump controller.