Skip to content

Real-time IoT dashboard for multi-node industrial gas detection with intelligent alerting and historical analytics.

Notifications You must be signed in to change notification settings

projects-the-ai-developer/multigas-detection.app

Repository files navigation

IoT Multi-Gas Detection System using ESP-NOW

A Professional-Grade, Enterprise-Ready Multi-Node Gas and Environmental Monitoring System

License PlatformIO ESP32 ESP8266

A Comprehensive Guide to Building a Gas Detection Network


Table of Contents


Introduction

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.

Key Features

  • 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.

What is This Project?

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.

Real-World 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.

Key Features

  • 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.

System Architecture

Network Topology

┌─────────────────────────────────────────────────────────────────────┐
│                                                                     │
│                        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  │
                                    └────────────────┘

Communication Flow

┌────────────┐         ┌────────────┐         ┌──────────────┐
│ Sensor     │ Reads   │ ESP Board  │ Sends   │ Central Hub  │
│ (MQ/DHT)   ├────────►│ (Node)     ├────────►│ (Receiver)   │
└────────────┘         └────────────┘         └──────────────┘
                             │                        │
                             │                        │
                             ▼                        ▼
                      ┌──────────────┐        ┌──────────────┐
                      │ struct_msg   │        │ Parse Data   │
                      │ populated    │        │ Display Info │
                      └──────────────┘        └──────────────┘

Data Packet Structure

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;

Hardware Requirements

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

Bill of Materials (BOM)

Central Hub Components

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

Node 1 Components (Environmental + LPG)

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

Node 2 Components (CO Monitor)

Item Specifications Quantity Notes
ESP8266 NodeMCU Same as Node 1 1 -
MQ-9 Gas Sensor Detects CO, CH4, LPG 1 More sensitive to CO

Node 3 Components (Multi-Gas Air Quality)

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

Pin Configuration

Node 1 (ESP8266)

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)

Node 2 (ESP8266)

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)

Node 3 (ESP32)

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

Hardware Assembly

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.

Software Requirements

  1. Visual Studio Code: The primary IDE.
  2. PlatformIO IDE Extension: For project management and building.
  3. USB to UART Drivers:

All required libraries are managed automatically by the platformio.ini file.


Installation Guide

  1. Install Environment: Install VS Code, the PlatformIO extension, and the necessary USB drivers.
  2. 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
  3. Open in PlatformIO: Open the project folder in VS Code with the PlatformIO extension.
  4. Configure & Upload:
    • Upload the UniversalESP-Now_Reciever.cpp sketch to the Hub (ESP32) and get its MAC address from the Serial Monitor.
    • Paste the Hub's MAC address into the hubMAC array in each of the Node sketches.
    • Upload the code to each respective node.
  5. System Testing: Power up all nodes and monitor the Hub's serial output to verify data reception.

ESP-NOW Protocol

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.


Code Structure

Project Directory Layout

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

Code File Breakdown

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.


Data Structure

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.


Sensor Calibration

Why it matters: Raw analog readings from MQ sensors are not meaningful without calibration. The process involves:

  1. Preheating: Powering the sensors for 24-48 hours to stabilize them.
  2. Baseline Recording: Measuring the sensor's resistance in clean air (R₀).
  3. Exposure Test: Measuring the resistance in the presence of a known gas concentration (Rs).
  4. Calculating Rs/R₀: Using this ratio and the sensor's datasheet to determine the gas concentration in Parts Per Million (PPM).

Deployment Guide

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.


Troubleshooting

  • 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.

Safety Warnings

  • 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.

Performance Metrics

  • 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.

Advanced Configuration

  • 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.

FAQ

  • 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.

Future Roadmap

  • 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.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your changes.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgments

  • Espressif for the powerful ESP32/ESP8266 platforms and ESP-NOW protocol.
  • The PlatformIO team for their excellent development ecosystem.

About

Real-time IoT dashboard for multi-node industrial gas detection with intelligent alerting and historical analytics.

Resources

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •