💧 Smart Water Management System (Serial WiFi Terminal Hotspot Mode )
This version supports:
-
AUTO mode
-
MANUAL mode
-
Motor ON/OFF from mobile
-
Water level monitoring
-
Warning when tank FULL
📦 Components Required
-
ESP32
-
Water Level Sensor (Analog)
-
Single Channel Relay
-
DC Water Pump
-
Android TCP Terminal App
Recommended Apps:
-
TCP Telnet Terminal
-
Serial WiFi Terminal
🔌 Connections
🔹 Water Level Sensor
| Sensor | ESP32 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| AO | GPIO 34 |
🔹 Relay Module
| Relay | ESP32 |
|---|---|
| VCC | 5V |
| GND | GND |
| IN | GPIO 26 |
⚠ Relay Active LOW
LOW → Motor ON
HIGH → Motor OFF
💻 COMPLETE FINAL CODE (Hotspot Mode + TCP Server)
Copy & paste into Arduino IDE:
#include <WiFi.h>
#define WATER_SENSOR 34
#define RELAY_PIN 26
const char* ssid = "Water_System_AP";
const char* password = "12345678";
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
// Start Access Point
WiFi.softAP(ssid, password);
Serial.println("Access Point Started");
Serial.print("AP IP Address: ");
Serial.println(WiFi.softAPIP());
server.begin();
Serial.println("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 (Step-by-Step)
1️⃣ Create Hotspot (Access Point Mode)
ESP32 becomes a WiFi router.
Mobile connects to:
Password: 12345678
2️⃣ Get ESP32 IP Address
Default IP:
You use this in mobile app.
3️⃣ Create TCP Server
Port 23 is Telnet communication port.
4️⃣ Read Water Sensor
Reads water level (0–4095).
5️⃣ Auto Mode Logic
If:
→ Tank LOW → Motor ON
If:
→ Tank FULL → Motor OFF + Warning
6️⃣ Manual Mode Logic
Manual works only if:
Commands:
| Command | Function |
|---|---|
| AUTO | Enable automatic control |
| MANUAL | Enable manual control |
| ON | Motor ON |
| OFF | Motor OFF |
7️⃣ Relay Control
Relay Active LOW:
digitalWrite(RELAY_PIN, HIGH); // Motor OFF
📱 How To Use Mobile App
Step 1
Upload code
Step 2
Open Serial Monitor
You will see:
AP IP Address: 192.168.4.1
Step 3
Mobile WiFi → Connect to:
Step 4
Open TCP Client App
Enter:
-
Host: 192.168.4.1
-
Port: 23
Click Connect
Step 5
Send commands:
MANUAL
ON
OFF
📟 Example Output in App
Water LOW -> Motor ON
Motor is ON
Water Level: 3200
Tank FULL -> Motor OFF
Motor is OFF
WARNING: Tank Full!
🎯 What Students Learn
-
Access Point Mode
-
TCP Client-Server communication
-
Analog sensor reading
-
Relay motor control
-
Auto + Manual system design
-
Embedded networking