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

🧠Smart Dustbin (WiFi Serial Terminal – Hotspot Mode)


🎯 Objective of This Lesson

Students will learn:

  • How ESP32 creates its own WiFi hotspot

  • How to connect mobile to ESP32 directly

  • How to monitor sensor data wirelessly

  • How to build offline IoT systems


📱 Required Mobile App

Install any:

  • TCP/UDP Terminal App

  • WiFi Serial Monitor App

  • Serial WiFi Terminal


📡 How This Works

  1. ESP32 creates WiFi hotspot

  2. Mobile connects to ESP32 WiFi

  3. App connects using IP address

  4. Data sent using TCP server

No router required.


🔌 Hardware Connections (Same as Before)

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 (Hotspot Mode – Copy & Paste)

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

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

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.softAP(ssid, password);
server.begin();

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

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 Library

 
#include <WiFi.h>
 

This library allows ESP32 to use WiFi features.


2️⃣ Creating Access Point

 
WiFi.softAP(ssid, password);
 

This line creates a WiFi hotspot named:

Smart_Dustbin

Password: 12345678

Your phone connects directly to ESP32.


3️⃣ Starting TCP Server

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

This creates a server on Port 80.

Port 80 is standard HTTP/TCP port.


4️⃣ Getting IP Address

 
WiFi.softAPIP();
 

Usually IP will be:

192.168.4.1

Students must enter this IP in WiFi Terminal app.


5️⃣ Checking Client Connection

 
WiFiClient client = server.available();
 

If mobile app connects → client becomes active.

Then data is sent using:

 
client.println();
 

6️⃣ Hand Detection Logic

If distance < 20 cm:

  • Lid opens

  • Message sent to WiFi app


7️⃣ Garbage Level Detection

If garbage distance < 8 cm:

WiFi app shows:

Status: Dustbin is FULL

Otherwise:

Status: Dustbin is NOT Full


📱 How To Connect Mobile (Hotspot Mode)

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

IP → 192.168.4.1
Port → 80

Step 8 – Connect

Now you will see live dustbin data.


📊 Example WiFi Terminal Output

 
Garbage Distance: 24 cm
Status: Dustbin is NOT Full
———————-
Garbage Distance: 5 cm
Status: Dustbin is FULL
———————-
 

🎓 Learning Outcomes

we learn:

  • What is Access Point mode

  • TCP communication

  • IP Address concept

  • Client–Server model

  • Offline IoT system

Scroll to Top