Course Content
📘 MODULE 6 – Smart Pet Feeder
0/1
📘 MODULE 7 – Smart Water Management System
0/2
📘 MODULE 8 – Water Quality Monitoring System
0/2
📘 MODULE 10 – Gas Leakage Detection System
0/2
Mastering IoT with 11 Real-World Projects and 1 mega project

✅ ESP32 Smart Door Lock (WiFi Terminal Hotspot Mode)

ESP32 + Servo + Solenoid Lock + 1-Channel Relay

📱 What App to Use

Use Android app:

WiFi Terminal
(or TCP/UDP Terminal)

This code uses TCP Server (best + stable).


✅ ESP32 Hotspot Details

ESP32 will create:

  • WiFi Name (SSID): ESP32_DoorLock

  • Password: 12345678

  • IP Address: 192.168.4.1

  • Port: 8080


✅ Commands (Send in WiFiTerminal)

Command Action
1 Unlock
0 Lock
S Status

✅ FULL CODE (ESP32 Hotspot + WiFi Terminal)

#include <WiFi.h>
#include <Servo.h>

// ---------------- Hotspot Settings ----------------
const char* ssid = "ESP32_DoorLock";
const char* password = "12345678";

// ---------------- TCP Server ----------------
WiFiServer server(8080);

// ---------------- Pins ----------------
#define SERVO_PIN 13
#define RELAY_PIN 26

// ---------------- Servo ----------------
Servo lockServo;

int LOCK_ANGLE = 0;
int UNLOCK_ANGLE = 90;

bool isUnlocked = false;

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

// Relay setup
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);

// Servo setup
lockServo.attach(SERVO_PIN);
lockServo.write(LOCK_ANGLE);

// Start ESP32 Hotspot
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);

IPAddress IP = WiFi.softAPIP();

Serial.println("==================================");
Serial.println(" ESP32 Door Lock Hotspot Started ");
Serial.println("==================================");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Password: ");
Serial.println(password);
Serial.print("ESP32 IP: ");
Serial.println(IP);
Serial.println("TCP Port: 8080");
Serial.println("----------------------------------");

// Start server
server.begin();
}

void loop() {

WiFiClient client = server.available(); // check client connection

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

client.println("Welcome to ESP32 Door Lock!");
client.println("Commands:");
client.println("1 = Unlock");
client.println("0 = Lock");
client.println("S = Status");
client.println("---------------------------");

while (client.connected()) {

if (client.available()) {
char cmd = client.read();

// ignore newline, carriage return, spaces
if (cmd == '\n' || cmd == '\r' || cmd == ' ') continue;

Serial.print("Command Received: ");
Serial.println(cmd);

if (cmd == '1') {
unlockDoor(client);
}
else if (cmd == '0') {
lockDoor(client);
}
else if (cmd == 'S' || cmd == 's') {
showStatus(client);
}
else {
client.println("❌ Invalid Command! Use 1 / 0 / S");
}
}
}

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

// ---------------- Functions ----------------

void unlockDoor(WiFiClient &client) {
client.println("🔓 Unlocking Door...");

// Servo unlock
lockServo.write(UNLOCK_ANGLE);
delay(700);

// Relay ON (Solenoid ON)
digitalWrite(RELAY_PIN, HIGH);
delay(200);

isUnlocked = true;

client.println("✅ Door Unlocked!");
}

void lockDoor(WiFiClient &client) {
client.println("🔒 Locking Door...");

// Relay OFF (Solenoid OFF)
digitalWrite(RELAY_PIN, LOW);
delay(200);

// Servo lock
lockServo.write(LOCK_ANGLE);
delay(700);

isUnlocked = false;

client.println("✅ Door Locked!");
}

void showStatus(WiFiClient &client) {
if (isUnlocked) {
client.println("📌 Status: Door is UNLOCKED 🔓");
} else {
client.println("📌 Status: Door is LOCKED 🔒");
}
}

✅ How to Use (Step-by-Step)

✅ Step 1: Upload code

Upload to ESP32 using Arduino IDE.


✅ Step 2: Connect phone to ESP32 Hotspot

Go to phone WiFi → connect to:

📌 SSID: ESP32_DoorLock
📌 Password: 12345678


✅ Step 3: Open WiFiTerminal App

In WiFiTerminal App set:

  • Mode: TCP Client

  • IP: 192.168.4.1

  • Port: 8080

Then connect.


✅ Step 4: Send command

Send:

  • 1 → Unlock

  • 0 → Lock

  • S → Status


✅ Code Explanation (Simple)

🔹 1) Hotspot Mode

 
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);

This makes ESP32 act like a WiFi router.


🔹 2) ESP32 IP

 
WiFi.softAPIP();

ESP32 always gives:
📌 192.168.4.1


🔹 3) TCP Server

 
WiFiServer server(8080);
server.begin();

ESP32 starts a TCP server on port 8080.


🔹 4) Client Communication

 
WiFiClient client = server.available();

If phone connects, ESP32 accepts the connection.


🔹 5) Command Read

 
char cmd = client.read();

Reads one character from app.


🔹 6) Servo + Relay control

  • Servo rotates for latch

  • Relay ON/OFF controls solenoid


⭐ IMPORTANT NOTE (Relay Logic)

Some relay modules work reverse:

  • HIGH = OFF

  • LOW = ON

If your relay behaves opposite, tell me — I’ll correct it in 5 seconds 😄




Scroll to Top