Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » Gas Leakage Detector with Email Alert Notification using ESP32
ESP32 Projects IoT Projects

Gas Leakage Detector with Email Alert Notification using ESP32

Mamtaz AlamBy Mamtaz AlamUpdated:November 27, 20231 Comment5 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
Gas Leakage Detector Email Alert Notification ESP32
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview: Gas Leakage Detector with Email Alert Notification using ESP32

In this project, we will learn how to make ESP32 Based Gas Leakage Detector with Email Alert Notification System. To sense the LPG Gas in Air, we will use the MQ2/MQ5 Gas Sensor. The MQ5 Gas Sensor is suitable for detecting H2, LPG, CH4, CO & Alcohol. Because of its greater Sensitivity & fast Response, safety Alert can be sent quickly. Earlier we made Gas Leak Alarm System using the MQ2 Sensor.

We will use the ESP32 Mail Client library & Send Email to any Email Address using an SMTP Server. The Email is sent automatically when any Gas/LPG Leakage is detected above certain Threshold Value. Thus this ESP32 Email Notifier system Alert the users to shut down the valve or take any action immediately. The system can be activated or deactivated through the Local Web Server. If we choose to deactivate, we won’t receive email notifications when the gas level is exceeded. We can use this System as a Safety System in Industry or at home.


Bill of Materials

Following are the components required for making this project. All the components can be easily purchased from Amazon. The component purchase link is given below.

S.N.Components NameQuantityPurchase Links
1ESP32 Board1Amazon | AliExpress
2MQ2/MQ5 Gas Sensor1Amazon | AliExpress
35V Micro USB Data Cable1Amazon | AliExpress
5Breadboard1Amazon | AliExpress
4Connecting Jumper WiresAmazon | AliExpress



MQ5 Gas Sensor

The most popular Analog Gas Sensor used is MQ5 Gas Sensor. The MQ5 gas sensor detects the presence of various gases such as hydrogen, carbon monoxide, methane and LPG. The sensor interacts with a gas to measure its concentration ranging from 100ppm to 3,000ppm.

MQ5 Gas Sensor

When a gas interacts with this sensor, it is first ionized into its constituents and is then adsorbed by the sensing element. This adsorption creates a potential difference in the element which is conveyed to the controller unit through an output analog pin in the form of current. Thus we are measuring the Analog current as an output which increases on increasing Gas Level.

The Sensor works on 3V-5V & has both the Analog & Digital output. It has 4 Pins as VCC, GND, D0 & A0. To learn more about this Sensor check its datasheet here: MQ5 Datasheet.

You can check some of the previous project made using this Sensor:
1.Gas Level Monitoring Over Internet Using ESP8266 & Gas Sensor

2.Smoke Level Detector using MQ-135 Sensor & Arduino with Alarm

3.Gas Leakage Detector using GSM & Arduino with SMS Alert



Hardware Circuit Assembly

We can make a small device that can be operated on Battery. In my case, I used Breadboard to Assemble and test the device. The Circuit Diagram for “ESP32 Gas Leakage Detector with Email Alert Notification” is given below.

ESP32 Gas Sensor

You can connect Sensor VCC Pin to 3.3V/5V of ESP32 Board. We are not using the digital Pin, so we connect the Sensor Analog Pin to GPIO35 of ESP32 Board.

ESP32 Gas Leakage Detector


Setting Up Sender Email Address

Create a New Gmail Account from here: https://www.google.com/gmail/about/. Its better not to use the Personal Gmail Account for this purpose.

esp32 send email

Now you need to allow less secure apps to get access to this new Gmail account by ESP32. If you don’t allow this option, you might not be able to set the ESP32 Email Alert part. Go to this link & turn on the “allow less secure” option.

Note: Turn OFF two-step Verification to access this.




Installing the Required Libraries

1. ESP32 Mail Client Library

To send emails with the ESP32, we’ll use the ESP32 Mail Client library. The library is developed by Mobizt and can be installed via Library Manager. You can download the library from here.

Download ESP32 Mail Client Library

2. Asynchronous Web Server Libraries

To build the asynchronous web server we need two libraries, i.e ESPAsyncWebServer library & Async TCP Library.

Download ESPAsyncWebServer Library & Download AsyncTCP Library


Source Code: ESP32 Gas Leakage Detector with Email Alert Notification

