Course Content
πŸ“˜ MODULE 4 – Bridge Health Monitoring System
0/2
πŸ“˜ 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 Home Router Mode)

ESP32 will connect to:

  • WiFi Name: YOUR_WIFI_NAME

  • Password: YOUR_WIFI_PASSWORD

ESP32 will get an IP like:

  • 192.168.1.5 or 192.168.0.10 (depends on router)


πŸ“± WiFiTerminal App Settings

Use:

βœ… WiFi Terminal app (TCP Client)

Set:

  • IP = ESP32 IP address (shown in Serial Monitor)

  • Port = 8080


βœ… Commands

Command Action
1 Unlock
0 Lock
S Status

βœ… FULL CODE (ESP32 + Home WiFi + WiFiTerminal)

Β 

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

// ---------------- WiFi Settings ----------------
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

// ---------------- 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); // Solenoid OFF initially

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

// ---------------- Connect to WiFi ----------------
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

Serial.println("==================================");
Serial.println(" ESP32 Door Lock (Home WiFi Mode) ");
Serial.println("==================================");
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);

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

Serial.println("\nβœ… WiFi Connected!");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("TCP Port: 8080");
Serial.println("----------------------------------");

// Start TCP server
server.begin();
}

void loop() {

WiFiClient client = server.available();

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 / 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...");

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

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

isUnlocked = true;

client.println("βœ… Door Unlocked!");
}

void lockDoor(WiFiClient &client) {
client.println("πŸ”’ Locking Door...");

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

// Step 2: 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 πŸ”’");
}
}

βœ… Code Explanation (Very Easy)


πŸ”Ή 1) WiFi Station Mode

Β 
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

This makes ESP32 connect to your router like a normal device.


πŸ”Ή 2) Wait until WiFi connects

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

ESP32 keeps trying until WiFi is connected.


πŸ”Ή 3) Print IP Address

Β 
Serial.println(WiFi.localIP());

This is VERY important.

πŸ“Œ Because WiFiTerminal app needs ESP32 IP.


πŸ”Ή 4) TCP Server

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

ESP32 opens port 8080.


πŸ”Ή 5) Phone connects to ESP32

Your phone must be on same WiFi network.

Then WiFiTerminal sends commands to:

πŸ“Œ IP = ESP32 IP
πŸ“Œ Port = 8080


πŸ”Ή 6) Commands Control

Β 
if(cmd == '1') unlockDoor();
if(cmd == '0') lockDoor();

Simple and fast.


πŸ“± How to Connect in WiFiTerminal App

Step 1:

Connect your phone to same WiFi router.

Step 2:

Open Serial Monitor and copy ESP32 IP.

Example:
192.168.1.8

Step 3:

Open WiFiTerminal app

Set:

  • Mode: TCP Client

  • Server IP: 192.168.1.8

  • Port: 8080

Step 4:

Send command:

  • 1

  • 0

  • S


⚠️ Very Important Notes

βœ… 1) Same WiFi

Phone + ESP32 must be connected to same router WiFi.


βœ… 2) Router Isolation

Some routers block device-to-device connection.

If app can’t connect:

  • Turn OFF β€œAP Isolation”

  • Turn OFF β€œClient Isolation”


βœ… 3) Relay Reverse Problem

If relay works opposite, change:

For Relay ON:

Β 
digitalWrite(RELAY_PIN, LOW);

For Relay OFF:

Β 
digitalWrite(RELAY_PIN, HIGH);


Scroll to Top