Thursday, 6 August 2026

AI Based Autonomous Farming Robot with Crop Health Monitoring

AI-Based Autonomous Farming Robot with Crop Health Monitoring

1. Full Project Description

The AI-Based Autonomous Farming Robot is an end-to-end Agentic IoT solution designed to automate crop health monitoring, environmental sensing, and field navigation.

Architecture Overview

  1. Edge Sensing & Actuation (ESP32): Reads soil moisture, ambient temperature/humidity (DHT22), optical leaf health via an LDR light/multispectral reflectance array, and tracks battery power consumption (INA219). It controls dual DC motors (L298N driver) for autonomous row navigation.

  2. Telemetry Dispatch: The ESP32 transmits sensor payload telemetry every cycle directly to ThingSpeak for continuous time-series graphing and sends a parallel HTTP POST webhook payload to an n8n Automation Engine.

  3. Agentic Orchestration & AI Analysis (n8n Engine): n8n parses incoming JSON payloads and routes sensor metrics to an AI Agent (powered by OpenAI / Groq / Gemini). The AI Agent evaluates soil stress levels, predicts power drain, and assesses plant health anomalies.

  4. Data Persistence & Analytics: Telemetry and AI health assessments are automatically logged to Google Sheets for historical auditing and yield forecasting.

  5. Real-time Voice Alerts (Telegram Bot): If crop stress, low soil moisture, or high power drain is detected, n8n passes the AI-generated advisory to a Text-to-Speech engine (OpenAI TTS / ElevenLabs) and delivers an audio voice note message directly to the farmer via Telegram.

2. Components List

Component Quantity Purpose / Specification
ESP32 NodeMCU Development Board 1 Master Edge Microcontroller with Wi-Fi & Bluetooth
L298N Dual H-Bridge Motor Driver 1 Controls 12V Gear Motors for robot locomotion
12V DC Geared Motors + Wheels 2 Differential drive robot chassis propulsion
DHT22 Temperature & Humidity Sensor 1 Ambient microclimate environmental monitoring
Capacitive Soil Moisture Sensor v1.2 1 Corrosion-resistant soil moisture measurement
LDR Photoresistor Module / Optical Sensor 1 Foliage reflectance / ambient light health checking
INA219 I2C Current/Voltage Sensor Module 1 Real-time voltage, current, and power consumption monitoring
HC-SR04 Ultrasonic Sensor 1 Autonomous obstacle detection and navigation
12.6V 3S Li-ion Battery Pack (18650) 1 Main power source for motors and electronics
LM2596 Buck Converter (12V to 5V) 1 Regulates 12V down to clean 5V for ESP32 and sensors
Breadboard / Custom PCB & Jumper Wires 1 Lot Interconnections

3. Circuit Schematic Diagram

Connection Pinout Table

Sensor / Module Module Pin ESP32 GPIO Pin Power Rail
L298N Motor Driver IN1 / IN2 GPIO 16 / GPIO 17 12V External (Motor Power)

IN3 / IN4 GPIO 18 / GPIO 19 12V External

ENA / ENB GPIO 22 / GPIO 23
DHT22 Sensor DATA GPIO 4 (10k Pull-up to 3.3V) 3.3V / GND
Soil Moisture Sensor AOUT GPIO 34 (ADC1_CH6) 3.3V / GND
LDR Sensor Module AOUT GPIO 35 (ADC1_CH7) 3.3V / GND
INA219 Power Sensor SDA / SCL GPIO 21 (SDA) / GPIO 22 (SCL) 3.3V / GND
HC-SR04 Ultrasonic TRIG / ECHO GPIO 12 / GPIO 13 5V / GND
LM2596 Buck Converter OUT+ / OUT- ESP32 VIN / GND 12V Battery Input

Schematic Wire Block Diagram

Plaintext

               +--------------------------------------------------------+
               |                   12.6V Li-ion Battery                 |
               +---------------------------+----------------------------+
                                           |
                                 +---------+---------+
                                 |                   |
                         +-------v-------+   +-------v-------+
                         | LM2596 Buck   |   | L298N Driver  |
                         | Converter 5V  |   | (Motor Power) |
                         +-------+-------+   +-------+-------+
                                 |                   |
                        +--------v-------------------+--------+
                        |           ESP32 DevBoard            |
                        +----+--------+-------+-------+-------+
                             |        |       |       |
                 +-----------+   +----+---+  ++-----+ ++------+
                 |               |        |   |     |  |      |
           +-----v-----+      +--v---+ +--v---+ +---v--v-+ +--v-----+
           | Capacitive|      |DHT22 | | LDR  | | INA219 | | HC-SR04|
           | Soil Sensor|     |Temp/ | | Leaf | | I2C    | | Ultra- |
           | (GPIO 34) |      |Hum   | | Light| | Sensor | | sonic  |
           +-----------+      +------+ +------+ +--------+ +--------+

