Header Ads

IOT Based LPG/CNG Gas Leakage Detection & Alert Using Arduino UNO, ESP8266, THINGSPEAK

Safety for all must be insured in today’s world and it is necessary that efficient and proactive safety systems should be implemented in public places and households. This project modifies the existing safety model installed in industries and this system also be used in homes and offices. The main objective of the work is designing micro-controller based toxic gas detecting and alerting system. The hazardous gases like LPG and propane were sensed . If the hazardous gases exceed the normal level then an alarm is triggered immediately at the incident place and also an alert message (i.e. Push Notification) is sent to the authorized person through the INTERNET with the help of used ESP8266 module incorporating Things Speak CloudThe advantage of this automated detection and alerting system over the manual method is that it provide real time response and accurate detection of an emergency and in turn leading faster control over the critical situation.

How does it work : 

Hardware's Required:

1. Arduino Uno  => BUY
2. ESP8266 Wifi Module => BUY
3. Jumper Wires => BUY
4. LED => BUY
5. Buzzer => BUY

Steps for Creating Project:

1. Hardware Connection
2. Configuring ThingSpeak Cloud for uploading Sensor Values
3. Make changes to Code & Upload  
4. Posting to Pushbullet via ThingSpeak for getting Push Notification on Android

Step 1 : Hardware Connection - 

Circuit Diagram:

Step 2 : Configuring Thing Speak Cloud -

Source: www.thingspeak.com


Visit www.thingspeak.com and click on the "Get Started" button to make a free account.

Now click on the "New Channel" Give it a name and select two fields. If you want you can fill more info about your channel, e.g. Latitude and Longitude of your sensor. If you want to share sensor info with your friends (and more..) click on the "Make Public?" check box option. Finally click on the "Save Channel" button.

press new channel and select the API keys tab (third image above). Copy your "Write API Key" , you will need it in the next step.

Step 3 : The Code 

