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

💧 Smart Water Management System

(ESP32 + Ultrasonic + Relay + WiFi Hotspot Mode)

In this lesson, ESP32 works as a WiFi Hotspot (Access Point).

Your mobile connects directly to ESP32.

This version supports:

  • 📏 Ultrasonic water level measurement

  • 📊 Tank percentage calculation

  • 🔄 AUTO mode

  • 🎮 MANUAL mode

  • 📱 Motor ON/OFF from mobile

  • 🚨 Tank FULL warning

  • 🌐 No router required

Internet not required ❌
Router not required ❌
ESP32 Hotspot ✔


📦 Components Used

  • ESP32

  • HC-SR04 Ultrasonic Sensor

  • Single Channel Relay

  • DC Water Pump

  • Android Phone

  • 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

(WiFi Hotspot Mode + TCP Server + Ultrasonic)

Copy & paste into Arduino IDE:

#include <WiFi.h>

#define TRIG_PIN 5
#define ECHO_PIN 18
#define RELAY_PIN 26

const char* ssid = "Water_System_AP";
const char* password = "12345678";

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);

WiFi.softAP(ssid, password);

Serial.println("Hotspot Started");
Serial.print("AP IP Address: ");
Serial.println(WiFi.softAPIP());

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️⃣ ESP32 Hotspot Mode

 
 
WiFi.softAP(ssid, password);
 

ESP32 becomes WiFi router:

Network Name:

 
 
Water_System_AP
 

Password:

 
 
12345678
 

Default IP:

 
 
192.168.4.1
 

2️⃣ TCP Server

 
 
WiFiServer server(23);
 

Creates Telnet server on Port 23.

Mobile connects using:

  • Host: 192.168.4.1

  • Port: 23


3️⃣ Ultrasonic Calculation

 
 
distance = duration * 0.034 / 2;
percentage = (waterLevel / tankHeight) * 100;
 

Calculates tank water percentage.


4️⃣ AUTO Mode Logic

If tank < 20%
→ Motor ON

If tank > 90%
→ Motor OFF + Warning


5️⃣ MANUAL Mode Commands

Command Function
AUTO Enable auto mode
MANUAL Enable manual mode
ON Motor ON
OFF Motor OFF

📱 How To Use

1️⃣ Upload code
2️⃣ Open Serial Monitor
3️⃣ Note IP address (usually 192.168.4.1)
4️⃣ Connect mobile WiFi to:

 
 
Water_System_AP
 

5️⃣ Open TCP Terminal App

Enter:

  • Host: 192.168.4.1

  • Port: 23

6️⃣ Send commands:

 
 
AUTO
MANUAL
ON
OFF
 

📟 Example Output

 
 
Tank Level: 15 %
Water LOW -> Motor ON
Motor is ON

Tank Level: 92 %
Tank FULL -> Motor OFF
Motor is OFF
WARNING: Tank Full!

 

🎯 What Students Learn

  • Access Point Mode

  • TCP Client-Server communication

  • Ultrasonic tank measurement

  • Wireless control without router

  • Auto + Manual system logic

  • Industrial IoT architecture

Scroll to Top