# Tutorial on how to build a smart gas alarm ## Richard Willhelm Oelschlager ro222gu This tutorial will cover the steps needed in order to create a gas alarm using a generic ESP32 board using micropython alongside a MQ-2 gas detector. Estimated time to complete this project is 5-10 hours. ## Objective I have chosen this project as a last resort based on the hardware available. I initially intended to build a smart home security system but this was impossible due to the series of unfortunate events that lead to two separate orders not arriving. This device will be a gas / smoke alarm that will send notifications. The idea is that depending on readings the user would be notified if there is either a gas leak or smoke in the kitchen. This device is different in comparison to regular gas alarms which normally detect CO2 OR CH4 not both and not at the same time. ## Material | Product | Description| Price | | -------- | -------- | -------- | | ![](https://i.imgur.com/wD5W9at.png)| This kit contains one breadboard and all necessary cables to connect the sensors and a power module.|Price: 60SEK| | ![](https://i.imgur.com/7ZTpdwe.png)| Extra breadboard to allow placing the ESP 32 on the board | Price: 12SEK | | ![](https://i.imgur.com/dBCbRuE.png)| ESP Wroom 32 development board | Price: 100SEK | | ![](https://i.imgur.com/5ieZHhi.png)| MQ2 gas sensor | Price: 48SEK | Purchased from emag.ro ## Computer setup Using a Windows machine following steps are necessary: The IDE of choice is Atom. ([installation guide](https://hackmd.io/@lnu-iot/SydH7MTcw)) Flashing the firmware with Mircropython is also necessary. ([pycom](https://hackmd.io/@lnu-iot/SJ91R_jSO), [heltec](https://hackmd.io/@lnu-iot/By5ZUqvOq), [generic](https://hackmd.io/@lnu-iot/SyZ2diUOq)) After installing Atom the code can be uploaded to the board using the highlighted button or keyboard command ![](https://i.imgur.com/XXGklgt.png) ## Putting everything together Assembly will vary depending on your board. I reccomend consulting the pin sheet of your board. It is essential that the **plus pin** on the MQ2 and the **5v** pin on your board be connected to the **red(+) bus** and the **minus pin** on the MQ2 and the **ground pin** on your board to the **blue(-)** bus on your breadboard. **Getting this wrong could result in damage to parts.** The analogue pin of the MQ2 can then be connected any ADC(analogue to digital) pin. [More details here.](https://create.arduino.cc/projecthub/Junezriyaz/how-to-connect-mq2-gas-sensor-to-arduino-f6a456) ## Platform The platform to be used for handling data is datacake. Datacake is a multi-purpose, low-code IoT platform that requires no programming skills and minimal time to create custom IoT applications that can be brought into a white label IoT solution at the push of a button. https://datacake.co/ first 2 devices free. [Datacake tutorial ](https://hackmd.io/@lnu-iot/B1GUJqJq9) ## The code [MQ2 sensor library.](https://github.com/kartun83/micropython-MQ) This library is required for the gas sensor to funtion properly. The relevant files from the repository should be cloned to the lib folder as depicted in the following image. ![](https://i.imgur.com/oHERMgd.png) This library has two measuring modes, namely fast and accurate. The fast mode makes readings for all gases at the same time and the accurate one reads each gas for which the `read()` function is called separately, resulting in slower readings. If you only want to make the alarm trigger for one single gas, the fast method is appropiate. If you plan to read two different gases like I did, CO2 and CH4, then I would reccoment the accurate method, since otherwise changes in CO2 could trigger the CH4 alarm. The measuring mode can be set in the mq2.py file as can be observed below. ![](https://i.imgur.com/QU0EJ6q.png) ## Transmitting data/connectivity Data is transmitted to datacake every 30 seconds using MQTT. MQTT is a lightweight, publish-subscribe, machine to machine network protocol. It is designed for connections with remote locations that have devices with resource constraints or limited network bandwidth. Locally the device is connected via WiFi. ## Presenting the data ![](https://i.imgur.com/DCj7HgC.png) The dashboard is built using datacake. Datacake supports simple drag and drop dashboard functionality using MQTT integration for data retrieval. Data is refreshed every 30 seconds. In datacake if you click on rules, you can set up automation with conditions based on data. I have therefore set it up so that upon reaching a dangerous level of CO2 an email is sent. ![](https://i.imgur.com/vIcj4jd.png) ## Finalizing the design I originally wanted to build a smart home security system. After electro kit canceled the order I immediately contacted a TA and placed a new order for a kit from Amazon DE. They gave me a delivery window of 20 June - 5 July. On 21 June the order was updated to delivery on 5 July. I then decided to cancel the order and again contacted a TA and we spent a couple of hours looking for local variants in Romania. Bucharest has no esp32 for sale, so I had to order from pretty much the only seller that had all the parts for a basic gas alarm. These parts arrived on Friday. To my surprise, after I got the parts I discovered that the manufacturer of the board I got has no website and there is no pin sheet anywhere to be found on the internet. Data is therefore only simulated.