Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

🌱 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:

Β 
ADC: 3800 | Moisture: 6% | Soil: DRY | Pump: ON
ADC: 2600 | Moisture: 22% | Soil: OK | Pump: OFF

βœ… 4) FULL CODE EXPLANATION (Line-by-Line)

Now deep explanation.


βœ… A) WiFi Library

Β 
#include <WiFi.h>

This library provides ESP32 WiFi features.


βœ… B) Hotspot Name and Password

Β 
const char* ssid = "ESP32_PLANT_WIFI";
const char* password = "12345678";

ESP32 will create WiFi hotspot with these details.


βœ… C) TCP Server Creation

Β 
WiFiServer server(23);

This makes ESP32 act like a server.

Port 23 is standard for Telnet terminal.


βœ… D) WiFi Client Object

Β 
WiFiClient client;

This stores the connected mobile device.


βœ… E) Pin Setup

Β 
#define SOIL_SENSOR_PIN 34
#define RELAY_PIN 26
  • Soil sensor analog output on GPIO34

  • Relay control on GPIO26


βœ… F) Calibration Values

Β 
int dryValue = 3500;
int wetValue = 1500;

Used for converting sensor ADC to moisture %.


βœ… G) Threshold

Β 
int pumpThreshold = 10;

Main logic:

  • <10% β†’ Pump ON

  • =10% β†’ Pump OFF


βœ… setup() Explanation


1) Serial Monitor Start

Β 
Serial.begin(115200);

Starts Serial Monitor.


2) Relay Output Pin

Β 
pinMode(RELAY_PIN, OUTPUT);

Relay needs output.


3) Pump OFF Initially

Β 
digitalWrite(RELAY_PIN, HIGH);

Active LOW relay:

  • HIGH = OFF


4) Start Hotspot

Β 
WiFi.softAP(ssid, password);

ESP32 starts WiFi Access Point mode.


5) Print IP Address

Β 
Serial.println(WiFi.softAPIP());

Shows ESP32 IP address.

Default is:
192.168.4.1


6) Start TCP Server

Β 
server.begin();
server.setNoDelay(true);

Starts the terminal server.


βœ… loop() Explanation


Step 1: Client Connection Check

Β 
if (!client || !client.connected())

Means:

  • If no phone connected, wait for new client.


Accept new client

Β 
client = server.available();

If phone connects, this returns client object.


Welcome message

Β 
client.println("SMART PLANT MONITORING...");

Shows message on WiFi terminal app.


Step 2: Read sensor + control pump

Β 
readSensorAndControlPump();

This function does:

  • read sensor ADC

  • convert to moisture %

  • pump ON/OFF


Step 3: Create output message

Β 
String message = "";
message += "ADC: ";
...

This builds one line message.


Step 4: Print Serial

Β 
Serial.println(message);

So you also see output on laptop.


Step 5: Send to WiFi Terminal App

Β 
if (client && client.connected())
{
client.println(message);
}

Sends message to mobile app only if connected.


Step 6: Delay

Β 
delay(1000);

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.

Scroll to Top