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

📘Gas Leakage Detection System

🎯 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 monitor gas readings using WiFi Serial Terminal app

  • Client–Server communication over local network


🔌 Hardware Reminder

MQ3 Pin ESP32 Pin
VCC 5V
GND GND
AO GPIO 34

No extra components.


🌐 How This System Works

1️⃣ ESP32 connects to home WiFi
2️⃣ Router assigns IP address (e.g., 192.168.1.105)
3️⃣ Mobile connects to same WiFi
4️⃣ WiFi Serial Terminal app connects using ESP32 IP & Port
5️⃣ Gas readings are received wirelessly


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

⚠ Replace WiFi credentials before uploading.

#include <WiFi.h>

#define MQ3_PIN 34

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

WiFiServer server(80);

int gasValue = 0;
int threshold = 1200; // Adjust after calibration

void setup() {

Serial.begin(115200);

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 sensor...");
delay(30000);
Serial.println("Sensor Ready!");
}

void loop() {

WiFiClient client = server.available();

if (client) {

gasValue = analogRead(MQ3_PIN);

client.print("Gas Value: ");
client.println(gasValue);

if (gasValue > threshold) {
client.println("⚠ GAS LEAK DETECTED!");
} else {
client.println("Environment Safe");
}

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

delay(1000);
}
}

🧠 Code Explanation (Step-by-Step)


1️⃣ Connect to WiFi (Station Mode)

 
WiFi.begin(ssid, password);
 

ESP32 behaves like a mobile phone connecting to your router.

This mode is called:

👉 STA Mode (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.

Port 80 is standard communication port.


5️⃣ Detect Client Connection

 
WiFiClient client = server.available();
 

If mobile connects → client becomes active.

Then we send data using:

 
client.println();
 

6️⃣ Gas Detection Logic

 
gasValue = analogRead(MQ3_PIN);
 

Reads analog value (0–4095).

If value > threshold:

⚠ GAS LEAK DETECTED!

Else:

Environment Safe


📱 How to Connect (WiFi Serial Terminal App)

Step 1 – Replace WiFi name and 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 you will see live gas readings.


📊 Example WiFi Terminal Output

Gas Value: 390
Environment Safe

Gas Value: 1700
GAS LEAK DETECTED!


🔎 Difference: Hotspot vs Router Mode

Feature Hotspot Mode Router Mode
Internet Required No Yes (Router needed)
IP Address 192.168.4.1 192.168.1.x
Multiple Devices Limited Yes
Range Short Router Coverage

🎓 Learning Outcomes

Students now understand:

  • Station Mode (STA Mode)

  • Router-based IoT communication

  • Local network concept

  • IP addressing

  • TCP server communication

Scroll to Top