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

🧠Smart Dustbin (WiFi – Home Router Mode)


🎯 Objective of This Lesson

Students will learn:

  • How ESP32 connects to Home WiFi

  • What is Station Mode (STA Mode)

  • How client-server communication works over router

  • How to monitor dustbin from any device connected to same WiFi


🌐 How This Version Works

  1. ESP32 connects to your Home WiFi

  2. Router assigns IP address

  3. Mobile/PC connects to same WiFi

  4. Open WiFi Serial Terminal

  5. Enter ESP32 IP address

  6. Monitor dustbin wirelessly

Now no direct hotspot required.


🔌 Hardware Connections (Same as Previous Lessons)

Ultrasonic 1 – Hand Detection

  • TRIG → GPIO 5

  • ECHO → GPIO 18

Ultrasonic 2 – Garbage Level

  • TRIG → GPIO 17

  • ECHO → GPIO 16

Servo

  • Signal → GPIO 13


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

#include <WiFi.h>
#include <ESP32Servo.h>

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

WiFiServer server(80);

// -------- Pin Definitions --------
#define TRIG1 5
#define ECHO1 18

#define TRIG2 17
#define ECHO2 16

#define SERVO_PIN 13

Servo myServo;

long duration;
float distanceHand;
float distanceGarbage;

// -------- Setup --------
void setup() {

Serial.begin(115200);

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

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();

pinMode(TRIG1, OUTPUT);
pinMode(ECHO1, INPUT);

pinMode(TRIG2, OUTPUT);
pinMode(ECHO2, INPUT);

myServo.attach(SERVO_PIN);
myServo.write(0); // Lid closed
}

// -------- Distance Function --------
float getDistance(int trigPin, int echoPin) {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

float distance = duration * 0.034 / 2;

return distance;
}

// -------- Main Loop --------
void loop() {

WiFiClient client = server.available();

if (client) {

distanceHand = getDistance(TRIG1, ECHO1);

if (distanceHand > 0 && distanceHand < 20) {
myServo.write(90);
delay(3000);
myServo.write(0);
client.println("Hand Detected - Lid Opened");
}

distanceGarbage = getDistance(TRIG2, ECHO2);

client.print("Garbage Distance: ");
client.print(distanceGarbage);
client.println(" cm");

if (distanceGarbage > 0 && distanceGarbage < 8) {
client.println("Status: Dustbin is FULL");
} else {
client.println("Status: Dustbin is NOT Full");
}

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

delay(1000);
}
}

🧠 Code Explanation (Step-by-Step)


1️⃣ WiFi Station Mode

 
WiFi.begin(ssid, password);
 

This connects ESP32 to your home router.

This mode is called:

👉 STA (Station Mode)

ESP32 behaves like a mobile phone connecting to WiFi.


2️⃣ Checking Connection

 
while (WiFi.status() != WL_CONNECTED)
 

This waits until WiFi is connected.

Once connected:

 
WiFi.localIP();
 

This shows the IP address assigned by router.

Example:

 
192.168.1.105
 

3️⃣ Starting Server

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

Creates a TCP server on port 80.


4️⃣ Client Connection

 
WiFiClient client = server.available();
 

If any mobile or PC connects → client becomes active.

Data is sent using:

 
client.println();
 

5️⃣ Hand Detection Logic

If distance < 20 cm:

  • Servo rotates to 90°

  • Lid opens

  • Closes after 3 seconds

  • Message sent to WiFi client


6️⃣ Garbage Detection Logic

If garbage distance < 8 cm:

 
Status: Dustbin is FULL
 

Else:

 
Status: Dustbin is NOT Full
 

📱 How To Connect (Home Router Mode)

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

IP → 192.168.1.105
Port → 80

Step 8 – Connect

Now you can monitor dustbin.


🔎 Difference: Hotspot vs Router Mode

Feature Hotspot Mode Router Mode
Internet required No Yes (router)
IP address 192.168.4.1 192.168.1.x
Range Limited Router coverage
Multiple devices Limited Yes

🎓 Learning Outcomes

We now understand:

  • Station Mode (STA)

  • Router-based communication

  • IP addressing

  • TCP server concept

  • Real IoT networking basics

Scroll to Top