🌱 Smart Plant Monitoring + Automatic Watering System
Using ESP32 + Soil Moisture Sensor + Relay + Water Pump
ESP32 Wifi Terminal Code + VERY detailed line-by-line explanation.
✅ ESP32 connects to your Home WiFi
✅ Mobile also connected to same WiFi
✅ Open WiFi Terminal App (TCP Client)
✅ Enter ESP32 IP + Port
✅ You get live terminal output
✅ Moisture < 10% → Pump ON
✅ Moisture ≥ 10% → Pump OFF
✅ 1) WiFi Terminal App (Android)
Use any one:
📱 TCP Telnet Terminal
📱 Telnet Client
📱 Serial WiFi Terminal
✅ 2) ESP32 WiFi Router Mode WiFi Terminal Code (COPY-PASTE)
⚡ Connections:
-
Soil Sensor AO → GPIO34
-
Relay IN → GPIO26
🔧 Change your WiFi name + password below.
/************************************************************
SMART PLANT MONITORING + AUTO WATERING SYSTEM (WiFi Router)
Board: ESP32
Sensor: Soil Moisture Sensor (Analog)
Relay: 1 Channel Relay
Pump: DC Water Pump
Condition:
Moisture < 10% -> Pump ON
Moisture >= 10% -> Pump OFF
Output:
Serial Monitor + WiFi Terminal App (TCP)
************************************************************/
#include <WiFi.h>
// ----------- Your Running WiFi Details -----------
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
// ----------- TCP Server Port -----------
WiFiServer server(23); // Port 23 (Telnet)
WiFiClient client;
// ----------- Pin Definitions -----------
#define SOIL_SENSOR_PIN 34
#define RELAY_PIN 26
// ----------- Moisture Calibration Values -----------
int dryValue = 3500;
int wetValue = 1500;
// ----------- Moisture Threshold -----------
int pumpThreshold = 10;
// ----------- Variables -----------
int sensorValue = 0;
int moisturePercent = 0;
String soilStatus = "";
String pumpStatus = "";
// ----------- Function: Read sensor + control pump -----------
void readSensorAndControlPump()
{
// Read ADC from soil sensor
sensorValue = analogRead(SOIL_SENSOR_PIN);
// Convert ADC to moisture %
moisturePercent = map(sensorValue, dryValue, wetValue, 0, 100);
// Clamp moisture % (0-100)
if (moisturePercent > 100) moisturePercent = 100;
if (moisturePercent < 0) moisturePercent = 0;
// Pump control
if (moisturePercent < pumpThreshold)
{
// Soil dry -> Pump ON
digitalWrite(RELAY_PIN, LOW); // Active LOW relay
soilStatus = "DRY";
pumpStatus = "ON";
}
else
{
// Soil ok -> Pump OFF
digitalWrite(RELAY_PIN, HIGH);
soilStatus = "OK";
pumpStatus = "OFF";
}
}
void setup()
{
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
// Pump OFF initially
digitalWrite(RELAY_PIN, HIGH);
// ----------- Connect ESP32 to Running WiFi -----------
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\n=======================================");
Serial.println("WiFi Connected Successfully!");
Serial.print("WiFi Name: ");
Serial.println(ssid);
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("TCP Port: 23");
Serial.println("=======================================");
// Start TCP Server
server.begin();
server.setNoDelay(true);
}
void loop()
{
// ----------- Step 1: Accept new client connection -----------
if (!client || !client.connected())
{
client = server.available();
if (client)
{
Serial.println("New WiFi Terminal Client Connected!");
client.println("=======================================");
client.println("SMART PLANT MONITORING + AUTO WATERING");
client.println("WiFi Terminal Connected Successfully!");
client.println("Condition: Moisture < 10% => Pump ON");
client.println("=======================================");
}
}
// ----------- Step 2: Read sensor and control pump -----------
readSensorAndControlPump();
// ----------- Step 3: Create output message -----------
String message = "";
message += "ADC: ";
message += sensorValue;
message += " | Moisture: ";
message += moisturePercent;
message += "%";
message += " | Soil: ";
message += soilStatus;
message += " | Pump: ";
message += pumpStatus;
// ----------- Step 4: Print to Serial Monitor -----------
Serial.println(message);
// ----------- Step 5: Send to WiFi Terminal App -----------
if (client && client.connected())
{
client.println(message);
}
// ----------- Step 6: Delay -----------
delay(1000);
}
✅ 3) How To Use (Step-by-Step)
Step 1:
Replace:
With your actual WiFi details.
Example:
Step 2:
Upload code to ESP32.
Step 3:
Open Serial Monitor (115200)
You will see ESP32 IP like:
Step 4:
Connect your mobile to same WiFi.
Step 5:
Open WiFi Terminal App
Set:
-
IP = ESP32 IP (example 192.168.29.112)
-
Port = 23
Press Connect.
Step 6:
You will see live output like:
✅ 4) FULL DETAILED CODE EXPLANATION (Line-by-Line)
Now complete explanation.
✅ A) WiFi Library
This library allows ESP32 to connect to WiFi router.
✅ B) WiFi Credentials
ESP32 uses this to connect to your home WiFi.
✅ C) TCP Server Setup
-
server(23)creates a TCP server on port 23 -
clientstores the connected mobile device
✅ D) Pins
-
GPIO34 reads analog soil sensor
-
GPIO26 controls relay
✅ E) Calibration
Used for moisture percentage.
✅ F) Threshold
If moisture < 10% pump ON.
✅ G) readSensorAndControlPump() Function
This function is made to keep code clean.
It does:
1) Read ADC
Reads soil sensor value (0-4095)
2) Convert to Moisture %
Converts ADC to percentage.
3) Clamp
Fix range.
4) Pump logic
If moisture less than 10% → soil dry.
Pump ON
Active LOW relay means LOW = ON.
Pump OFF
HIGH = OFF.
✅ H) setup() Explanation
Serial Start
Starts Serial Monitor.
Relay setup
Pump OFF initially.
Connect to WiFi
ESP32 tries to connect.
Wait until connected
This loop keeps waiting until WiFi connects.
Print IP
This prints the router network IP.
Example:192.168.29.112
Start server
Starts TCP server.
✅ I) loop() Explanation
Accept client
If a phone connects, this returns a client.
Welcome message
This message appears in WiFi terminal.
Read sensor + control pump
Build output message
Print to serial
Send to WiFi terminal
Delay
Update every 1 second.
✅ 5) IMPORTANT NOTES
✅ 1) Mobile and ESP32 must be on SAME WiFi
Otherwise app will not connect.
✅ 2) Port 23 should not be blocked
Some routers block port 23.
If that happens, use port 8080.
If you want I can give port 8080 version too.