A Professional-Grade, Enterprise-Ready Multi-Node Gas and Environmental Monitoring System
- Introduction
- Key Features
- System Architecture
- Hardware Requirements
- Pin Configuration
- Hardware Assembly
- Software Requirements
- Installation Guide
- ESP-NOW Protocol
- Code Structure
- Data Structure
- Sensor Calibration
- Deployment Guide
- Troubleshooting
- Safety Warnings
- Performance Metrics
- Advanced Configuration
- FAQ
- Future Roadmap
- Contributing
- License
- Acknowledgments
This document provides a comprehensive guide to building an IoT-based Multi-Gas Detection System. It is intended for both beginners and experienced developers who are interested in creating a production-ready monitoring system using ESP32 and ESP8266 microcontrollers.
- ESP-NOW Communication: Direct device-to-device communication without the need for a WiFi router.
- Real-Time Monitoring: Low-latency data transmission for critical safety alerts.
- Scalable Architecture: Easily expand the network from a single node to over twenty nodes.
- Low Power Consumption: Optimized for battery-powered deployments.
- Production Ready: Suitable for real-world industrial applications.
- Beginner Friendly: Step-by-step instructions for a seamless setup process.
This project enables the creation of a distributed gas and environmental monitoring network. It allows for the continuous monitoring of various gases, such as Carbon Monoxide (CO), Methane (CH4), and LPG, making it suitable for a wide range of applications.
| Application | Use Case |
|---|---|
| Home Safety | Detect gas leaks from stoves, heaters, or LPG cylinders. |
| Industrial Monitoring | Monitor air quality in factories, warehouses, or chemical plants. |
| Laboratory Safety | Detect harmful gas emissions during experiments. |
| Parking Garages | Monitor CO levels from vehicle emissions. |
| Mining Operations | Detect methane and other dangerous gases in confined spaces. |
| Breweries/Distilleries | Monitor alcohol vapors and CO2 levels. |
| Battery Storage Rooms | Detect hydrogen gas from charging batteries. |
| Agricultural Storage | Monitor grain silos for combustible gas buildup. |
- Multi-Node Architecture: Supports up to 20 sensor nodes with plug-and-play connectivity and unique node identification.
- ESP-NOW Communication: Provides ultra-low latency (<10ms), extended range (200m+), and secure, energy-efficient data transmission.
- Comprehensive Gas Detection:
Gas Formula Danger Level Detection Range LPG C₃H₈ + C₄H₁₀ Explosive 200-10,000 ppm Carbon Monoxide CO Toxic 10-1,000 ppm Methane CH₄ Explosive 200-10,000 ppm Propane C₃H₈ Explosive 200-10,000 ppm Hydrogen H₂ Explosive 100-10,000 ppm Alcohol C₂H₅OH Flammable 10-500 ppm Smoke Various Health Hazard Analog Signal Ammonia NH₃ Toxic 10-300 ppm Benzene C₆H₆ Carcinogenic 10-1,000 ppm Carbon Dioxide CO₂ Asphyxiant 350-5,000 ppm - Environmental Monitoring: Measures temperature, humidity, and provides a composite Air Quality Index.
- Smart Alerting System: Features a local buzzer, customizable thresholds, multi-level alerts, and real-time serial notifications.
- Developer-Friendly: Built with PlatformIO, featuring modular, well-documented code and ready-to-use example sketches.
┌─────────────────────────────────────────────────────────────────────┐
│ │
│ CENTRAL HUB (ESP32) │
│ │
│ ┌────────────────────────────────────────────────────────────┐ │
│ │ • Receives data from all nodes via ESP-NOW │ │
│ │ • Processes and displays sensor readings │ │
│ │ • Identifies nodes by unique nodeID │ │
│ │ • Can trigger actions based on received data │ │
│ │ • Serial output for monitoring/logging │ │
│ └────────────────────────────────────────────────────────────┘ │
│ │
└──────────────────────────┬──────────────────────────────────────────┘
│
│ ESP-NOW Protocol
│ (2.4 GHz Direct Communication)
│
┌─────────────────┼─────────────────┐
│ │ │
│ │ │
┌────────▼────────┐ ┌──────▼──────┐ ┌───────▼────────┐
│ │ │ │ │ │
│ NODE 1 │ │ NODE 2 │ │ NODE 3 │
│ (ESP8266) │ │ (ESP8266) │ │ (ESP32) │
│ │ │ │ │ │
│ ┌─────────────┐ │ │ ┌─────────┐ │ │ ┌────────────┐ │
│ │ DHT22 │ │ │ │ MQ-9 │ │ │ │ MQ-4 │ │
│ │ (Temp/Hum) │ │ │ │ (CO) │ │ │ │ (CH4) │ │
│ └─────────────┘ │ │ └─────────┘ │ │ └────────────┘ │
│ │ │ │ │ │
│ ┌─────────────┐ │ │ │ │ ┌────────────┐ │
│ │ MQ-2 │ │ │ │ │ │ MQ-7 │ │
│ │ (LPG/Smoke) │ │ │ │ │ │ (CO) │ │
│ └─────────────┘ │ │ │ │ └────────────┘ │
│ │ │ │ │ │
│ TX: Every 10s │ │ TX: Every │ │ ┌────────────┐ │
│ │ │ 10s │ │ │ MQ-135 │ │
└─────────────────┘ └─────────────┘ │ │ (Air Qual) │ │
│ └────────────┘ │
│ │
│ ┌────────────┐ │
│ │ BUZZER │ │
│ │ (Alert) │ │
│ └────────────┘ │
│ │
│ TX: Every 12s │
└────────────────┘
┌────────────┐ ┌────────────┐ ┌──────────────┐
│ Sensor │ Reads │ ESP Board │ Sends │ Central Hub │
│ (MQ/DHT) ├────────►│ (Node) ├────────►│ (Receiver) │
└────────────┘ └────────────┘ └──────────────┘
│ │
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ struct_msg │ │ Parse Data │
│ populated │ │ Display Info │
└──────────────┘ └──────────────┘
Every node sends data in this standardized C++ format:
typedef struct struct_message {
int nodeID; // Unique identifier (1, 2, or 3)
float temperature; // Temperature in Celsius
float humidity; // Relative humidity percentage
int mq2_lpg; // MQ-2 LPG reading (analog)
int mq2_smoke; // MQ-2 Smoke reading (analog)
int mq9_co; // MQ-9 CO reading (analog)
int mq4_ch4; // MQ-4 Methane reading (analog)
int mq7_co; // MQ-7 CO reading (analog)
int mq135_air_quality; // MQ-135 Air quality reading (analog)
} struct_message;| Component | Quantity | Purpose | Estimated Cost (USD) |
|---|---|---|---|
| ESP32 Dev Board | 2 | Hub + Node 3 | $15 - $20 |
| ESP8266 NodeMCU | 2 | Node 1 + Node 2 | $8 - $12 |
| DHT22 Sensor | 1 | Temperature & Humidity | $5 - $8 |
| MQ-2 Sensor | 1 | LPG & Smoke Detection | $2 - $4 |
| MQ-4 Sensor | 1 | Methane Detection | $2 - $4 |
| MQ-7 Sensor | 1 | CO Detection | $3 - $5 |
| MQ-9 Sensor | 1 | CO & Combustibles | $3 - $5 |
| MQ-135 Sensor | 1 | Air Quality | $3 - $5 |
| Buzzer (Active) | 1 | Audio Alerts | $1 - $2 |
| Breadboards | 3-4 | Prototyping | $5 - $10 |
| Jumper Wires | 40+ | Connections | $3 - $5 |
| USB Cables | 4 | Programming/Power | $5 - $10 |
| Power Supply (Optional) | 4 | Standalone Operation | $10 - $20 |
| TOTAL | - | - | $65 - $110 |
| Item | Specifications | Quantity | Notes |
|---|---|---|---|
| ESP32 DevKit V1 | 32-bit dual-core, 4MB Flash, WiFi, Bluetooth | 1 | Any ESP32 variant works |
| USB Micro Cable | Data + Power capable | 1 | Not charge-only |
| Breadboard | 830 tie-points | 1 | Optional for prototyping |
| Item | Specifications | Quantity | Notes |
|---|---|---|---|
| ESP8266 NodeMCU | CP2102 USB, 4MB Flash, 11 GPIO | 1 | V2 or V3 recommended |
| DHT22 (AM2302) | Temp/Humidity Sensor | 1 | DHT11 also works |
| MQ-2 Gas Sensor | Detects LPG, Propane, H2, Smoke | 1 | Requires 24-48hr preheat |
| 10kΩ Resistor | 1/4W tolerance | 1 | Pull-up for DHT22 |
| Item | Specifications | Quantity | Notes |
|---|---|---|---|
| ESP8266 NodeMCU | Same as Node 1 | 1 | - |
| MQ-9 Gas Sensor | Detects CO, CH4, LPG | 1 | More sensitive to CO |
| Item | Specifications | Quantity | Notes |
|---|---|---|---|
| ESP32 DevKit | Same as Hub | 1 | Needs more analog pins |
| MQ-4 Gas Sensor | Detects CH4, Natural Gas | 1 | Best for methane |
| MQ-7 Gas Sensor | Detects CO | 1 | Requires heating cycle |
| MQ-135 Sensor | Detects NH3, NOx, Benzene, CO2 | 1 | General air quality |
| Active Buzzer | 5V operation | 1 | For audio alerts |
| Component | Component Pin | ESP8266 Pin | GPIO Number | Notes |
|---|---|---|---|---|
| DHT22 | VCC | 3V3 | - | 3.3V power |
| DHT22 | DATA | D4 | GPIO2 | Digital I/O with 10kΩ pull-up |
| DHT22 | GND | GND | - | Ground |
| MQ-2 | VCC | VIN | - | 5V power (critical!) |
| MQ-2 | GND | GND | - | Ground |
| MQ-2 | A0 | A0 | ADC0 | Analog reading (0-1024) |
| Component | Component Pin | ESP8266 Pin | GPIO Number | Notes |
|---|---|---|---|---|
| MQ-9 | VCC | VIN | - | 5V power required |
| MQ-9 | GND | GND | - | Ground |
| MQ-9 | A0 | A0 | ADC0 | Analog reading (0-1024) |
| Component | Component Pin | ESP32 Pin | GPIO Number | ADC Channel | Notes |
|---|---|---|---|---|---|
| MQ-4 | VCC | VIN | - | - | 5V power |
| MQ-4 | GND | GND | - | - | Ground |
| MQ-4 | A0 | D34 | GPIO34 | ADC1_CH6 | Analog input only |
| MQ-7 | VCC | VIN | - | - | 5V power |
| MQ-7 | GND | GND | - | - | Ground |
| MQ-7 | A0 | D35 | GPIO35 | ADC1_CH7 | Analog input only |
| MQ-135 | VCC | VIN | - | - | 5V power |
| MQ-135 | GND | GND | - | - | Ground |
| MQ-135 | A0 | D32 | GPIO32 | ADC1_CH4 | 12-bit ADC (0-4095) |
| Buzzer | + | D12 | GPIO12 | - | Digital output |
| Buzzer | - | GND | - | - | Ground |
Follow the detailed pin configurations above to assemble each node on a breadboard. Common Mistakes to Avoid:
- Powering MQ sensors with 3.3V instead of 5V (VIN pin).
- Using ADC2 pins on the ESP32 (conflicts with WiFi). Use ADC1 pins (GPIO32-39) only.
- Forgetting the 10kΩ pull-up resistor for the DHT22 sensor.
- Reversing polarity on the buzzer.
- Visual Studio Code: The primary IDE.
- PlatformIO IDE Extension: For project management and building.
- USB to UART Drivers:
All required libraries are managed automatically by the platformio.ini file.
- Install Environment: Install VS Code, the PlatformIO extension, and the necessary USB drivers.
- Download Project: Clone the repository using Git or download the ZIP file.
git clone https://github.com/projects-the-ai-developer/multigas-detection.app.git
- Open in PlatformIO: Open the project folder in VS Code with the PlatformIO extension.
- Configure & Upload:
- Upload the
UniversalESP-Now_Reciever.cppsketch to the Hub (ESP32) and get its MAC address from the Serial Monitor. - Paste the Hub's MAC address into the
hubMACarray in each of the Node sketches. - Upload the code to each respective node.
- Upload the
- System Testing: Power up all nodes and monitor the Hub's serial output to verify data reception.
ESP-NOW is a direct device-to-device communication protocol from Espressif. It is ideal for this project due to its low latency, low power consumption, and ability to work without a WiFi router. It uses the MAC address of the receiver to send data packets of up to 250 bytes.
multigas-detection.app/
│
├── .vscode/ # VS Code configuration
│ └── extensions.json
│
├── include/ # Header files (if any)
│ └── README
│
├── lib/ # Custom libraries (if any)
│ └── README
│
├── src/ # SOURCE CODE (Main folder)
│ ├── UniversalESP-Now_Reciever.cpp # Central Hub code
│ ├── Node1_ESP8266_MQ2_DHT22.cpp # Node 1 sender
│ ├── Node2_ESP8266_MQ9.cpp # Node 2 sender
│ └── Node3_ESP32_v0.1.cpp # Node 3 sender
│
├── test/ # Unit tests (future)
│ └── README
│
├── platformio.ini # PlatformIO configuration
├── README.md # This file
├── LICENSE # MIT License
└── .gitignore # Git ignore rules
The project is organized into a standard PlatformIO structure. All source code is located in the src/ directory, with separate files for the receiver hub and each sensor node. The platformio.ini file at the root configures the build environments and dependencies for both ESP32 and ESP8266 boards.
A universal struct_message is used across all nodes to ensure compatibility. Each node populates the fields relevant to its sensors and sends the entire 36-byte structure to the hub. The hub then identifies the sender by the nodeID field and processes the data accordingly.
Why it matters: Raw analog readings from MQ sensors are not meaningful without calibration. The process involves:
- Preheating: Powering the sensors for 24-48 hours to stabilize them.
- Baseline Recording: Measuring the sensor's resistance in clean air (R₀).
- Exposure Test: Measuring the resistance in the presence of a known gas concentration (Rs).
- Calculating Rs/R₀: Using this ratio and the sensor's datasheet to determine the gas concentration in Parts Per Million (PPM).
Once tested, you can move the nodes from breadboards to a more permanent solution like PCB boards and enclosures. Ensure a stable 5V power supply for each node for reliable, long-term operation.
- No data on Hub: Check MAC address, ensure all nodes are on the same WiFi channel, and verify power to all nodes.
- Incorrect sensor readings: Ensure sensors are preheated, check for correct voltage (5V for MQ sensors), and perform calibration.
- Upload fails: Check COM port, ensure correct board is selected in PlatformIO, and try a different USB cable.
- Gas is dangerous! Calibrate and test in a well-ventilated area.
- Do not rely on this project as your sole life-saving device. It is an educational tool.
- MQ sensors get hot during operation. Do not touch them.
- Latency: <10ms between node transmission and hub reception.
- Range: Up to 200m in open space, 20-50m indoors.
- Power Consumption: Varies by node, but significantly lower than traditional WiFi.
- Data Logging: Add an SD card module to the hub to log sensor data over time.
- Cloud Integration: Use the hub to forward data to an MQTT broker or a cloud platform like AWS IoT or Thingspeak.
- Local Display: Add an OLED display to the hub for a visual interface.
- Can I add more nodes? Yes, up to 20 per hub.
- Can I use different sensors? Yes, but you'll need to modify the code and data structure.
- Does it need internet? No, the core system is completely offline. Internet is only needed if you add cloud integration.
- Develop a custom PCB design.
- Create 3D printable enclosures.
- Add support for more sensor types.
- Implement a web server on the hub for local network monitoring.
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
This project is licensed under the MIT License. See the LICENSE file for details.
- Espressif for the powerful ESP32/ESP8266 platforms and ESP-NOW protocol.
- The PlatformIO team for their excellent development ecosystem.