Perfect Shiv 👑🔥
Here is your ready-to-upload lesson content for:
📘 Lesson 7.6 – Smart Water Management System Code
(Serial WiFi Terminal – Home WiFi Mode – Ultrasonic)
💧 Smart Water Management System
(ESP32 + Ultrasonic + Relay + Home WiFi Mode)
In this lesson, ESP32 connects to your home WiFi router instead of hotspot mode.
Your mobile and ESP32 must be connected to the same WiFi network.
This version supports:
-
📏 Ultrasonic water level measurement
-
📊 Tank percentage calculation
-
🔄 AUTO mode
-
🎮 MANUAL mode
-
📱 Motor ON/OFF from mobile
-
🚨 Tank FULL warning
-
🌐 Router-based communication
Router required ✔
Internet not compulsory (Local network works) ✔
📦 Components Used
-
ESP32
-
HC-SR04 Ultrasonic Sensor
-
Single Channel Relay
-
DC Water Pump
-
Home WiFi Router
-
Android TCP Terminal App
Recommended Apps:
-
TCP Telnet Terminal
-
Serial WiFi Terminal
🔌 Pin Configuration
🔹 Ultrasonic Sensor
| Ultrasonic Pin | ESP32 |
|---|---|
| VCC | 5V |
| GND | GND |
| Trig | GPIO 5 |
| Echo | GPIO 18 |
⚠ Use voltage divider for Echo (5V → 3.3V)
🔹 Relay Module
| Relay Pin | ESP32 |
|---|---|
| VCC | 5V |
| GND | GND |
| IN | GPIO 26 |
Relay Active LOW
LOW → Motor ON
HIGH → Motor OFF
💻 COMPLETE CODE
(Home WiFi Mode + TCP Server + Ultrasonic)
Replace WiFi credentials before uploading.
#include <WiFi.h>
#define TRIG_PIN 5
#define ECHO_PIN 18
#define RELAY_PIN 26
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
WiFiServer server(23);
long duration;
float distance;
float tankHeight = 30.0;
float waterLevel;
float percentage;
bool autoMode = true;
bool motorState = false;
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
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()) {
// Ultrasonic Measurement
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
waterLevel = tankHeight - distance;
if (waterLevel < 0) waterLevel = 0;
if (waterLevel > tankHeight) waterLevel = tankHeight;
percentage = (waterLevel / tankHeight) * 100;
client.print("Tank Level: ");
client.print(percentage);
client.println(" %");
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);
}
}
// AUTO MODE LOGIC
if (autoMode) {
if (percentage < 20 && !motorState) {
client.println("Water LOW -> Motor ON");
turnMotorOn(client);
}
if (percentage > 90 && 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️⃣ Connect to Home WiFi
ESP32 connects to your router.
Prints IP address.
Example:
You must use this IP in mobile app.
2️⃣ TCP Server Creation
Creates Telnet server on Port 23.
Mobile connects using:
-
Host: ESP32 IP
-
Port: 23
3️⃣ Ultrasonic Distance Calculation
Calculates distance in cm.
4️⃣ Tank Percentage Calculation
Gives tank water percentage.
5️⃣ AUTO Mode Logic
If tank < 20%
→ Motor ON
If tank > 90%
→ Motor OFF + Warning
6️⃣ MANUAL Mode Commands
| Command | Function |
|---|---|
| AUTO | Enable auto mode |
| MANUAL | Enable manual mode |
| ON | Motor ON |
| OFF | Motor OFF |
📱 How To Use
Step 1
Upload code
Step 2
Open Serial Monitor
Note ESP32 IP address
Step 3
Connect mobile to same WiFi
Step 4
Open TCP Terminal App
Enter:
-
Host: ESP32 IP
-
Port: 23
Step 5
Send commands:
MANUAL
ON
OFF
📟 Example Output
Water LOW -> Motor ON
Motor is ON
Tank Level: 93 %
Tank FULL -> Motor OFF
Motor is OFF
WARNING: Tank Full!
🎯 What Students Learn
-
Home WiFi networking
-
TCP client-server communication
-
Ultrasonic tank measurement
-
Router-based IoT system
-
Auto + Manual control logic
-
Embedded networking architecture