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

📘 Gas Leakage Detection System

🎯 Objective of This Lesson

Students will learn:

  • How ESP32 creates its own WiFi network

  • What is Access Point (AP Mode)

  • How to connect mobile to ESP32 hotspot

  • How to monitor gas level using WiFi Serial Terminal app

No router required.
No internet required.


🔌 Hardware Reminder

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

📡 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️⃣ Gas readings are sent using TCP server


🧾 Complete ESP32 Code (WiFi Hotspot Mode – Copy & Paste)

#include <WiFi.h>

#define MQ3_PIN 34

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

WiFiServer server(80);

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

void setup() {

Serial.begin(115200);

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

 
#include <WiFi.h>
 

Allows ESP32 to use WiFi features.


2️⃣ Create Hotspot

 
WiFi.softAP(ssid, password);
 

ESP32 becomes a WiFi Access Point.

Network Name:
Gas_Detector_AP

Password:
12345678


3️⃣ Start TCP Server

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

Creates server on Port 80.

Port 80 is standard TCP port.


4️⃣ Get IP Address

 
WiFi.softAPIP();
 

Usually ESP32 hotspot IP is:

192.168.4.1

Students must enter this IP in WiFi Serial Terminal app.


5️⃣ Detect Client Connection

 
WiFiClient client = server.available();
 

If mobile connects, client becomes active.

Then we send data using:

 
client.println();
 

6️⃣ Read Gas Sensor

 
gasValue = analogRead(MQ3_PIN);
 

Reads analog value from MQ3.

Range:

0 – 4095


7️⃣ Gas Detection Logic

 
if (gasValue > threshold)
 

If gas level exceeds threshold:

Display:

⚠ GAS LEAK DETECTED!

Otherwise:

Environment Safe


📱 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 – Open phone WiFi
Step 5 – Connect to Gas_Detector_AP
Step 6 – Open WiFi Serial Terminal app
Step 7 – Enter:

IP → 192.168.4.1
Port → 80

Step 8 – Connect

Now you will see live gas readings.


📊 Example WiFi Terminal Output

Gas Value: 420
Environment Safe

Gas Value: 1650
GAS LEAK DETECTED!


🎓 Learning Outcomes

Students now understand:

  • Access Point mode (AP Mode)

  • TCP communication

  • IP addressing

  • Wireless monitoring without internet

  • Real IoT communication basics



Scroll to Top