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
Raspberry Pi Zero W
ADS1115
MQ2 gas sensor
Resistor 10k
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.
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.
- 1.
The gas leak is detected by the MQ2 gas sensor.
- 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.
The ADS1115 device connects to the SCL and SDA pins on the Raspberry Pi board.
- 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.
If the captured data exceeds 12000 units, then the ThingSpeak server sends an alert.
- 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.
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.
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.
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
Name: Gas Leak Alarm
Description: Alarm System with MQ2 gas sensor
Field1: MQ2
Save the project. The system will provide you with the channel number and the Write API key (Figure 3-8).
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
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.