4. System Flowchart

Plaintext

[START: Robot Power On]
       |
       v
[Initialize Wi-Fi, I2C, Sensors, & Motors]
       |
       v
[Read Sensors: Temp, Humidity, Moisture, Reflectance, Power]
       |
       v
[Obstacle Ahead < 20cm?] --YES--> [Stop Motors & Pivot Autonomous Turn]
       |                                     |
       NO                                    v
       |                          [Resume Forward Path]
       v
[Package Sensor Payload JSON]
       |
       v
[HTTP POST -> ThingSpeak Dashboard] (Every 15s)
       |
       v
[HTTP POST -> n8n Webhook Endpoint]
       |
       v
[n8n Workflow Activated]
       |
       +---> [Write Raw Telemetry -> Google Sheets]
       |
       +---> [Pass Telemetry -> Agentic AI Node]
                   |
                   v
         [Evaluate Crop Stress & Run Power Prediction Model]
                   |
         Anomalies / Threshold Crossed?
                   |
              +----+----+
             YES        NO
              |         |
              v         v
     [Generate Audio  [Log Healthy Status]
      via TTS API]
              |
              v
     [Send Telegram Voice
      Alert to Farmer]

5. ESP32 Source Code

Compile using Arduino IDE with the following required libraries:

WiFi.h, HTTPClient.h, DHT.h, Wire.h, Adafruit_INA219.h, ThingSpeak.h.

C++

#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <ThingSpeak.h>

// WiFi Configuration
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";

// ThingSpeak & n8n Configuration
WiFiClient client;
unsigned long thingSpeakChannelID = 1234567; // Replace with Channel ID
const char* thingSpeakAPIKey = "YOUR_THINGSPEAK_WRITE_KEY";
const char* n8nWebhookURL = "https://your-n8n-instance.com/webhook/crop-health";

// Pin Definitions
#define DHTPIN 4
#define DHTTYPE DHT22
#define SOIL_PIN 34
#define LDR_PIN 35
#define TRIG_PIN 12
#define ECHO_PIN 13

// L298N Motor Pins
#define IN1 16
#define IN2 17
#define IN3 18
#define IN4 19

DHT dht(DHTPIN, DHTTYPE);
Adafruit_INA219 ina219;

// Timing Constants
unsigned long lastPublishTime = 0;
const unsigned long publishInterval = 15000; // 15 seconds

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

  // Pin Modes
  pinMode(SOIL_PIN, INPUT);
  pinMode(LDR_PIN, INPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);

  // Initialize Sensors
  dht.begin();
  if (!ina219.begin()) {
    Serial.println("Warning: INA219 current sensor initialized with errors!");
  }

  // Connect WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi Connected! IP: " + WiFi.localIP().toString());

  ThingSpeak.begin(client);
}

long getDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout
  if (duration == 0) return 999;
  return duration * 0.034 / 2;
}

void moveForward() {
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}

void turnRight() {
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);  digitalWrite(IN4, HIGH);
}

void stopRobot() {
  digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
}

