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

📡 Smart Parking System – Home WiFi Mode (WiFi Terminal App)

Home WiFi (Station Mode)
Serial WiFi Terminal App
✅ TCP communication
✅ Real-time parking status on mobile

💻 Complete ESP32 Code (Home WiFi + TCP Server)

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

// --------- Enter Your Home WiFi Details ---------
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

WiFiServer server(8080);
WiFiClient client;

// -------- IR Sensors --------
int ir1 = 14;
int ir2 = 27;
int ir3 = 26;
int ir4 = 25;
int ir5 = 33;

// -------- Ultrasonic --------
#define TRIG 4
#define ECHO 5

// -------- Servo --------
Servo gateServo;
int servoPin = 18;

long duration;
float distance;

void setup() {

Serial.begin(115200);

pinMode(ir1, INPUT);
pinMode(ir2, INPUT);
pinMode(ir3, INPUT);
pinMode(ir4, INPUT);
pinMode(ir5, INPUT);

pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);

gateServo.attach(servoPin);
gateServo.write(0); // Gate Closed

// -------- Connect to Home WiFi --------
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("TCP Server Started on Port 8080");
}

void loop() {

client = server.available();

if (client) {

client.println("Smart Parking System Connected");
client.println("--------------------------------");

while (client.connected()) {

int available = 0;

bool slot1 = digitalRead(ir1);
bool slot2 = digitalRead(ir2);
bool slot3 = digitalRead(ir3);
bool slot4 = digitalRead(ir4);
bool slot5 = digitalRead(ir5);

if (slot1 == HIGH) available++;
if (slot2 == HIGH) available++;
if (slot3 == HIGH) available++;
if (slot4 == HIGH) available++;
if (slot5 == HIGH) available++;

client.println("------ Parking Status ------");

client.print("Slot 1: ");
client.println(slot1 ? "Available" : "Occupied");

client.print("Slot 2: ");
client.println(slot2 ? "Available" : "Occupied");

client.print("Slot 3: ");
client.println(slot3 ? "Available" : "Occupied");

client.print("Slot 4: ");
client.println(slot4 ? "Available" : "Occupied");

client.print("Slot 5: ");
client.println(slot5 ? "Available" : "Occupied");

client.print("Total Available: ");
client.println(available);

// -------- Ultrasonic --------
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);

duration = pulseIn(ECHO, HIGH);
distance = duration * 0.034 / 2;

client.print("Entrance Distance: ");
client.print(distance);
client.println(" cm");

if (distance < 15) {

client.println("Car detected at entrance!");

if (available > 0) {
client.println("Slots Available - Opening Gate");
gateServo.write(90);
delay(3000);
gateServo.write(0);
}
else {
client.println("Parking Full - Gate Closed");
}
}

client.println("------------------------------");
delay(2000);
}

client.stop();
}
}

🧠 FULL CODE EXPLANATION (Deep Understanding)


🔹 1️⃣ WiFi Station Mode

 
 
WiFi.begin(ssid, password);
 

This connects ESP32 to your home router.

Unlike hotspot mode:
ESP32 does NOT create WiFi.
It joins your existing network.


🔹 2️⃣ Checking Connection

 
 
while (WiFi.status() != WL_CONNECTED)
 

ESP32 waits until successfully connected.


🔹 3️⃣ Getting IP Address

 
 
WiFi.localIP();
 

Router assigns an IP address.

Example:
192.168.1.105

⚠ This IP will change sometimes (DHCP).


🔹 4️⃣ TCP Server

 
 
WiFiServer server(8080);
 

Server runs on Port 8080.

Mobile app connects using:
IP address + 8080


🔹 5️⃣ IR Slot Detection

 
 
bool slot1 = digitalRead(ir1);
 

If HIGH → Slot Available
If LOW → Slot Occupied

Available slots counted.


🔹 6️⃣ Ultrasonic Distance Formula

 
 
distance = duration * 0.034 / 2;
 

Measures entrance distance.

If less than 15 cm → Car detected.


🔹 7️⃣ Gate Logic

If:
Car detected AND available > 0
→ Open gate

If:
Car detected AND available = 0
→ Keep gate closed


📱 How To Connect WiFi Terminal App (Home WiFi)

1️⃣ Upload code
2️⃣ Open Serial Monitor
3️⃣ Note IP address shown

Example:
192.168.1.105

4️⃣ Connect your mobile to SAME WiFi

5️⃣ Open Serial WiFi Terminal App

Add new connection:

Host: 192.168.1.105
Port: 8080

Press CONNECT


📊 Example Output in App

 
 
—— Parking Status ——
Slot 1: Available
Slot 2: Occupied
Slot 3: Available
Slot 4: Occupied
Slot 5: Available
Total Available: 3
Entrance Distance: 12 cm
Car detected at entrance!
Slots Available – Opening Gate
 

🎓 Viva Explanation (Professional Answer)

“In this version, ESP32 connects to a home WiFi network in station mode. It runs a TCP server on port 8080. The mobile application connects using the ESP32’s IP address. The system reads IR sensors to detect parking slot availability and uses an ultrasonic sensor to detect vehicles at the entrance. Based on slot availability, it controls a servo motor to automate the gate and sends real-time parking status to the mobile application.”

Scroll to Top