#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
float sensor=A0;
float gas_value;
String apiKey = "5G0NEIEWV7FQWERTY"; // replace with your channel's thingspeak WRITE API key
String ssid="kavin"; // Wifi network SSID
String password ="12345612"; // Wifi network password
boolean DEBUG=true;
#define VOLTAGE_MAX 5.0
#define VOLTAGE_MAXCOUNTS 1023.0
//======================================================================== showResponse
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
if (DEBUG) Serial.print(c);
//=======================connection to thinkspeak.com=================================================
boolean thingSpeakWrite(float value1){
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
cmd += ""; // api.thingspeak.com
cmd += "\",80";
if (DEBUG) Serial.println(cmd);
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);
//getStr +="&field2=";
//getStr += String(value2);
// ...
getStr += "\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
if (DEBUG) Serial.println(cmd);
if (DEBUG) Serial.print(getStr);
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
return true;
//================================================================================ setup
void setup() {
DEBUG=true; // enable debug serial
//----------my sensor code-----------
espSerial.begin(115200); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again
//espSerial.println("AT+RST"); // Enable this line to reset the module;
//espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
if (DEBUG) Serial.println("Setup completed");
// ======================================================================= loop
void loop() {
//---------my gas sensor value reading-------------------
delay(1); //delay in between reads for stability
// initialize digital pin LED_BUILTIN as an output.
digitalWrite(LED_BUILTIN, HIGH);
// digitalWrite(LED_BUILTIN, LOW);
float t = gas_value*(VOLTAGE_MAX / VOLTAGE_MAXCOUNTS); //convert gas value to voltages
if (isnan(t)) {
if (DEBUG) Serial.println("Failed to read from MQ5");
if (DEBUG) Serial.println("Voltage="+String(t));
thingSpeakWrite(t); // Write values to thingspeak
// thingspeak needs 15 sec delay between updates, */
digitalWrite(LED_BUILTIN, LOW);

Just edit these lines with your information in the code & upload it to Arduino :

String apiKey = "5G0NEIEWV7FQWERTY"; -ThingSpeak key 
String ssid="kavin"; - Wifi Hotspot Name
String password ="12345612"; - Wfi Password

Once the code editing is done upload it & Use a Gas/Cigarette lighter near sensor in gas leak condition you will see a change in sensor value in serial monitor and also able to see push API key data update in monitor. 

Serial Monitor Output When Gas Leakage Detected:


Monitor Thing Speak Channel Output:

Our code is written in a way that if there is gas leakage then a sensor value will be pushed to ThingSpeak Channel.


Were do you start?

Step 1: setup the PushBullet service (free)
Pushbullet is an internet service which for SMS sending, notification management and file sending between your mobile devices and pc..”
First you need to go to PushBullet and make a new free account. Activate your account, login in the website and you will see the next screen:

Select and press mobile OS type ex android or IOS or go in the apps market / Google play in case of android and search for "PushBullet" and setup the application on your mobile device with the help of account you’ve just created. After that you should see your mobile smartphone in the Devices menu.

Go to Settings menu of the app where you have create a new Access Token by clicking Create Access Token button.

Get the access token and save it to a notepad text file.

Step 2: Setup the ThingSpeak Http, React App:

Step by step guide on configuring ThingSpeak to auto post to PushBullet, I use ESP8266’s but the you can use this for anything posting to ThingSpeak. I’ve been messing with the ESP8266 chips, a really cheap Wifi SoC that can be used with Arduino, but also with the right firmware can be used on their own without a master control processor. 

So the point of this step is to figure out how to send information to PushBullet via ThingSpeak. This is really a part two to an article I haven’t written yet, but as the hackanons article has already described how to program an arduino & 8266 and post to ThingSpeak I will expect you to have done that and we continue from there.

I have created a channel called MQ5 SENSOR VALUE. It doesn’t really do anything except take a “Field1” input, which I only ever send a value ofsensor. Because In my instance I only ever log something to ThingSpeak if the sensor detectssome Gas Leakage.
Of course on your project you can use any existing channel and check any field for a value in that to trigger a notification.

Next we need to create the ThingSpeak App based on “ThingHTTP”. Click on Apps in the
Menu bar and then select 'ThingHTTP'

Now Click New ThingHTTP button & Fill Out the Form.

Name:Request 49499

HTTP Auth Password:sdfgh
Content Type:application/json
HTTP Version:1.1
Name: Authorization
Value: Bearer [PushBulletAPI] 
Body:{"type": "note", "title": "IOT SOS ALERT", "body": "LPG/CNG Gas Leakage"}
Parse String:
Created:2017-04-27 7:35 am
So now we can log into ThingSpeak and we have something that can call into PushBullet which will send a notification to an Android OR iPhone.

Lastly we need to tie these things together and we do this also inside ThingSpeak for that we need to use React App in ThingSpeak.

 Now Click New React  button & Fill Out the Form.

Here we are testing on every insertion string on channel MQ5 SENSOR VALUE, Field 1 in which the condition is - if inserted value is not equal to 0

If this condition is true we call into the ThingHttp by setting the Action to ThingHttp. 
Save it.
That’s it.
Now whenever your IoT device sends a piece of data into ThingSpeak, ThingSpeak will automatically relay a post to PushBullet and in turn notify your devices. 


  1. I am using a Mq 2 sensor. I am getting an approximate 1000 gas value in normal and about 60 when gas is detected. What changes should i make

  2. Voltage=1.28

    It has not been updated. Is there a workaround?

    1. I am also facing the same :(

    2. What should we do.. I am also facing the same..

  3. i want to know that this sensor detect the LPG gas or the fire flame

    1. this sensor detects LPG gas and smoke also in some amount

  4. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. bellfires

  5. can i use the same code for mq135 if there must be any changes please specify me i need it for y project

    1. No there will no change for any MQ series sensor


  6. I want to change ESP card with GSM or SIM arduino how!


  7. I want to change ESP card with GSM or SIM arduino how!

  8. Voltage=1.28

    It has not been updated. Is there a workaround?

  9. i am not able to view anything in the serial monitor once the code is dumped

  10. In this project ,do the header part is compulsory?and what will be the authorization and value of the header?my project is not working. and what will be this following part?

  11. if you want to avoid tragic incidents like this then you should fit gas leak detector.

  12. The code compiles succesfully and works perfectly fine, but i am unable to get notification on my mobile through Pushbullet. What to do? What is that Http authorization password in Thingshttp

    1. Same problem btw not sending data to thinkspeak cloud

  13. hello guys.my field chart 1 does not display data.what should i do

  14. why message cannot receive?

  15. After reading this post I have gained some additional knowledge on Leakage Detection. Thanks for sharing such an informative post. Keep sharing! Best Gas Company

  16. Gas Leak detector is the effective one to avoid hazard situation in workplace or home. The gas leak detector you shared here was simply amazing. Thanks.
    Gas Leak Detector

  17. There are a few intriguing points in time in this post but I do not determine if I see these people center to heart. There is some validity but I will take hold opinion until I investigate it further. Excellent article , thanks and we want more! Added onto FeedBurner too Gas Safe Engineer in Croydon

  18. I have bookmarked your blog, the articles are way better than other similar blogs.. thanks for a great blog! 12V Electric Car Jack and Wrench for Smart Travel

  19. Is it possible to make this device only by using ESP 32 DOIT DEVKIT V1 ?? instead of using both Arduino and ESP8266.. IF YES THEN PLEASE SHARE THE CODE WITH US

  20. I read your blog and i found it very interesting and useful blog for me. as we provide lpg price comparison UK at affordable prices. for more info visit our website.

  21. important of these electronic devices is fully automatic washing machines, and therefore this advanced technology needs to تصليح غسالات

  22. I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! midstream Energy

  23. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. uno4d

  24. Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. gilgameshindustrial


Powered by Blogger.