🐔 AI-Based Smart Poultry Farm Monitoring System
"; "ESP32 + AI Agent + n8n + Telegram + Google Sheets + ThingSpeak + PHP Dashboard
"; "Agentic IoT Poultry Farm Monitoring and Automation Platform
"; "1. Full Project Description
"; ""; "The AI-Based Smart Poultry Farm Monitoring System is an advanced IoT and Artificial Intelligence based solution designed to monitor and automate poultry farm environmental conditions."; "
"; ""; "The system uses an ESP32 microcontroller to collect real-time data from temperature, humidity, gas, light, water-level, feed-level, motion and power sensors."; "
"; ""; "The ESP32 sends the collected sensor data through Wi-Fi to an n8n automation platform. The n8n workflow analyzes the data, communicates with an AI Agent, stores information in Google Sheets, updates ThingSpeak cloud dashboards and sends Telegram text and voice alerts."; "
"; ""; "The system can automatically control poultry farm fans, lighting systems, water pumps and other actuators."; "
"; "2. Main Objectives
"; "- ";
"
- Monitor poultry farm temperature. "; "
- Monitor poultry farm humidity. "; "
- Detect harmful gases and ammonia. "; "
- Monitor lighting conditions. "; "
- Monitor water tank level. "; "
- Monitor poultry feed level. "; "
- Detect poultry movement. "; "
- Monitor electrical power consumption. "; "
- Automatically control ventilation fans. "; "
- Automatically control poultry lighting. "; "
- Automatically control water pumps. "; "
- Use AI for farm condition analysis. "; "
- Predict future power consumption. "; "
- Send Telegram text notifications. "; "
- Send Telegram voice notifications. "; "
- Store data in Google Sheets. "; "
- Display data using ThingSpeak. "; "
- Provide a PHP and MySQL web dashboard. "; "
3. System Architecture
"; "";
"
POULTRY FARM
|
v
+----------------------+
| IoT SENSORS |
|----------------------|
| Temperature |
| Humidity |
| Gas / Ammonia |
| Light |
| Water Level |
| Feed Level |
| Motion |
| Power Consumption |
+----------+-----------+
|
v
+----------------------+
| ESP32 |
|----------------------|
| Sensor Collection |
| Local Automation |
| Relay Control |
| Wi-Fi Communication |
+----------+-----------+
|
v
INTERNET
|
v
+----------------------+
| n8n |
|----------------------|
| Webhook |
| Data Processing |
| AI Agent |
| Alert Automation |
| Google Sheets |
| Telegram |
| ThingSpeak |
+----------+-----------+
|
+--------------+---------------+
| |
v v
+--------------+ +-------------+
| AI AGENT | | TELEGRAM |
| Farm Analysis| | Text Alert |
| Prediction | | Voice Alert |
+--------------+ +-------------+
|
v
+----------------------+
| Cloud Dashboard |
| Google Sheets |
| ThingSpeak |
| PHP / MySQL Website |
+----------------------+
";
"";
"4. Components List
"; "| Component | "; "Purpose | "; "
|---|---|
| ESP32 DevKit | "; "Main IoT controller | "; "
| DHT22 | "; "Temperature and humidity monitoring | "; "
| MQ-135 / MQ-137 | "; "Gas and ammonia detection | "; "
| LDR | "; "Light intensity measurement | "; "
| HC-SR04 / JSN-SR04T | "; "Water and feed level measurement | "; "
| PIR Sensor | "; "Motion detection | "; "
| PZEM-004T / ACS712 | "; "Power consumption monitoring | "; "
| Relay Module | "; "Fan, light and pump control | "; "
| Buzzer | "; "Local emergency warning | "; "
| Wi-Fi Router | "; "Internet connectivity | "; "
5. Circuit Schematic Diagram
"; "";
"
+-------------------+
| ESP32 |
| |
DHT22 DATA ------| GPIO 4 |
| |
MQ-135 AO -------| GPIO 34 |
| |
LDR AO ----------| GPIO 35 |
| |
Water TRIG ------| GPIO 5 |
Water ------| GPIO 18 |
| |
Feed TRIG -------| GPIO 19 |
Feed -------| GPIO 21 |
| |
PIR OUT ---------| GPIO 27 |
| |
Fan Relay <------| GPIO 25 |
Light Relay <----| GPIO 26 |
Pump Relay <-----| GPIO 33 |
| |
Buzzer <----------| GPIO 14 |
+---------+---------+
|
| Wi-Fi
v
+-----------+
| INTERNET |
+-----+-----+
|
v
+-----------+
| n8n |
+-----+-----+
|
+---------------+---------------+
| |
v v
+-----------+ +-----------+
| AI AGENT | | TELEGRAM |
+-----------+ +-----------+
";
"";
"6. ESP32 Pin Configuration
"; "| Device | ESP32 Pin |
|---|---|
| DHT22 Data | GPIO 4 |
| Gas Sensor Analog | GPIO 34 |
| LDR | GPIO 35 |
| Water Ultrasonic Trigger | GPIO 5 |
| Water Ultrasonic | GPIO 18 |
| Feed Ultrasonic Trigger | GPIO 19 |
| Feed Ultrasonic | GPIO 21 |
| PIR Sensor | GPIO 27 |
| Fan Relay | GPIO 25 |
| Light Relay | GPIO 26 |
| Pump Relay | GPIO 33 |
| Buzzer | GPIO 14 |
7. Complete ESP32 Source Code
"; "";
htmlspecialchars('
#include
#include
#include
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
#define GAS_PIN 34
#define LDR_PIN 35
#define WATER_TRIG 5
#define WATER_ 18
#define FEED_TRIG 19
#define FEED_ 21
#define PIR_PIN 27
#define FAN_RELAY 25
#define LIGHT_RELAY 26
#define PUMP_RELAY 33
#define BUZZER_PIN 14
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
const char* N8N_WEBHOOK =
"https://YOUR_N8N_DOMAIN/webhook/poultry-data";
DHT dht(DHTPIN, DHTTYPE);
float temperature;
float humidity;
int gasValue;
int lightValue;
long waterDistance;
long feedDistance;
int motionStatus;
bool fanStatus = false;
bool lightStatus = false;
bool pumpStatus = false;
long readUltrasonic(int trigPin, int Pin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration =
pulseIn( Pin, HIGH, 30000);
long distance =
duration * 0.034 / 2;
return distance;
}
void connectWiFi()
{
Serial.print("Connecting to WiFi");
WiFi.begin(
WIFI_SSID,
WIFI_PASSWORD
);
while (
WiFi.status()
!= WL_CONNECTED
)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println(
"WiFi Connected"
);
Serial.println(
WiFi.localIP()
);
}
void readSensors()
{
temperature =
dht.readTemperature();
humidity =
dht.readHumidity();
gasValue =
analogRead(GAS_PIN);
lightValue =
analogRead(LDR_PIN);
waterDistance =
readUltrasonic(
WATER_TRIG,
WATER_
);
feedDistance =
readUltrasonic(
FEED_TRIG,
FEED_
);
motionStatus =
digitalRead(PIR_PIN);
}
void localControl()
{
if (
temperature > 30
||
gasValue > 1800
)
{
digitalWrite(
FAN_RELAY,
LOW
);
fanStatus =
true;
}
else
{
digitalWrite(
FAN_RELAY,
HIGH
);
fanStatus =
false;
}
if (
lightValue < 1000
)
{
digitalWrite(
LIGHT_RELAY,
LOW
);
lightStatus =
true;
}
else
{
digitalWrite(
LIGHT_RELAY,
HIGH
);
lightStatus =
false;
}
if (
waterDistance > 30
)
{
digitalWrite(
PUMP_RELAY,
LOW
);
pumpStatus =
true;
}
else
{
digitalWrite(
PUMP_RELAY,
HIGH
);
pumpStatus =
false;
}
if (
gasValue > 2500
)
{
digitalWrite(
BUZZER_PIN,
HIGH
);
}
else
{
digitalWrite(
BUZZER_PIN,
LOW
);
}
}
void sendDataToN8N()
{
if (
WiFi.status()
!= WL_CONNECTED
)
{
connectWiFi();
}
HTTPClient http;
http.begin(
N8N_WEBHOOK
);
http.addHeader(
"Content-Type",
"application/json"
);
StaticJsonDocument<1024>
doc;
doc["device_id"] =
"POULTRY_ESP32_001";
doc["temperature"] =
temperature;
doc["humidity"] =
humidity;
doc["gas_value"] =
gasValue;
doc["light_value"] =
lightValue;
doc["water_distance"] =
waterDistance;
doc["feed_distance"] =
feedDistance;
doc["motion"] =
motionStatus;
doc["fan"] =
fanStatus;
doc["light"] =
lightStatus;
doc["pump"] =
pumpStatus;
doc["timestamp"] =
millis();
String jsonData;
serializeJson(
doc,
jsonData
);
int responseCode =
http.POST(
jsonData
);
Serial.print(
"n8n Response: "
);
Serial.println(
responseCode
);
http.end();
}
void setup()
{
Serial.begin(
115200
);
pinMode(
WATER_TRIG,
OUTPUT
);
pinMode(
WATER_ ,
INPUT
);
pinMode(
FEED_TRIG,
OUTPUT
);
pinMode(
FEED_ ,
INPUT
);
pinMode(
PIR_PIN,
INPUT
);
pinMode(
FAN_RELAY,
OUTPUT
);
pinMode(
LIGHT_RELAY,
OUTPUT
);
pinMode(
PUMP_RELAY,
OUTPUT
);
pinMode(
BUZZER_PIN,
OUTPUT
);
digitalWrite(
FAN_RELAY,
HIGH
);
digitalWrite(
LIGHT_RELAY,
HIGH
);
digitalWrite(
PUMP_RELAY,
HIGH
);
dht.begin();
connectWiFi();
}
void loop()
{
readSensors();
localControl();
sendDataToN8N();
delay(
60000
);
}
', ENT_QUOTES);
" ";
"8. ESP32 JSON Data Format
"; "";
htmlspecialchars('{
"device_id": "POULTRY_ESP32_001",
"temperature": 32.5,
"humidity": 75.4,
"gas_value": 2100,
"light_value": 850,
"water_distance": 42,
"feed_distance": 15,
"motion": 1,
"fan": true,
"light": true,
"pump": true,
"timestamp": 123456
}', ENT_QUOTES);
"";
"9. Complete System Flowchart
"; ""; " START | v ESP32 POWER ON | v CONNECT TO Wi-Fi | +---- FAILED ----> RETRY | v READ ALL SENSORS | +---- Temperature +---- Humidity +---- Gas +---- Light +---- Water +---- Feed +---- Motion +---- Power | v LOCAL CONTROL | +---- Temperature HIGH --> FAN ON | +---- Gas HIGH ---------> FAN ON | +---- Water LOW --------> PUMP ON | +---- Light LOW --------> LIGHT ON | v SEND JSON TO n8n | v n8n WEBHOOK | v AI AGENT ANALYSIS | +---- NORMAL | +---- WARNING | +---- CRITICAL | v SAVE DATA | +---- Google Sheets +---- ThingSpeak +---- MySQL | v SEND ALERT | +---- Telegram Text | +---- Telegram Voice | v REPEAT "; ""; "
10. n8n Automation Workflow
"; "";
"
ESP32
|
v
WEBHOOK
|
v
DATA VALIDATION
|
v
AI AGENT
|
v
RISK ANALYSIS
|
+---- NORMAL ------> Google Sheets
|
+---- WARNING -----> Telegram Message
|
+---- CRITICAL ----> Telegram Text
|
v
Voice Alert
|
v
Google Sheets
|
v
ThingSpeak
";
"";
"11. n8n Workflow JSON
"; "";
htmlspecialchars('{
"name": "AI Poultry Farm Monitoring",
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "poultry-data",
"responseMode": "onReceived"
},
"name": "ESP32 Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2
},
{
"parameters": {
"jsCode":
"const data = $json.body || $json;\\n\\nlet risk = \\'NORMAL\\';\\nlet message = \\'Poultry farm conditions are normal.\\';\\n\\nif (data.temperature > 35) {\\n risk = \\'CRITICAL\\';\\n message = \\'Critical temperature detected.\\';\\n}\\n\\nif (data.gas_value > 2500) {\\n risk = \\'CRITICAL\\';\\n message = \\'Critical harmful gas level detected.\\';\\n}\\n\\nif (data.water_distance > 40) {\\n risk = \\'WARNING\\';\\n message = \\'Water level is low.\\';\\n}\\n\\nreturn [{ json: { ...data, risk_level: risk, ai_message: message } }];"
},
"name": "Farm Risk Analysis",
"type": "n8n-nodes-base.code"
},
{
"parameters": {
"chatId": "YOUR_TELEGRAM_CHAT_ID",
"text": "POULTRY FARM ALERT"
},
"name": "Telegram Alert",
"type": "n8n-nodes-base.telegram"
}
]
}', ENT_QUOTES);
"";
"12. Telegram Bot Setup
"; "- ";
"
- Open Telegram. "; "
- Search for BotFather. "; "
- Send /newbot. "; "
- Enter the poultry farm bot name. "; "
- Copy the generated BOT TOKEN. "; "
- Create Telegram credentials in n8n. "; "
- Send a message to your bot. "; "
- Find your Telegram Chat ID. "; "
- Configure the Telegram node. "; "
Example Telegram Alert
"; ""; "🚨 POULTRY FARM CRITICAL ALERT 🚨 Temperature: 38 °C Humidity: 82 % Gas Level: HIGH Fan: ACTIVATED Please inspect the poultry farm immediately."; ""; "
13. Telegram Voice Notification Workflow
"; "";
"
CRITICAL SENSOR DATA
|
v
n8n
|
v
AI AGENT
|
v
GENERATE ALERT TEXT
|
v
TEXT-TO-SPEECH
|
v
AUDIO FILE
|
v
TELEGRAM VOICE MESSAGE
|
v
FARMER MOBILE PHONE
";
"";
"14. Google Sheets Integration
"; "Create a Google Sheet with the following columns:
"; ""; " Timestamp Device ID Temperature Humidity Gas Level Light Level Water Level Feed Level Motion Fan Status Light Status Pump Status Power Consumption Risk Level AI Recommendation "; ""; "
15. ThingSpeak Dashboard Configuration
"; "| Field | Data |
|---|---|
| Field 1 | Temperature |
| Field 2 | Humidity |
| Field 3 | Gas Level |
| Field 4 | Light Intensity |
| Field 5 | Water Level |
| Field 6 | Feed Level |
| Field 7 | Motion |
| Field 8 | Power Consumption |
16. AI Farm Risk Classification
"; "";
"
IF temperature > 35
RISK = CRITICAL
IF gas > 2500
RISK = CRITICAL
IF humidity > 85
RISK = WARNING
IF water_level < 25
RISK = WARNING
IF feed_level < 20
RISK = WARNING
IF motion = 0 FOR LONG TIME
RISK = WARNING
";
"";
"| Risk Score | Condition |
|---|---|
| 0 - 2 | NORMAL |
| 3 - 5 | WARNING |
| 6 - 8 | HIGH |
| 9+ | CRITICAL |
17. AI Power Consumption Prediction
"; ""; "The system records power consumption together with temperature, fan runtime, pump runtime and lighting operation."; "
"; "";
"
Temperature Increases
|
v
Fan Runtime Increases
|
v
Power Consumption Increases
|
v
AI Predicts Future Power Usage
";
"";
"Example Prediction
"; ""; "Current temperature is 34 degrees Celsius."; "
"; ""; "Fan runtime is 70 percent."; "
"; ""; "Current power consumption is 550 watts."; "
"; ""; "AI Prediction: Expected power consumption may increase to approximately 650 watts if temperature continues to rise."; "
"; "18. PHP Web Dashboard
"; ""; " +--------------------------------------+ | AI POULTRY FARM DASHBOARD | +--------------------------------------+ | Temperature 32.5 °C | | Humidity 72 % | | Gas Level NORMAL | | Water Level 65 % | | Feed Level 80 % | | Power 480 W | +--------------------------------------+ | FAN ON | | LIGHT OFF | | WATER PUMP ON | +--------------------------------------+ | AI STATUS: WARNING | +--------------------------------------+ "; ""; "
19. PHP Project Folder Structure
"; ""; " poultry-farm/ | |-- index.php | |-- dashboard.php | |-- config.php | |-- database.php | |-- api/ | |-- sensor_data.php | |-- latest_data.php | |-- device_control.php | |-- ai/ | |-- ai_analysis.php | |-- power_prediction.php | |-- assets/ | |-- css/ | |-- js/ | |-- database/ | |-- poultry.sql | |-- n8n/ | |-- workflow.json | |-- documentation/ | |-- poultry_farm_documentation.php "; ""; "
20. MySQL Database
"; "";
htmlspecialchars("
CREATE DATABASE poultry_farm;
USE poultry_farm;
CREATE TABLE sensor_data (
id INT AUTO_INCREMENT PRIMARY KEY,
device_id VARCHAR(100),
temperature FLOAT,
humidity FLOAT,
gas_value INT,
light_value INT,
water_distance FLOAT,
feed_distance FLOAT,
motion INT,
fan_status BOOLEAN,
light_status BOOLEAN,
pump_status BOOLEAN,
power_consumption FLOAT,
risk_level VARCHAR(50),
ai_message TEXT,
created_at TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
);
", ENT_QUOTES);
"";
"21. PHP Sensor Data API
"; "";
htmlspecialchars("
35
||
$gas > 2500
)
{
$risk =
'CRITICAL';
}
elseif (
$temperature > 30
||
$humidity > 80
)
{
$risk =
'WARNING';
}
else
{
$risk =
'NORMAL';
}
json_encode(
[
'success' => true,
'risk_level' =>
$risk,
'temperature' =>
$temperature,
'humidity' =>
$humidity,
'gas' =>
$gas
]
);
?>
", ENT_QUOTES);
"";
"22. Complete System Operation
"; "";
"
1. ESP32 starts.
|
v
2. ESP32 connects to Wi-Fi.
|
v
3. Sensors collect farm data.
|
v
4. ESP32 performs local control.
|
v
5. Sensor data becomes JSON.
|
v
6. JSON is sent to n8n.
|
v
7. n8n receives webhook data.
|
v
8. AI Agent analyzes farm conditions.
|
v
9. Risk level is calculated.
|
v
10. Data is saved to Google Sheets.
|
v
11. Data is sent to ThingSpeak.
|
v
12. PHP dashboard is updated.
|
v
13. Telegram alert is generated.
|
v
14. Critical condition generates voice alert.
|
v
15. System continues monitoring.
";
"";
"23. Testing Procedure
"; "Temperature Test
"; "Increase the temperature reading and verify that the fan automatically turns ON.
"; "Gas Test
"; "Test the gas sensor using a safe test method and verify that the fan, buzzer and Telegram alert operate correctly.
"; "Water Test
"; "Reduce the water level and verify that the water pump activates.
"; "Internet Failure Test
"; "Disconnect Wi-Fi and verify that local ESP32 automation continues operating.
"; "24. Future Enhancements
"; "- ";
"
- AI-based poultry disease detection. "; "
- ESP32-CAM bird behavior monitoring. "; "
- Computer vision-based bird counting. "; "
- Automatic feed dispensing. "; "
- Solar-powered poultry farm system. "; "
- Predictive maintenance for fans and pumps. "; "
- Mobile Android application. "; "
- Advanced machine learning power prediction. "; "
- Cloud-based multi-farm management. "; "
- AI-based mortality detection. "; "
25. Final Project Summary
"; ""; "The AI-Based Smart Poultry Farm Monitoring System combines ESP32 IoT hardware, environmental sensors, AI analysis, n8n workflow automation, Telegram notifications, voice alerts, Google Sheets, ThingSpeak cloud monitoring and a PHP/MySQL web dashboard."; "
"; ""; "The ESP32 performs real-time monitoring and local automation. n8n acts as the central automation engine. The AI Agent analyzes farm conditions and generates intelligent recommendations. Google Sheets stores historical information. ThingSpeak provides cloud visualization. Telegram sends text and voice alerts. The PHP and MySQL dashboard provides centralized monitoring."; "
"; ""; " SENSORS | v ESP32 | v Wi-Fi | v n8n | v AI AGENT | v DECISION | +---- FAN | +---- LIGHT | +---- PUMP | +---- BUZZER | +---- TELEGRAM | +---- VOICE ALERT | +---- GOOGLE SHEETS | +---- THINGSPEAK | +---- PHP DASHBOARD "; ""; "

No comments:
Post a Comment