© Guillermo Guillen 2019
G. GuillenSensor Projects with Raspberry Pihttps://doi.org/10.1007/978-1-4842-5299-4_3

3. Gas Leak Alarm

Guillermo Guillen1 
(1)
Ciudad de Mexico, Mexico
 

In this chapter, you’ll develop a system for the detection of fires or gas leaks with the Raspberry Pi board and the MQ2 gas sensor. This system will send the captured data to the remote ThingSpeak server and, when a gas increase is detected beyond a programed limit, send a message to your Twitter account. In this project, you'll use an analog port and the IoT ThingSpeak server.

I have developed a gas leak alarm with a microcontroller before but I have never monitored the values registered in ThingSpeak and sent an alert to Twitter when the values were above a certain level, so this is a good experience. You can check this data anywhere you are without the need to directly observe the MQ2 gas sensor.

Hardware

Let’s begin by making the electrical connections shown in Figure 3-1. The following electronic components are shown in Figure 3-1:
  • Raspberry Pi Zero W

  • ADS1115

  • MQ2 gas sensor

  • Resistor 10k

../images/486908_1_En_3_Chapter/486908_1_En_3_Fig1_HTML.jpg
Figure 3-1

Electrical diagram

Use the ADS1115 digital-analog converter to interface the MQ2 gas sensor with the Raspberry Pi board. To do so, you must install the library of this device and you must activate the 12C interface. You can download the library from the link at the end of this tutorial.

You can install from pip with
$ sudo pip install adafruit-ads1x15
Remember that Unix language knowledge is required. Alternatively, to install the library from source (recommended), run the following commands on the Raspberry Pi system:
$ sudo apt-get install git build-essential python-dev
$ cd ~
$ git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git
$ cd Adafruit_Python_ADS1x15
$ sudo python setup.py install
You must activate the 12C interface on the Raspberry Pi Configuration screen and reboot the system to work correctly, as shown in Figure 3-2.
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig2_HTML.jpg
Figure 3-2

Activation of the 12C interface

The ADS1115 digital-analog converter can be configured for 16 and 15 bits. In this case, you are going to use the 16 bits and for this the following instruction is configured in code:
GAIN = 1

In Figure 3-3, you can see that the MQ2 gas sensor detects the presence of fire and LP gas, and then the Raspberry Pi board connects to the remote ThingSpeak server to capture them. The ThingSpeak server, in turn, sends an alert to your Twitter account.

Here’s a snapshot of how it works:
  1. 1.

    The gas leak is detected by the MQ2 gas sensor.

     
  2. 2.

    This sensor is analog and its output has to pass through the analog-to digital-converter, ADS1115, which is set to 16 bits; that is, it samples from 0 to 32768 quantization values.

     
  3. 3.

    The ADS1115 device connects to the SCL and SDA pins on the Raspberry Pi board.

     
  4. 4.

    The Raspberry Pi board connects via your WiFi to the remote ThingSpeak server and sends the captured data of your analog port A0.

     
  5. 5.

    If the captured data exceeds 12000 units, then the ThingSpeak server sends an alert.

     
  6. 6.

    The alert is sent via a message to your Twitter account. You can see this message either on your PC or on your cell phone or smart phone.

     
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig3_HTML.png
Figure 3-3

Gas leak schematic diagram

The project looks like Figures 3-4 and 3-5.
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig4_HTML.jpg
Figure 3-4

Complete system in operation

../images/486908_1_En_3_Chapter/486908_1_En_3_Fig5_HTML.jpg
Figure 3-5

MQ2 gas sensor

Now that you have seen how to connect the hardware of your system, the next step is to program your Raspberry Pi board. If you have any questions regarding the hardware pins used, please consult the appendix.

Software

The complete source code and libraries can be found at https://drive.google.com/file/d/11iq8Mjo4z_QZhoj2HpTqQlfg4GwoIt6n/view?usp=sharing.

