🌱 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 creates Hotspot (Access Point)
✅ Mobile connects to ESP32 WiFi
✅ Open WiFi Terminal app
✅ ESP32 sends live text like Serial Monitor
✅ Moisture < 10% → Pump ON
✅ Moisture ≥ 10% → Pump OFF
✅ 1) Best WiFi Terminal App (Android)
Use this app:
📱 “TCP Telnet Terminal”
or
📱 “WiFi Telnet Terminal”
(Any app that supports TCP Client / Telnet will work)
✅ 2) ESP32 Hotspot WiFi Terminal Code
⚡ Connections:
-
Soil Sensor AO → GPIO34
-
Relay IN → GPIO26
This code makes ESP32 act as:
✅ Hotspot
✅ TCP Server (like WiFi Serial terminal)
/************************************************************
SMART PLANT MONITORING + AUTO WATERING SYSTEM (WiFi Terminal)
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>
// ----------- Hotspot WiFi Details -----------
const char* ssid = "ESP32_PLANT_WIFI";
const char* password = "12345678";
// ----------- 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 = "";
void setup()
{
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
// Pump OFF initially
digitalWrite(RELAY_PIN, HIGH);
// ----------- Start ESP32 Hotspot -----------
WiFi.softAP(ssid, password);
Serial.println("=======================================");
Serial.println("ESP32 WiFi Terminal Started!");
Serial.print("Hotspot SSID: ");
Serial.println(ssid);
Serial.print("Hotspot Password: ");
Serial.println(password);
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.softAPIP());
Serial.println("TCP Port: 23");
Serial.println("=======================================");
// ----------- Start TCP Server -----------
server.begin();
server.setNoDelay(true);
}
// ----------- Function: Read sensor + control pump -----------
void readSensorAndControlPump()
{
sensorValue = analogRead(SOIL_SENSOR_PIN);
moisturePercent = map(sensorValue, dryValue, wetValue, 0, 100);
if (moisturePercent > 100) moisturePercent = 100;
if (moisturePercent < 0) moisturePercent = 0;
if (moisturePercent < pumpThreshold)
{
// Soil dry -> Pump ON
digitalWrite(RELAY_PIN, LOW);
soilStatus = "DRY";
pumpStatus = "ON";
}
else
{
// Soil ok -> Pump OFF
digitalWrite(RELAY_PIN, HIGH);
soilStatus = "OK";
pumpStatus = "OFF";
}
}
void loop()
{
// ----------- Step 1: Check if client connected -----------
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 + control pump -----------
readSensorAndControlPump();
// ----------- Step 3: Create terminal 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 WiFi Terminal App (Step-by-Step)
✅ Step 1: Upload Code
Upload this code to ESP32.
✅ Step 2: Connect Mobile to ESP32 WiFi
Go to mobile WiFi settings and connect:
📶 SSID: ESP32_PLANT_WIFI
🔐 Password: 12345678
✅ Step 3: Open WiFi Terminal App
In the app set:
-
Mode: TCP Client / Telnet
-
IP Address:
192.168.4.1 -
Port:
23
Then press Connect.
✅ Step 4: Result
You will see output like:
✅ 4) FULL CODE EXPLANATION (Line-by-Line)
Now deep explanation.
✅ A) WiFi Library
This library provides ESP32 WiFi features.
✅ B) Hotspot Name and Password
ESP32 will create WiFi hotspot with these details.
✅ C) TCP Server Creation
This makes ESP32 act like a server.
Port 23 is standard for Telnet terminal.
✅ D) WiFi Client Object
This stores the connected mobile device.
✅ E) Pin Setup
-
Soil sensor analog output on GPIO34
-
Relay control on GPIO26
✅ F) Calibration Values
Used for converting sensor ADC to moisture %.
✅ G) Threshold
Main logic:
-
<10% → Pump ON
-
=10% → Pump OFF
✅ setup() Explanation
1) Serial Monitor Start
Starts Serial Monitor.
2) Relay Output Pin
Relay needs output.
3) Pump OFF Initially
Active LOW relay:
-
HIGH = OFF
4) Start Hotspot
ESP32 starts WiFi Access Point mode.
5) Print IP Address
Shows ESP32 IP address.
Default is:192.168.4.1
6) Start TCP Server
Starts the terminal server.
✅ loop() Explanation
Step 1: Client Connection Check
Means:
-
If no phone connected, wait for new client.
Accept new client
If phone connects, this returns client object.
Welcome message
Shows message on WiFi terminal app.
Step 2: Read sensor + control pump
This function does:
-
read sensor ADC
-
convert to moisture %
-
pump ON/OFF
Step 3: Create output message
This builds one line message.
Step 4: Print Serial
So you also see output on laptop.
Step 5: Send to WiFi Terminal App
Sends message to mobile app only if connected.
Step 6: Delay
Updates every 1 second.
✅ 5) Final Result (You Will Get)
On WiFi Terminal app you will see:
-
ADC value
-
Moisture %
-
Soil status
-
Pump status
And pump will automatically ON/OFF.