NodeMCU ESP8266 IoT based LPG Gas Leakage Alarm

NodeMCU ESP8266 IoT Based LPG Gas Leakage Alarm Using Android App

Gas leakage, especially of Liquefied Petroleum Gas (LPG), poses a significant safety risk in households and industries. Traditional gas detectors work locally and may not alert you remotely. In this project, we’ll build an IoT-based LPG Gas Leakage Alarm System using the NodeMCU ESP8266, MQ5 gas sensor, LCD display, LED and buzzer for local alerts, and an Android app built with MIT App Inventor for remote control and real-time updates using MQTT protocol.

This system not only detects gas leakage but also notifies the user instantly and allows threshold value adjustments remotely via a mobile app.

Components Required

  1. NodeMCU ESP8266 – IoT development board with Wi-Fi.
  2. MQ5 Gas Sensor – Detects LPG and other combustible gases.
  3. 16×2 LCD Display (I2C module) – Displays gas concentration and alerts.
  4. Buzzer – Audible alarm when leakage is detected.
  5. LED – Visual alert for leakage.
  6. MIT App Inventor – To create the Android app.
  7. MQTT Broker (like HiveMQ or Mosquitto) – For communication.
  8. Jumper wires and breadboard – For connections.
  9. Power supply (5V) – To run the NodeMCU and peripherals.

Working Overview

  • MQ5 gas sensor detects LPG levels and sends analog data to the NodeMCU.
  • NodeMCU reads this data and:
    • Displays the gas level on the LCD screen.
    • Triggers buzzer and LED when the gas concentration exceeds a user-defined threshold.
    • Publishes data to an MQTT broker.
    • Subscribes to a threshold update topic from the Android App, allowing real-time updates.
  • The Android app allows the user to:
    • Monitor gas levels.
    • Update the gas concentration threshold value remotely.

NodeMCU ESP8266 IoT based LPG Gas Leakage Alarm

Circuit Diagram

Connections:

  • MQ5 Sensor:
    • VCC → 5V
    • GND → GND
    • AO (Analog Output) → A0 (NodeMCU)
  • LCD Display (I2C):
    • VCC → 5V
    • GND → GND
    • SDA → D2
    • SCL → D1
  • Buzzer:
    • +ve → D6
    • -ve → GND
  • LED:
    • Anode → D7(with 220-ohm resistor)
    • Cathode → GND

NodeMCU ESP8266 IoT based LPG Gas Leakage Alarm

Arduino Code

You can download the Arduino code from below link.

Libraries Needed:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

Let’s break down what each of these libraries does:

<ESP8266WiFi.h>

  • This library is essential to connect your NodeMCU ESP8266 to a Wi-Fi network.
  • It allows the board to act as a client or server on a wireless network.
  • You use it to connect to your home Wi-Fi using WiFi.begin(ssid, password)

<PubSubClient.h>

  • This library is used for MQTT communication.
  • It allows the NodeMCU to publish data (like gas sensor readings) and subscribe to topics (like receiving a new threshold from the app).
  • MQTT is a lightweight protocol ideal for IoT applications.

<Wire.h>

  • Required to use the I2C communication protocol.
  • It enables communication between the NodeMCU and I2C-compatible devices like the LCD display module.

<LiquidCrystal_I2C.h>

  • Specifically designed to work with 16×2 or 20×4 LCDs that use an I2C backpack.
  • It simplifies displaying text (e.g., gas levels, alerts) on the LCD with commands like lcd.print() and lcd.setCursor().

Global Variables:

Update the below variables with your wifi details along with MQTT server details.

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";

These are configuration settings that define how your NodeMCU connects to the internet and MQTT broker:

ssid

  • This is the name of your Wi-Fi network (Service Set Identifier).
  • Replace "YOUR_WIFI_SSID" with your actual Wi-Fi name.

password

  • This is your Wi-Fi password, required to connect your NodeMCU to the internet.
  • Ensure it’s entered exactly as it is (case-sensitive).

mqtt_server

  • This is the address of your MQTT broker.
  • You can uses "broker.hivemq.com", which is a free public MQTT broker useful for testing and prototyping.
  • You can replace this with your own broker (e.g., a local Mosquitto broker on a Raspberry Pi or another cloud MQTT server).

Android App with MIT App Inventor

We have created a complete app using MIT App Inventor. You can see the screenshot below.

NodeMCU ESP8266 IoT based LPG Gas Leakage Alarm