Now let us move to the programming Part of this Project. First add all the Libraries explained above.

Copy the code given below & before uploading it to the ESP32 Board, make some changes in the code explained below.

1
2
const char* ssid = "***********";        //SSID of Wifi network
const char* password = "***********";   // Password of wifi network

Make changes to Wifi SSID & Password from here.

1
2
#define emailSenderAccount    "[email protected]"    // Sender email address
#define emailSenderPassword   "***********"            // Sender email password

Change the Sender Email here and fill the Email Address & Password you created earlier.

1
String inputMessage = "[email protected]";   //Reciepent email alert.

Fill the Receiver Address here where you want to send the Alert Email.

1
2
// Default Threshold Value
String inputMessage3 = "40.0";                    // Default gas_value

Change the threshold value of the gas from here. When the gas level crosses this value, Email Alert is Send.

Thats all the whole changes you need to make in the code before uploading it to the ESP32 board. The full code is given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "ESP32_MailClient.h"
 
const char* ssid = "***********";        //SSID of Wifi network
const char* password = "***********";   // Password of wifi network
 
#define emailSenderAccount    "[email protected]"    // Sender email address
#define emailSenderPassword   "***********"            // Sender email password
#define smtpServer            "smtp.gmail.com"
#define smtpServerPort        465
#define emailSubject          "ALERT! Gas Leak Detected"   // Email subject
 
 
String inputMessage = "[email protected]";   //Reciepent email alert.
String enableEmailChecked = "checked";
String inputMessage2 = "true";
 
// Default Threshold Value
String inputMessage3 = "40.0";                    // Default gas_value
String lastgaslevel;
 
 
// HTML web page to handle 3 input fields (email_input, enable_email_input, threshold_input)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
  <title>Email Notification with Gas Level</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head><body>
  <h2>Gas Level Detection</h2>
  <h3>%GASVALUE%</h3>
  <h2>ESP Email Alert</h2>
  <form action="/get">
    Email Address <input type="email" name="email_input" value="%EMAIL_INPUT%" required><br>
    Enable Email Notification <input type="checkbox" name="enable_email_input" value="true" %ENABLE_EMAIL%><br>
    Gas Level Threshold <input type="number" step="0.1" name="threshold_input" value="%THRESHOLD%" required><br>
    <input type="submit" value="Submit">
  </form>
</body></html>)rawliteral";
 
void notFound(AsyncWebServerRequest *request)
{
  request->send(404, "text/plain", "Not found");
}
AsyncWebServer server(80);
 
String processor(const String& var)
{
  if(var == "GASVALUE")
  {
    return lastgaslevel;
  }
  else if(var == "EMAIL_INPUT")
  {
    return inputMessage;
  }
  else if(var == "ENABLE_EMAIL")
  {
    return enableEmailChecked;
  }
  else if(var == "THRESHOLD")
  {
    return inputMessage3;
  }
  return String();
}
 
 
// Flag variable to keep track if email notification was sent or not
bool emailSent = false;
const char* PARAM_INPUT_1 = "email_input";
const char* PARAM_INPUT_2 = "enable_email_input";
const char* PARAM_INPUT_3 = "threshold_input";
 
// Interval between sensor readings.
unsigned long previousMillis = 0;    
const long interval = 5000;    
 
SMTPData smtpData;
 
 
void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.println("WiFi Failed!");
    return;
  }
  Serial.println();
  Serial.print("ESP IP Address: http://");
  Serial.println(WiFi.localIP());
  
 
  // Send web page to client
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });
  // Receive an HTTP GET request at <ESP_IP>/get?email_input=<inputMessage>&enable_email_input=<inputMessage2>&threshold_input=<inputMessage3>
  server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
    // GET email_input value on <ESP_IP>/get?email_input=<inputMessage>
    if (request->hasParam(PARAM_INPUT_1)) {
      inputMessage = request->getParam(PARAM_INPUT_1)->value();
      // GET enable_email_input value on <ESP_IP>/get?enable_email_input=<inputMessage2>
      if (request->hasParam(PARAM_INPUT_2)) {
        inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
        enableEmailChecked = "checked";
      }
      else
      {
        inputMessage2 = "false";
        enableEmailChecked = "";
      }
      // GET threshold_input value on <ESP_IP>/get?threshold_input=<inputMessage3>
      if (request->hasParam(PARAM_INPUT_3)) {
        inputMessage3 = request->getParam(PARAM_INPUT_3)->value();
      }
    }
    else {
      inputMessage = "No message sent";
    }
    Serial.println(inputMessage);
    Serial.println(inputMessage2);
    Serial.println(inputMessage3);
    request->send(200, "text/html", "HTTP GET request sent to your ESP.<br><a href=\"/\">Return to Home Page</a>");
  });
  server.onNotFound(notFound);
  server.begin();
}
 
 
 
