Introduction: IoT Gas Sensor
I had been wanting to create a Gas Sensor that can detect the gas leakage in the house. The practical usage of this it to ensure you didn't leave the stove on with no fire, resulting gas poisoning. Another use can be to make sure that you didn't over cook or let your pan on the fire for too long, which resulting on charcoal food. The latter seems more difficult in practice, and need to have further thoughts into this. So I am reusing the similar concept to the IoT Temperature sensor, to build a data later on the webserver to avoid the hassle of opening ports on the router.
Step 1: The Concept
The idea is connect the sensor to the ESP8266 and monitor the quantity of the gas in the air. When the quantity of gas reach a certain threshold, this will trigger the alarm (Buzzer). The gas data also will be uploaded periodically to the cloud (webserver) which allows remote access and monitoring of the gas. If the data is captured in the database over the period, this can be plotted onto graph to show the trend.
Step 2: Materials Used
Here are the list of the materials used in this build:
- ESP8266 - This will be the brain that allows us to connect things to the internet
- Gas sensor MQ-5
The ESP8266 is a fabulous module that allows things to connect to the internet, the Gas sensor used MQ5 allows 2 modes of operation, Digital mode and Analog mode. It also allow us to adjust the gas sensitivity via the variable resistor on board of the sensor.
Step 3: Connection Diagram
We are connecting the Gas sensor MQ-5 to the Analog input (AD0) of the ESP8266 as shown in the diagram. The buzzer is connected to Pin GND and D3.
In this example we are using the analog output of the sensor which allow us to monitor much bigger range of gas. The Digital output of the sensor can also be used, but this have to be calibrated properly to ensure that it will gives the desired trigger when a certain composition of gas was detected.
The second picture shows the connection using the prototype board. We connected the sensor and the buzzer. ESP8266 is powered by 3.3 V. The board allowed USB connection which down convert the 5V to 3.3 V used by the board.
Once this is connected you can then connect the USB connection to PC or Mac to allow uploading the code via Arduino IDE. If you are not familiar with the Arduino IDE, you can check my other Instructables post that can help you to get started.
Step 4: Webserver Setup
Prerequisite: You are familiar with setting up a webserver, uploading files via ftp, creating virtual directories and server scripting. If you are not familiar, don't worry you can always get your geeky friend to help you out with this step.
Download the "IoTGasSensorWebserver.zip" file and extract this to the root of your webserver using your favourite ftp software, or into any virtual directories that you like. In this example I am assuming the webserver is "http://arduinotestbed.com"
The php script that the ESP8266 will call is called "gasdata_store.php". in this example we are assuming the full path to this file is "http://arduinotestbed.com/gasdata_store.php"
If you have uploaded the files correctly you can test that everything is working by pointing your web browser to the following link "http://arduinotestbed.com/GasData.php"
You should be presented the similar site to the picture above with the Gas data dial.
One more thing you will need to make sure is the file “gas.txt” needs to be writeable, so you need to set the permission of this file to “666” by using the following unix command:
chmod 666 gas.txt
This can also be done using your ftp software or the file manager in your webhosting.
This file is where the sensor data will be uploaded into by the ESP8266.
Step 5: The Code
Once you get it all setup you can open the Arduino IDE and download the sketch above. Extract the zip file, and you should have 2 files in total:
Put them all in the same folder and opne the "ESP8266GasSensor.ino" in the Arduino IDE, then make the small modification to the code to point to the correct webserver location shown in the picture above.
Also modify the following line to match the file in your webserver location.
String weburi = "/gasdata_store.php"
Then compiled the sketch by selecting the "tick" button at the top of Arduino IDE. If all goes well, your code should compile successfully.
The next step is to upload the code to the ESP8266, to do this you can click on the "=>" button on the Arduino interface, and this should load your code into the ESP8266. If all goes well you should have a working AP (Access point) from the ESP8266 the first time you run this. The AP’s name is called “ESP-GasSensor”.
Try to connect to this AP using your laptop or mobile phone, then find out what is the ip address that was assigned to you, this can be done using “ipconfig” command in windows or “ifconfig” command if you are in linux or mac. If you are using iPhone you can click on the “i” button next to the ESP-GasSensor that you are connected to. Open your web browser and point to the ESP-GasSensor Ip address, if you are assigned with 192.168.4.10 as your up, the ESP-GasSensor is have the ip of 192.168.4.1, so you can point your web browser to http://192.168.4.1 You should be presented with the settings page where you can enter your wifi configuration. Once you had enter your WiFi access point that connects to the internet, tick the “update Wifi Config” check box, and click on “update” to save the settings to the ESP8266.
The ESP8266 will now restart and trying to connect to your WiFi router. If all goes well you should see the gas data being updated to you webserver on a regular interval. In this example you can point your browser to "http://arduinotestbed.com/GasData.php"
Congratulations!! if you manage to reach this part. You should give yourself a pat on the back. Now you can tell your friends about the gas sensor that you have.
Step 6: What's Next
You might want to re-calibrate the sensor alarm to suit your need.
This is not just for show, it should trigger and alarm when the threshold of gas reach a certain level. Depends on the type of sensor that you are using you will need to calibrate this. So go get a lighter, and point the lighter towards the sensor, and without lighting the lighter, press the gas release button on the lighter, so the gas will flow to the sensor. This should trip the buzzer. If not then you need to check whether the reading goes up by looking at the webserver. If this is not working then you need to check the connection, the sensor and the buzzer. If all goes well, the buzzer should make a noise.
The threshold in the code is set to 100, you should be able to find it in the following section of the code:
double threshold = 100;
Feel free to change the threshold to higher or lower depends on your need.
I hope you like this project. If you do please drop me a line and vote for me in the IoT contest, and subscribe to my blog for more simple Arduino projects.
Some final thoughts, you can record the gas reading into a database using sqllite or something more powerful. This will allow you to plot the graph similar to the above. Not just to look neat, but also to help you calibrating the sensors. For example, if you wanted to put this to monitor the leakage of the gas on your stove you might want to leave it reading the measurement for a couple of days, and then download the reading to see what the patterns looks like for normal usage, and then you can set the trigger for the exceptions to the rule, when the reading is outside normal.
Step 7: Disclaimer
This project is intended for hobbyist, to allow people to learn about IoT and the practical usage of it. It is by no means to replace the commercial gas leakage sensor. I can not be hold responsible, if you set your home on fire or fainted due to gas intoxication by using this project to detect the gas leakage. There are many sensors out there and quality are varying depends on the cost. Before using this as something serious, through testing needs to be done, and calibrated to ensure safety and robustness of the build.
Participated in the