You will also get the push notifications as shown below

NodeMCU ESP8266 IoT based LPG Gas Leakage Alarm

If you need aia file of this project, then drop us an email on startwithiot@gmail.com

Features of the Android App

  • 🔢 Real-Time Gas Level Monitoring

    • Displays the current LPG gas concentration value (from MQ5 sensor) in real-time via MQTT.

  • 🛠️ Adjustable Threshold Value

    • Allows the user to input and update the gas threshold limit dynamically.

    • This value is sent to the NodeMCU over MQTT to adjust sensitivity on-the-fly.

  • 📡 MQTT Communication

    • Uses MQTT protocol to subscribe to gas level topic and publish threshold updates to the device.

  • 📤 Bi-Directional Communication

    • Receives sensor data and sends commands without needing USB or Bluetooth connection.

  • ⚠️ Leak Status Display

    • Displays a warning message or changes color (e.g., red text or background) if gas levels exceed the threshold.

  • 🟢 Clean & Simple UI

    • Built for easy use, even by non-technical users, with large input boxes and labeled buttons.

  • ✅ Connectivity Status Indicator

    • Shows MQTT connection status (Connected/Disconnected) to inform the user if the app is online.

  • 🔐 Secure Input

    • Ensures only valid numerical inputs are accepted for threshold values, preventing bugs or crashes.

MQTT Brokers

You can use public brokers like:

Final Prototype

NodeMCU ESP8266 IoT based LPG Gas Leakage Alarm

After completing the wiring, uploading the Arduino code, and setting up the Android app and MQTT broker, the final prototype brings together all components into a functional and responsive LPG leakage detection system.

The MQ5 gas sensor continuously monitors the surrounding air for LPG concentration. The real-time sensor values are displayed on the 16×2 I2C LCD screen, allowing for immediate local visibility. If the detected gas level exceeds the predefined threshold, the system instantly activates the buzzer and LED, providing audible and visual alerts to signal a potential leak.

Simultaneously, the current gas levels are published via MQTT to the cloud, making them accessible remotely through the Android app built using MIT App Inventor. The app not only displays these live readings but also allows the user to update the gas threshold value dynamically. This updated value is sent to the NodeMCU over MQTT and takes effect immediately—no need to reprogram the device.

The system is compact, low-cost, and easily scalable. It can be mounted in kitchens, basements, or any enclosed area where gas leakage is a concern. The final prototype successfully integrates hardware safety mechanisms with smart IoT features, making it an ideal project for home automation, academic demonstrations and personal safety.

Advantages of This System

  • Remote Monitoring: Check gas levels from anywhere.
  • Real-time Alerts: Buzzer, LED, and display alert instantly.
  • Customizable Threshold: User-defined limits via Android app.
  • MQTT Communication: Lightweight and fast IoT protocol.
  • Scalable: Can integrate SMS, email, or home automation in future.

Safety Precautions

  • Place the MQ5 sensor in areas where LPG is likely to leak.
  • Test the system with controlled gas release.
  • Use a protective enclosure for the electronics.
  • Do not use the system as a certified gas leak detector—it’s a DIY prototype.

FAQs

1. Can I use MQ2 or MQ6 instead of MQ5?

Yes, MQ2 and MQ6 can also detect LPG, but MQ5 is more accurate for LPG and natural gases.

2. What is a safe threshold for LPG gas?

Typically, values above 400–500 (analogRead scale) on MQ5 indicate a potential leak. However, calibrate based on your environment.

3. What happens if Wi-Fi disconnects?

The system will still alert locally via buzzer and LED, but MQTT features will be disabled until reconnection.

4. Can I integrate SMS or Email alerts?

Yes. You can use services like IFTTT or Twilio via webhooks integrated in the NodeMCU code.

5. Why is MQTT preferred over HTTP?

MQTT is lightweight, reliable in unstable networks, and perfect for IoT devices with limited power and memory.

6. Can the LCD be omitted?

Yes, but it’s helpful for real-time local monitoring. If cost is a constraint, you can replace it with a basic OLED or skip it.

Conclusion

Building an IoT-based LPG Gas Leakage Alarm using NodeMCU ESP8266, MQ5 sensor, and MQTT adds both local safety and remote monitoring. The integration with an Android app makes this system smarter and more adaptable to various environments. It’s a great DIY project for students, hobbyists, and even small-scale industries aiming to enhance safety.

Leave a Comment