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

📘 Home Security System ( WiFi Hotspot Version )


🎯 Objective of This Lesson

Students will learn:

  • What is Access Point (AP Mode)

  • How ESP32 creates its own WiFi network

  • How to connect mobile directly to ESP32

  • How to receive motion alerts via WiFi Serial Terminal app


🔌 Hardware Reminder

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

📡 How This System Works

1️⃣ ESP32 creates WiFi hotspot
2️⃣ Mobile connects to ESP32 WiFi
3️⃣ WiFi Serial Terminal app connects via IP & Port
4️⃣ Motion alerts are sent using TCP communication

No router needed.


🧾 Complete ESP32 Code (Hotspot Version – Copy & Paste)

#include <WiFi.h>

#define PIR_PIN 27

// -------- WiFi Access Point --------
const char* ssid = "HomeSecurity_AP";
const char* password = "12345678";

WiFiServer server(80);

int motionState = 0;
int lastState = 0;

void setup() {

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

// Start WiFi Access Point
WiFi.softAP(ssid, password);
server.begin();

Serial.println("WiFi Hotspot Started");
Serial.print("IP Address: ");
Serial.println(WiFi.softAPIP());

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️⃣ Include WiFi Library

 
#include <WiFi.h>
 

Allows ESP32 to use WiFi features.


2️⃣ Create Hotspot (AP Mode)

 
WiFi.softAP(ssid, password);
 

ESP32 becomes a WiFi network.

Network Name:
👉 HomeSecurity_AP

Password:
👉 12345678


3️⃣ Start TCP Server

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

Creates TCP server on Port 80.

Port 80 is standard communication port.


4️⃣ Get Hotspot IP Address

 
WiFi.softAPIP();
 

Default IP is usually:

👉 192.168.4.1

Students must enter this in WiFi Serial Terminal app.


5️⃣ Detect Client Connection

 
WiFiClient client = server.available();
 

If mobile connects → client becomes active.


6️⃣ Motion Detection Logic

 
motionState = digitalRead(PIR_PIN);
 

If motion detected:

Send:

⚠ MOTION DETECTED! Intruder Alert!

If motion stops:

Send:

Area Clear – No Motion


📱 How to Connect (WiFi Serial Terminal App)

Step 1 – Upload code
Step 2 – Open Serial Monitor
Step 3 – Note IP address (usually 192.168.4.1)
Step 4 – Connect phone to WiFi → HomeSecurity_AP
Step 5 – Open WiFi Serial Terminal app
Step 6 – Enter:

IP → 192.168.4.1
Port → 80

Step 7 – Connect

Now motion alerts will appear on mobile.


📊 Example WiFi Output

⚠ MOTION DETECTED! Intruder Alert!

Area Clear – No Motion


🎓 Learning Outcomes

Students now understand:

  • Access Point Mode

  • TCP communication

  • Wireless motion alerts

  • Local IoT communication without internet

Scroll to Top