A real-time LPG gas leak detection and alert system built with ESP32, Flask, and a web-based dashboard. The system monitors gas concentration levels, triggers physical safety mechanisms (servo valve shutoff, relay, buzzer), and sends email alerts when dangerous levels are detected.
- Features
- System Architecture
- Gas Level States
- Hardware Components
- Pin Configuration
- Software Setup
- Environment Variables
- How It Works
- API Endpoints
- Real-time gas monitoring with live dashboard updates every second
- Three-state detection system — Safe, Warning, and Danger
- Automatic gas valve shutoff via servo motor when danger is detected
- Buzzer alarm with mute control from the web dashboard
- Relay control for additional safety mechanisms
- Email alerts via Brevo (formerly Sendinblue) API on high gas levels
- Concentration history chart with real-time graph
- Responsive dark-themed dashboard built with Tailwind CSS
- Cloud deployment ready (designed for Render)
┌──────────────┐ HTTP POST /update ┌──────────────────┐
│ ESP32 + │ ──────────────────────────────► │ Flask Backend │
│ MQ Gas │ │ (backend.py) │
│ Sensor │ ◄────────────────────────────── │ │
│ │ JSON response (mute cmd) │ Brevo Email API │
└──────────────┘ └────────┬─────────┘
│ Servo (valve) │
│ Relay GET /data │
│ Buzzer ▼
┌──────────────────┐
│ Web Dashboard │
│ (index.html) │
│ Polls every 1s │
└──────────────────┘
The system classifies gas concentration into three states:
All systems nominal. No action required.
Elevated gas levels detected. Monitor closely.
Critical gas concentration! The system automatically:
- Activates the buzzer alarm
- Rotates the servo to shut off the gas valve
- Triggers the relay
- Sends an email alert
- Displays an alert banner on the dashboard
| Component | Description |
|---|---|
| ESP32 | Microcontroller with WiFi |
| MQ Gas Sensor | LPG/combustible gas detection |
| Servo Motor | Gas valve shutoff mechanism |
| Relay Module | Additional safety control (active low) |
| Buzzer | Audible alarm |
| Pin | Component |
|---|---|
| 34 | Gas Sensor (Analog) |
| 18 | Servo Motor |
| 27 | Relay |
| 26 | Buzzer |
- Python 3.x
- Arduino IDE (for ESP32 flashing)
- ESP32 board package installed in Arduino IDE
-
Clone the repository
git clone https://github.com/your-username/gas-safety-dashboard.git cd gas-safety-dashboard -
Install dependencies
pip install -r requirements.txt
-
Set environment variables (see Environment Variables)
-
Run the server
python backend.py
The server starts on
http://localhost:5000
- Open
Gas_Safety_esp32Code.inoin Arduino IDE - Update WiFi credentials:
const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD";
- Update the server URL to point to your deployed backend:
const char* serverName = "https://your-server-url.com/update";
- Flash the code to your ESP32
| Variable | Description |
|---|---|
BREVO_API_KEY |
API key for Brevo email service |
EMAIL_SENDER |
Sender email address for alerts |
EMAIL_RECEIVER |
Recipient email address for alerts |
PORT |
Server port (default: 5000) |
- The ESP32 reads the gas sensor value every 200ms
- If gas stays above 2000 ppm for 8 seconds, the system activates:
- Servo rotates to 90° (shuts gas valve)
- Relay turns ON
- Buzzer starts ringing
- Data is sent to the Flask backend every 5 seconds via HTTP POST
- The backend triggers an email alert via Brevo when gas exceeds the threshold
- The web dashboard polls the backend every 1 second and updates the UI
- If gas drops below 1800 ppm:
- Buzzer turns off after 5 seconds
- Full system resets after 60 seconds below threshold
- The buzzer can be muted remotely from the dashboard
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Serves the web dashboard |
| GET | /data |
Returns latest gas data as JSON |
| POST | /update |
Receives gas data from ESP32 |
| POST | /mute |
Sends mute command to ESP32 |
| GET | /force-email |
Triggers a test email alert |
| GET | /health |
Health check endpoint |
├── backend.py # Flask server & email logic
├── Gas_Safety_esp32Code.ino # ESP32 Arduino code
├── requirements.txt # Python dependencies
├── templates/
│ └── index.html # Web dashboard
├── static/
│ └── screen.png # Dashboard screenshot
└── Readme_images/
├── SAFE.png # Safe state screenshot
├── WARNING.png # Warning state screenshot
└── DANGER.png # Danger state screenshot