void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
 
    float gas_analog_value = analogRead(35);
    float gas_value = ((gas_analog_value/1023)*100);
    Serial.print(gas_analog_value);
    Serial.print(", ");
    Serial.println(gas_value);
    
    lastgaslevel = String(gas_value);
    
    // Check if gas_value is above threshold and if it needs to send the Email alert
    if(gas_value > inputMessage3.toFloat() && inputMessage2 == "true" && !emailSent){
      String emailMessage = String("Gas Level above threshold. Current Gas Level: ") +
                            String(gas_value));
      if(sendEmailNotification(emailMessage)) {
        Serial.println(emailMessage);
        emailSent = true;
      }
      else {
        Serial.println("Email failed to send");
      }    
    }
    // Check if gas_value is below threshold and if it needs to send the Email alert
    else if((gas_value < inputMessage3.toFloat()) && inputMessage2 == "true" && emailSent)
    {
      String emailMessage = String("Gas Level below threshold. Current gas_value: ") +
                            String(gas_value) + String(" C");
      if(sendEmailNotification(emailMessage))
      {
        Serial.println(emailMessage);
        emailSent = false;
      }
      else {
        Serial.println("Email failed to send");
      }
    }
  }
}
 
 
bool sendEmailNotification(String emailMessage)
{
  // Set the SMTP Server Email host, port, account and password
  smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
  
  smtpData.setSender("ESP32_Gas_Alert_Mail", emailSenderAccount);
  
  // Set Email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
  smtpData.setPriority("High");
  
  // Set the subject
  smtpData.setSubject(emailSubject);
  
  // Set the message with HTML format
  smtpData.setMessage(emailMessage, true);
  
  // Add recipients
  smtpData.addRecipient(inputMessage);
  smtpData.setSendCallback(sendCallback);
  
  if (!MailClient.sendMail(smtpData))
{
    Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
    return false;
  }
  smtpData.empty();
  return true;
}
 
 
void sendCallback(SendStatus msg)
{
  // Print the current status
  Serial.println(msg.info());
  
  // Do something when complete
  if (msg.success())
{
    Serial.println("----------------");
  }
}




ESP32 Gas Leakage Detector with Email Alert Notification

Once the code is uploaded to ESP32 Board, you can now open the Serial Monitor. The Serial Monitor will display the IP Address of the ESP32 Board. It will display all the process from start to the end including Mail Server getting Started to Sending Email if the threshold is crossed.

It will display the Gas Level along with the Analog value as well.

Now Copy the IP Address and paste it on any Web Browser like Chrome and then hit “Enter”. The Web Browser will display the Gas Value along with the Threshold Value Set.

You can enable or disable the Email Alert form here by simply ticking/unticking an option. Similarly, you can change the Email Address and also the Gas Threshold Value. All you need is put the value and Hit Send Button.

Now also open your Gmail Inbox, you can see Email Message Alert via ESP32 using the SMTP Mail Server.

ESP32 Email Notification


Video Tutorial & Demonstration

Gas Leakage Detector with Email Alert Notification using ESP32
Watch this video on YouTube.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleESP8266 to ESP8266 Communication : Ad hoc Networking
Next Article MicroPython: ESP32 with DHT11 Humidity Temperature Sensor

Related Posts

IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

Updated:May 10, 20261K
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

Updated:May 2, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

Updated:April 27, 20261K
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

Updated:April 27, 20262K
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

Updated:February 1, 20261K
View 1 Comment

1 Comment

  1. sekigwa on December 6, 2020 12:56 AM

    interesting project

    Reply

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • IoT Based PM & Air Quality Monitoring System using ESP32
    IoT Based PM & Air Quality Monitoring System using ESP32
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
    ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • L293D Dual H-Bridge Motor Driver IC Pins, Circuit, Working
    L293D Dual H-Bridge Motor Driver IC Pins, Circuit, Working
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.