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

📘 Home Security System ( WiFi Home Router Mode )


🎯 Objective of This Lesson

Students will learn:

  • What is Station Mode (STA Mode)

  • How ESP32 connects to home WiFi

  • How router assigns IP address

  • How to receive motion alerts using WiFi Serial Terminal app

  • Local network communication


🔌 Hardware Reminder

PIR Pin ESP32 Pin
VCC 5V
GND GND
OUT GPIO 27

🌐 How This System Works

1️⃣ ESP32 connects to home WiFi
2️⃣ Router assigns IP (example: 192.168.1.105)
3️⃣ Mobile connects to same WiFi
4️⃣ WiFi Serial Terminal app connects using IP & Port
5️⃣ Motion alerts are received wirelessly


🧾 Complete ESP32 Code (Router Mode – Copy & Paste)

⚠ Replace WiFi credentials before uploading.

#include <WiFi.h>

#define PIR_PIN 27

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

WiFiServer server(80);

int motionState = 0;
int lastState = 0;

void setup() {

Serial.begin(115200);
pinMode(PIR_PIN, INPUT);

Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);

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

Serial.println("\nConnected to WiFi");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());

server.begin();

Serial.println("Warming up PIR Sensor...");
delay(30000);
Serial.println("System Ready!");
}

void loop() {

WiFiClient client = server.available();

if (client) {

motionState = digitalRead(PIR_PIN);

if (motionState == HIGH && lastState == LOW) {
client.println("⚠ MOTION DETECTED! Intruder Alert!");
lastState = HIGH;
}

else if (motionState == LOW && lastState == HIGH) {
client.println("Area Clear - No Motion");
lastState = LOW;
}

delay(200);
}
}


🧠 Code Explanation (Step-by-Step)


1️⃣ Station Mode (STA Mode)

 
WiFi.begin(ssid, password);
 

ESP32 connects to router like a mobile phone.

This is called:

👉 Station Mode


2️⃣ Wait Until Connected

 
while (WiFi.status() != WL_CONNECTED)
 

Program waits until WiFi connection is successful.


3️⃣ Get Assigned IP Address

 
WiFi.localIP();
 

Router assigns dynamic IP.

Example:

👉 192.168.1.105

Students must enter this IP in WiFi Serial Terminal app.


4️⃣ Start TCP Server

 
WiFiServer server(80);
server.begin();
 

Creates TCP server on Port 80.


5️⃣ Detect Client Connection

 
WiFiClient client = server.available();
 

If mobile connects → client becomes active.


6️⃣ Motion Detection Logic

 
motionState = digitalRead(PIR_PIN);
 

If motion starts:

Send:

⚠ MOTION DETECTED! Intruder Alert!

If motion stops:

Send:

Area Clear – No Motion


📱 How to Connect (WiFi Serial Terminal App)

Step 1 – Replace WiFi name & password
Step 2 – Upload code
Step 3 – Open Serial Monitor
Step 4 – Note IP address (example: 192.168.1.105)
Step 5 – Connect phone to same WiFi
Step 6 – Open WiFi Serial Terminal app
Step 7 – Enter:

IP → 192.168.1.105
Port → 80

Step 8 – Connect

Now motion alerts will appear.


📊 Example WiFi Output

⚠ MOTION DETECTED! Intruder Alert!

Area Clear – No Motion


🔎 Difference: Hotspot vs Router Mode

Feature Hotspot Mode Router Mode
Internet Needed No Router required
IP Address 192.168.4.1 192.168.1.x
Range Limited Router coverage
Multiple Devices Limited Yes

🎓 Learning Outcomes

Students now understand:

  • Station Mode networking

  • Local network communication

  • IP address concept

  • TCP server communication

  • Practical IoT security system



Scroll to Top