void loop() {
  // Autonomous Navigation Loop
  long distance = getDistance();
  if (distance < 25) { // Obstacle closer than 25cm
    stopRobot();
    delay(300);
    turnRight();
    delay(600);
  } else {
    moveForward();
  }

  // Telemetry Upload Loop
  if (millis() - lastPublishTime >= publishInterval) {
    lastPublishTime = millis();

    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    int rawSoil = analogRead(SOIL_PIN);
    // Calibration: Map raw ADC (12-bit) to moisture percentage
    float moisture = map(rawSoil, 3200, 1400, 0, 100); 
    moisture = constrain(moisture, 0, 100);

    int rawLDR = analogRead(LDR_PIN);
    float leafReflectance = map(rawLDR, 0, 4095, 0, 100);

    float busVoltage = ina219.getBusVoltage_V();
    float current_mA = ina219.getCurrent_mA();
    float power_mW = ina219.getPower_mW();

    if (isnan(temp) || isnan(hum)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }

    // 1. Upload to ThingSpeak Dashboard
    ThingSpeak.setField(1, temp);
    ThingSpeak.setField(2, hum);
    ThingSpeak.setField(3, moisture);
    ThingSpeak.setField(4, leafReflectance);
    ThingSpeak.setField(5, busVoltage);
    ThingSpeak.setField(6, power_mW);
    
    int tsCode = ThingSpeak.writeFields(thingSpeakChannelID, thingSpeakAPIKey);
    Serial.println("ThingSpeak Update Status: " + String(tsCode));

    // 2. Transmit JSON Webhook to n8n Automation Engine
    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;
      http.begin(n8nWebhookURL);
      http.addHeader("Content-Type", "application/json");

      String jsonPayload = "{";
      jsonPayload += "\"temperature\":" + String(temp, 1) + ",";
      jsonPayload += "\"humidity\":" + String(hum, 1) + ",";
      jsonPayload += "\"soil_moisture\":" + String(moisture, 1) + ",";
      jsonPayload += "\"leaf_health_index\":" + String(leafReflectance, 1) + ",";
      jsonPayload += "\"battery_voltage\":" + String(busVoltage, 2) + ",";
      jsonPayload += "\"power_consumption_mW\":" + String(power_mW, 1);
      jsonPayload += "}";

      int httpResponseCode = http.POST(jsonPayload);
      Serial.println("n8n Webhook Response Code: " + String(httpResponseCode));
      http.end();
    }
  }
}

6. n8n Workflow JSON

Import this JSON directly into your n8n editor (Settings -> Import from File / URL):

JSON

{
  "name": "AI Agentic IoT Crop Health & Voice Notification Pipeline",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "crop-health",
        "options": {}
      },
      "name": "ESP32 Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [100, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "sheetId": "YOUR_GOOGLE_SHEET_ID",
        "range": "A:G",
        "options": {}
      },
      "name": "Google Sheets Logger",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [320, 180]
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an expert Autonomous Agriculture AI Agent. Analyze the telemetry data provided: Temperature, Humidity, Soil Moisture, Leaf Health Index, Battery Voltage, and Power Consumption. Output a JSON payload containing: 1) 'alert_required' (boolean), 2) 'speech_text' (A concise script under 30 words for farmer voice alerts in clear simple terms), and 3) 'power_prediction' (estimated battery operating hours remaining)."
            },
            {
              "role": "user",
              "content": "={{ JSON.stringify($json.body) }}"
            }
          ]
        }
      },
      "name": "AI Crop Reasoning Agent",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1,
      "position": [320, 420]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.message.content.alert_required }}",
              "value2": true
            }
          ]
        }
      },
      "name": "Check Alert Requirement",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [540, 420]
    },
    {
      "parameters": {
        "resource": "audio",
        "operation": "generate",
        "text": "={{ $json.message.content.speech_text }}",
        "voice": "alloy"
      },
      "name": "OpenAI Text-To-Speech",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [760, 320]
    },
    {
      "parameters": {
        "operation": "sendAudio",
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "binaryData": true,
        "binaryPropertyName": "data",
        "caption": "🚨 AI Farm Robot Alert!"
      },
      "name": "Send Telegram Voice Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [980, 320]
    }
  ],
  "connections": {
    "ESP32 Webhook Trigger": {
      "main": [
        [
          { "node": "Google Sheets Logger", "type": "main", "index": 0 },
          { "node": "AI Crop Reasoning Agent", "type": "main", "index": 0 }
        ]
      ]
    },
    "AI Crop Reasoning Agent": {
      "main": [
        [
          { "node": "Check Alert Requirement", "type": "main", "index": 0 }
        ]
      ]
    },
    "Check Alert Requirement": {
      "main": [
        [
          { "node": "OpenAI Text-To-Speech", "type": "main", "index": 0 }
        ]
      ]
    },
    "OpenAI Text-To-Speech": {
      "main": [
        [
          { "node": "Send Telegram Voice Alert", "type": "main", "index": 0 }
        ]
      ]
    }
  }
}

7. Telegram Bot Setup

  1. Open Telegram and search for @BotFather.

  2. Send /newbot, provide a name (e.g., SmartAgriRobotBot), and choose a username.

  3. Save the returned API Token (e.g., 7123456789:AAF...).

  4. Start a chat with your newly created bot and send any message (e.g., "Hello").

  5. Retrieve your numeric Chat ID by navigating to:

    [https://api.telegram.org/bot](https://api.telegram.org/bot)<YOUR_BOT_TOKEN>/getUpdates

  6. Enter the Token and Chat ID into the Telegram credentials section within n8n.

8. Google Sheets Integration

  1. Create a new Google Spreadsheet named Crop_Health_Telemetry.

  2. Add column headers in Row 1:

    • Column A: Timestamp

    • Column B: Temperature (°C)

    • Column C: Humidity (%)

    • Column D: Soil Moisture (%)

    • Column E: Leaf Health Index

    • Column F: Battery Voltage (V)

    • Column G: Power (mW)

  3. Connect your Google Account in n8n under Credentials -> Google Sheets OAuth2 API.

  4. Link the Spreadsheet ID in the Google Sheets Logger node.

9. ThingSpeak Cloud Dashboard Setup

  1. Sign up at ThingSpeak.com.  

  2. Click Channels -> My Channels -> New Channel.  

  3. Name: AI Farming Robot Dashboard.

  4. Configure Channel Fields:  

    • Field 1: Temperature (°C)  

    • Field 2: Humidity (%)  

    • Field 3: Soil Moisture (%)

    • Field 4: Leaf Health Index

    • Field 5: Battery Voltage (V)

    • Field 6: Power Consumption (mW)

  5. Save Channel and copy your Channel ID and Write API Key into the ESP32 code.  

  6. Customize the public/private dashboard with Gauge Widgets for Soil Moisture and Line Graphs for power consumption over time.

10. AI Power Consumption Prediction Logic

The power depletion rate is dynamically predicted inside the AI Agent using a regression formula based on instantaneous current draw and battery state:

$$P_{\text{instant}} = V_{\text{bus}} \times I_{\text{load}}$$
$$\text{Remaining Runtime (Hours)} = \frac{(V_{\text{bus}} - V_{\text{cutoff}}) \times Q_{\text{capacity}}}{P_{\text{instant}}} \times \eta$$

Where:

  • $V_{\text{bus}}$ = Current Battery Voltage read by INA219.

  • $V_{\text{cutoff}}$ = 9.9V (3S Li-ion empty threshold).

  • $Q_{\text{capacity}}$ = Nominal battery capacity (e.g., 2.5 Ah).

  • $P_{\text{instant}}$ = Measured power consumption in Watts.

  • $\eta$ = Discharge efficiency coefficient ($\approx 0.85$).

The AI Agent executes this reasoning calculation dynamically to predict runtime and flags an alert when estimated runtime drops under 30 minutes.

11. Voice Notification Automation

  1. Trigger: An anomaly is detected by n8n (e.g., soil_moisture < 25% or power_mW > 4500mW).

  2. Text Generation: The OpenAI Agent produces a contextual, high-priority summary script.

  3. Synthesis: The script is piped to the OpenAI Audio API (tts-1 model, alloy voice) or ElevenLabs, creating an .ogg / .mp3 audio payload.  

  4. Delivery: The n8n Telegram node transmits the binary audio file via sendAudio / sendVoice method, causing the farmer's mobile device to receive an audible push notification with voice instructions.

12. Future Enhancements & Deployment Guide

Deployment Checklist

  • Enclosure: Standard 3D-printed IP65 weatherproof housing to shield the ESP32 and motor driver from rain and dust.

  • Power: Mount a 5V/12V dual solar panel array directly on top of the robot chassis for continuous trickle charging.

Technical Next Steps

  • Edge Computer Vision: Upgrade the LDR sensor to an ESP32-CAM or Raspberry Pi Orin Nano running an ONNX YOLOV8 model for real-time leaf lesion and weed detection.

  • RTK GPS Autonomous Pathing: Integrate a Neo-6M GPS module with Real-Time Kinematic positioning for field navigation accurate to within centimeters.

Demonstrative Video Reference

To observe a implementation of a voice-triggered Telegram AI agent powered by n8n and Whisper/OpenAI speech nodes, check the demonstration below:

Build a Telegram Voice Trigger AI Agent in n8n  

This video demonstrates how incoming audio and AI voice responses are handled seamlessly in an n8n pipeline.  


No comments:

Post a Comment