A Smart Irrigation System based on ESP8266, designed to monitor environmental conditions like soil moisture, temperature, humidity, rain detection, and light intensity (via LDR), and control a water pump automatically or manually via WebSocket communication.
This project ensures efficient water usage, real-time monitoring, and automation to support smart farming and urban gardens.
- WiFi-Enabled Monitoring: Connects to WiFi for live sensor data streaming.
- WebSocket Communication: Fast, real-time data transmission to a server/frontend.
- NTP Time Synchronization: Timestamps all sensor readings using India's Standard Time (IST, UTC+5:30).
- Auto-Mode Pump Control: Water pump is turned ON/OFF based on real-time soil, rain, temperature, and humidity conditions.
- Light Detection (LDR): Measures surrounding light intensity.
- Rain Sensor: Detects rainfall.
- DHT11 Sensor: Monitors environmental temperature and humidity.
- Error Handling: Ensures data is sent only when values change or when first connecting.
- Low Power: Smart updates avoid unnecessary transmissions.
- Reconnect Automatically: Auto-reconnect to WebSocket server if disconnected.
- Manual Override: Auto-mode can be toggled via server commands.
| Phase | Description |
|---|---|
| Phase 1 | Setup ESP8266 firmware to send data over WebSocket |
| Phase 2 | Setup simple Node.js server to receive and display data |
| Phase 3 | Add Smart Decision Logic (control LED or Pump) |
| Phase 4 | (Optional) Build React Dashboard to visualize data |
| Phase 5 | (Optional) Save data to database (MongoDB) |
| Phase 6 | (Optional) Notifications and AI-based improvements |
Smart-Irrigation-System/
βββ client/ # Frontend (Optional: Web Dashboard for Monitoring)
β βββ public/ # Static assets
β βββ src/
β βββ components/
β β βββ sections/ # Reusable UI components
β β βββ dashboard-content.jsx
β β βββ dashboard-layout.jsx
β β βββ gauge.jsx
β β βββ history-chart.jsx
β β βββ theme-toggle.jsx
β βββ pages/ # Application pages (Dashboard, History, etc.)
β βββ services/ # API calls and service logic
β βββ App.jsx # Main application component
β βββ main.jsx # Application entry point
β
βββ esp8266-firmware/ # ESP8266 Arduino firmware (sensors + irrigation logic)
β βββ SmartIrrigation.ino # Main microcontroller code
β βββ README.md # Firmware-specific documentation
β
βββ server/ # Backend Server (Node.js + Express + WebSocket)
β βββ controllers/ # Functions for handling sensor data logic
β β βββ history-controller.js
β βββ database/ # Database connection files
β β βββ models/ # Database models (schemas for sensor readings, logs, etc.)
β β βββ (models go here)
β β βββ db.js # Database connection script
β βββ logs/ # Server and WebSocket logs
β βββ middlewares/ # Authentication, error handling, validations
β β βββ save-history-data.js
β βββ routes/ # API route definitions
β β βββ history-route.js
β βββ utils/ # Utility helper functions
β β βββ logger.js
β βββ server.js # Server entry point
β
βββ README.md # Project Overview and Setup Instructions
βββ LICENSE # (Optional) Open-source license information
βββ .gitignore # Files and folders to ignore in version control
- ReactJS / Vite frontend (Optional but very cool for live monitoring)
- Components like:
- Live Data Display (Temperature, Soil Moisture, Light)
- Status Indicator (Is LED ON or OFF)
- Responsive Web App to monitor field remotely.
- Your ESP8266 code:
- Connects to WiFi
- Connects to WebSocket server
- Reads sensors: DHT22, Soil Moisture, Rain Sensor, LDR
- Applies logic: Temperature, Light, Moisture, Rain
- Controls: Built-in LED or Relay Module for pump.
- Node.js server
- Handles WebSocket connection with ESP8266
- Displays received data
- (Optional) Save to database for future analysis
- (Optional) Expose REST APIs if needed.
- MongoDB or any database setup (optional but recommended)
- Can save history of sensor readings
- Analyze trends later (ex: which month was driest?).
- ESP8266 connects to WiFi.
- Synchronizes time using NTP Client (India timezone).
- Connects to a WebSocket Server (IP & Port specified).
- Continuously reads sensor data:
- Temperature, Humidity, Soil Moisture, Rain Detection, Light Level.
- AutoMode logic:
- Turns ON the pump when:
- Soil is dry AND (Temperature is high OR Humidity is low)
- AND No Rain detected.
- Turns OFF the pump otherwise.
- Turns ON the pump when:
- Sends sensor data + pump status in real-time to the server.
- If values change, or first-time connection happens, data is sent immediately.
- Soil Dryness: Soil moisture < 60%
- High Temperature: Temperature > 30Β°C
- Low Humidity: Humidity < 40%
- No Rain: Rain sensor not detecting rain
If these conditions match, pump (LED) turns ON, otherwise OFF.
- Flash ESP8266 with Code
Upload the.inosketch to your ESP8266 board via Arduino IDE or PlatformIO. - Libraries Needed:
ESP8266WiFi.hWebSocketsClient.hArduinoJson.hDHT.hWiFiUdp.hNTPClient.h
- Modify WiFi and WebSocket Settings:
const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; const char* websocket_server = "SERVER_IP_ADDRESS"; const uint16_t websocket_port = 3000;
- Connect Sensors Properly: Follow pin mappings shown above carefully.
- Start Your WebSocket Server:
Ensure your server is running and listening to the right port and endpoint
/.
{
"temperature": 29.5,
"humidity": 60,
"soilMoisture": 45,
"lightLevel": 78,
"rainDrop": 1,
"pumpStatus": true,
"autoMode": true,
"timestamp": "2025-04-27T15:45:30+05:30",
"espConnected": true
}| Field | Description |
|---|---|
| temperature | Measured in Β°C |
| humidity | Relative Humidity (%) |
| soilMoisture | Soil Moisture (%) |
| lightLevel | Light intensity (%) from LDR |
| rainDrop | 0 = Rain detected, 1 = No rain |
| pumpStatus | true = Pump ON, false = Pump OFF |
| autoMode | true if auto mode is active |
| timestamp | Time when reading was taken (ISO format) |
| espConnected | true if WebSocket connected |
This project implements a Smart Irrigation System that monitors:
- Temperature
- Humidity
- Light Intensity
- Soil Moisture
- Rain Detection
Based on environmental conditions, it automatically controls irrigation (water pump/LED). It also streams live data to a server using WebSocket technology.
[ ESP8266 + Sensors ] --WebSocket--> [ Node.js Server ] --Optional--> [ Database ]
|
β
[ React Web App (Client) ]
- π‘οΈ Read real-time environmental data.
- π§ Automatically decide when to water the plants.
- π¦οΈ Rain detection to avoid unnecessary watering.
- πΆ WebSocket communication for fast, real-time data transfer.
- π (Optional) Web dashboard for live monitoring.
| Part | Tech |
|---|---|
| Microcontroller | ESP8266 (NodeMCU) |
| Sensors | DHT22, Soil Moisture, LDR, Rain Sensor |
| Server | Node.js + Express + WebSocket |
| Frontend (Optional) | React.js + TailwindCSS + Shadcn UI |
| Database (Optional) | MongoDB |
cd server
npm install
node server.js- Open Arduino IDE
- Install libraries:
- ESP8266WiFi
- WebSocketsClient
- DHT Sensor Library
- Upload
SmartIrrigation.ino
cd client
npm install
npm run dev- ESP8266 NodeMCU board
- DHT22 Temperature and Humidity Sensor
- LDR (Light Dependent Resistor)
- Soil Moisture Sensor (Digital/Analog)
- Rain Detection Sensor
- (Optional) Relay Module + Motor/Pump
- Jumper wires, Breadboard
| Component | Quantity | Description |
|---|---|---|
| ESP8266 (e.g., NodeMCU) | 1 | WiFi-enabled microcontroller |
| DHT11 Sensor | 1 | Temperature and Humidity sensor |
| Soil Moisture Sensor | 1 | For soil moisture measurement |
| Rain Sensor Module | 1 | Detects rain presence |
| LDR (Light Dependent Resistor) | 1 | Measures ambient light |
| Relay Module (optional) | 1 | To control real water pump safely |
| Jumper wires | As needed | For connections |
| Breadboard | 1 | (Optional) prototyping |
| LED | 1 | Simulates the pump action (Built-in LED) |
| ESP8266 Pin | Connected To |
|---|---|
| D6 (GPIO12) | LDR Sensor Output (Digital) |
| D4 (GPIO2) | DHT11 Sensor Output (Digital) |
| A0 | Soil Moisture Sensor (Analog) |
| D5 (GPIO14) | Rain Sensor Output (Digital) |
| LED_BUILTIN | Simulated Water Pump (LED) |
Note:
- D3 (GPIO0) used for LDR is set as Input (digital read).
- A0 reads analog voltage from Soil Moisture sensor.
(Insert images of your hardware setup, screenshots of dashboard)

- Mobile App for notifications.
- AI-based predictive irrigation (analyze historical data).
- Solar-powered version.
Connected! IP Address: 192.168.1.105
Connected to WebSocket server
Frontend connected, sending immediate data...
Data Sent to Server
Data Sent to Server
Failed to read from DHT sensor!
Data Sent to Server| Problem | Solution |
|---|---|
| WebSocket not connecting | Check IP address, port, server status |
| ESP stuck during boot | Avoid pulling GPIO0 LOW at startup (no pressed buttons on D3 pin) |
| DHT11 readings NaN (failed) | Check wiring, DHT11 timing library |
| No soil moisture readings | Check if Soil Moisture sensor is powered correctly |
| Pump not switching ON/OFF | Check your AutoMode conditions manually |
- Add OTA updates (Over the Air programming).
- Real Relay Module for controlling real water pump.
- Mobile App Notification using WebSocket messages.
- Solar powered battery management for outdoor deployment.
Developed by Smitraj Bankar
- B.Tech Computer Engineering | Sanjivani College of Engineering
- Expertise: MERN Stack, IoT Systems, Arduino, Embedded Projects
This project is licensed under MIT License.
You can use ngrok to make your local Node.js server public if you want to control the system over the internet π.
β This folder structure and plan will make your Smart Irrigation System ready for:
- Deployment
- Presentations
- College Projects
- Even Real-world Product Launch!