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

💧 Smart Water Management System

(Serial WiFi Terminal – Home WiFi Version)

This version connects ESP32 to your home WiFi router instead of hotspot mode.

It supports:

  • ✅ AUTO Mode

  • ✅ MANUAL Mode

  • ✅ Motor ON/OFF from mobile

  • ✅ Real-time water level monitoring

  • ✅ Warning when tank FULL

  • ✅ Works inside same WiFi network

Router required ✔
Internet not compulsory ✔


📦 Components Required

  • ESP32

  • Water Level Sensor (Analog type)

  • Single Channel Relay

  • DC Water Pump

  • Home WiFi Router

  • Android TCP Terminal App

Recommended Apps:

  • TCP Telnet Terminal

  • Serial WiFi Terminal


🔌 Connections

🔹 Water Level Sensor

Sensor Pin ESP32
VCC 3.3V
GND GND
AO GPIO 34

🔹 Relay Module

Relay Pin ESP32
VCC 5V
GND GND
IN GPIO 26

⚠ Relay Active LOW
LOW → Motor ON
HIGH → Motor OFF


💻 COMPLETE FINAL CODE

(Home WiFi + TCP Server Version)

Replace WiFi credentials before uploading.

#include <WiFi.h>

#define WATER_SENSOR 34
#define RELAY_PIN 26

// Replace with your WiFi credentials
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

WiFiServer server(23); // Telnet Port

int waterValue = 0;

bool autoMode = true;
bool motorState = false;

int lowLevel = 1000;
int fullLevel = 3000;

void setup() {
Serial.begin(115200);

pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Motor OFF

Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("\nWiFi Connected!");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());

server.begin();
Serial.println("TCP Server Started on Port 23");
}

void loop() {

WiFiClient client = server.available();

if (client) {
Serial.println("Client Connected");

client.println("=== Smart Water Management ===");
client.println("Commands: AUTO, MANUAL, ON, OFF");

while (client.connected()) {

waterValue = analogRead(WATER_SENSOR);

client.print("Water Level: ");
client.println(waterValue);

if (client.available()) {

String command = client.readStringUntil('\n');
command.trim();

if (command.equalsIgnoreCase("AUTO")) {
autoMode = true;
client.println("Switched to AUTO Mode");
}

else if (command.equalsIgnoreCase("MANUAL")) {
autoMode = false;
client.println("Switched to MANUAL Mode");
}

else if (command.equalsIgnoreCase("ON") && !autoMode) {
turnMotorOn(client);
}

else if (command.equalsIgnoreCase("OFF") && !autoMode) {
turnMotorOff(client);
}

else {
client.println("Invalid Command!");
}
}

// AUTO MODE LOGIC
if (autoMode) {

if (waterValue < lowLevel && !motorState) {
client.println("Water LOW -> Motor ON");
turnMotorOn(client);
}

if (waterValue > fullLevel && motorState) {
client.println("Tank FULL -> Motor OFF");
turnMotorOff(client);
client.println("WARNING: Tank Full!");
}
}

delay(2000);
}

client.stop();
Serial.println("Client Disconnected");
}
}

void turnMotorOn(WiFiClient client) {
digitalWrite(RELAY_PIN, LOW);
motorState = true;
client.println("Motor is ON");
}

void turnMotorOff(WiFiClient client) {
digitalWrite(RELAY_PIN, HIGH);
motorState = false;
client.println("Motor is OFF");
}

🧠 Code Explanation


1️⃣ WiFi Connection

 
 
WiFi.begin(ssid, password);
 

ESP32 connects to your home router.

 
 
WiFi.localIP();
 

Prints ESP32 IP address.

Example:

 
 
192.168.1.45
 

Use this IP in mobile app.


2️⃣ TCP Server

 
 
WiFiServer server(23);
 

Creates Telnet server on port 23.

Mobile app connects using:

  • Host: ESP32 IP

  • Port: 23


3️⃣ Auto Mode Logic

If water value < lowLevel
→ Motor turns ON

If water value > fullLevel
→ Motor turns OFF + Warning


4️⃣ Manual Mode Logic

Manual mode works only when:

 
 
autoMode = false
 

Commands:

AUTO → Enable automatic
MANUAL → Enable manual
ON → Motor ON
OFF → Motor OFF


5️⃣ Relay Control

Relay is Active LOW:

 
 
LOW → Motor ON
HIGH → Motor OFF
 

📱 How To Use Mobile App

Step 1

Upload code

Step 2

Open Serial Monitor
Note IP address

Example:

 
 
ESP32 IP Address: 192.168.1.45
 

Step 3

Connect your mobile to same WiFi

Step 4

Open TCP Terminal App

Enter:

  • Host: 192.168.1.45

  • Port: 23

Click Connect

Step 5

Send Commands:

 
 
AUTO
MANUAL
ON
OFF
 

📟 Example Output

 
 
Water Level: 850
Water LOW -> Motor ON
Motor is ON

Water Level: 3200
Tank FULL -> Motor OFF
Motor is OFF
WARNING: Tank Full!

 

🎯 What Students Learn

  • Home WiFi networking

  • TCP client-server communication

  • Analog sensor reading

  • Relay motor control

  • Auto vs Manual system logic

  • Embedded networking fundamentals

Scroll to Top