β 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.5or192.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
This makes ESP32 connect to your router like a normal device.
πΉ 2) Wait until WiFi connects
ESP32 keeps trying until WiFi is connected.
πΉ 3) Print IP Address
This is VERY important.
π Because WiFiTerminal app needs ESP32 IP.
πΉ 4) TCP Server
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
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: