Header Ads

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

Safety plays a major role in today’s world and it is necessary that good safety systems are to be implemented in places of education and work. 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 microcontroller based toxic gas detecting and alerting system. The hazardous gases like LPG and propane were sensed . If these gases exceed the normal level then an alarm is generated immediately and also an alert message (Push Notification) is sent to the authorized person through the INTERNET and used ESP8266 module, Things Speak Cloud. The advantage of this automated detection and alerting system over the manual method is that it offers quick response time and accurate detection of an emergency and in turn leading faster diffusion of 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 -



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.

Click on your 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 cigar 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: 

Click on you mobile OS type (Android or IOS) or go in the apps market / Google play and search for PushBullet and setup the application on your mobile device using the same account you’ve just created. After that you should see your mobile smartphone in the Devices menu.

Go to Settings menu and create a new Access Token by pressing 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 of sensor. Because In my instance I only ever log something to ThingSpeak if the sensor detects some 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?


Powered by Blogger.