Comments have been added here and are marked by #. They don’t need to be reproduced when entering the code.
//*******************************
# Import required Python libraries
import sys
import os
import time
from time import sleep
import urllib2
import Adafruit_ADS1x15
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
#Setup our API and delay
myAPI = "OICJL711JK2CON1H"  # API Key from thingSpeak.com channel
myDelay = 15 #how many seconds between posting data
def getSensorData():
    MQ2 = 0
    print('Reading ADS1x15 values, press Ctrl-C to quit...')
    # Print nice channel column headers.
    print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
    print('-' * 37)
    try:
        # Main loop until users quits with CTRL-C
        while True :
            # Read all the ADC channel values in a list.
            values = [0]*4
            for i in range(4):
                    # Read the specified ADC channel using the previously set gain value.
                    values[i] = adc.read_adc(i, gain=GAIN)
                    MQ2 = values[0]
            # Print the ADC values.
            print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
            return (str(MQ2))
    except KeyboardInterrupt:
        print "  Quit"
def main():
    print 'starting...'
    baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
    print baseURL
    while True:
        try:
            print "Reading Sensor Data now"
            MQ2 = getSensorData()
            print MQ2 + " "
            f = urllib2.urlopen(baseURL + "&field1=%s" % (MQ2))
            print f.read()
            f.close()
            sleep(int(myDelay))
        except Exception as e:
            print e
            print 'exiting.'
            break
if __name__ == '__main__':
    main()
//******************************

Note that you must replace the write API key on line 22 (shown in bold) with the one you created for the ThingSpeak server. Follow the steps provided in Chapter 2 to generate the code. You can also find good examples in the IC ADS115 library to understand how it works.

To use the tools of the IoT service provider, you must follow the guidelines indicated.

Procedure

Now it’s time to fill out the reports.

Installing the ADS1115 Sensor Library

The ADS1115 library is attached at the end of this tutorial and is called Adafruit_Python_ADS1x15-master.zip.

Unzip the package. To execute the installation, type the following in the command line:
>>> sudo python setup.py install
In the library you can find simple examples to test the ADS1115 sensor (Figure 3-6).
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig6_HTML.jpg
Figure 3-6

ADS1115 board

Enabling the 12C Interface

You must activate the 12C interface on the Raspberry Pi Configuration screen and reboot the system to work correctly. Go to Menu ➤ Preferences ➤ Raspberry Pi Configuration.

Creating a Project in ThingSpeak

Use the ThingSpeak account you created in Chapter 2 and create a new channel with the following data (Figure 3-7):
  • Name: Gas Leak Alarm

  • Description: Alarm System with MQ2 gas sensor

  • Field1: MQ2

../images/486908_1_En_3_Chapter/486908_1_En_3_Fig7_HTML.jpg
Figure 3-7

Creating a new channel

Save the project. The system will provide you with the channel number and the Write API key (Figure 3-8).

Copy the Write API key, which is 16 characters, and paste it into the Python code where it says
myAPI = "GET_YOUR_KEY"
Remember that in the free account in ThingSpeak you can only monitor a maximum of four channels simultaneously.
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig8_HTML.jpg
Figure 3-8

Getting the Write API key

Figures 3-9 and 3-10 show how the system works. In Figure 3-9, the sensor measures the amount of gas in the environment and sends the data to the IoT server. In Figure 3-10, the IoT server plots the data it is receiving on a channel.
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig9_HTML.jpg
Figure 3-9

Data sent by the Raspberry Pi board to the ThingSpeak server

../images/486908_1_En_3_Chapter/486908_1_En_3_Fig10_HTML.jpg
Figure 3-10

Graph in the ThingSpeak server

Using Your Twitter Account in ThingSpeak

To use your Twitter account in ThingSpeak, follow the instructions provided in Chapter 2.

Sending an Alert to Your Twitter Account

Click Apps and then click React. Create a new React and fill in the data shown in Figure 3-11.
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig11_HTML.jpg
Figure 3-11

Fill out this react

At the end, you will have the data shown in Figure 3-12.
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig12_HTML.jpg
Figure 3-12

Finishing the react

You can see an example of a tweet alert in Figure 3-13.
../images/486908_1_En_3_Chapter/486908_1_En_3_Fig13_HTML.jpg
Figure 3-13

Alert sent by the ThingSpeak server to my Twitter account

Challenges

Using analog ports, experiment with a TMP36 temperature sensor or LDR sensor.

Conclusion

In this chapter, you made use of the analog ports of the ADS1115 integrated circuit to send analog MQ2 sensor data to the Raspberry Pi board. This board sent the data to the ThingSpeak IoT server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.236.174