Oct 09
Hey guys, back with another little project that I’ve been fiddling with for the past week. With kids around, every parent is thinking how to make their home safer for the little ones and for everybody in general. One of the most dangerous thing in the house can be the stove and since we have a gas-powered one, I always wondered why there are no simple gas detectors that can be used around the stove, just to alert instantly that gas may be leaking.
Well, that was the moment when I decided to build one of my own. Having a Particle (Spark) Photon lying around, I decided to use that as a foundation for the project. I like the fact that they are very small and cheap, and also can be flashed over the Wi-Fi. Having that settled, I needed the gas sensor and some kind of alerting system.
For the gas sensor I went for an
MQ-4 gas sensor because it can detect methane (CH4) as well as natural gas (CNG).
I found out later that it’s quite sensitive to CO2 as well, which turned out to be very useful while testing the setup. If you’re planning to use one in your projects and never used one before, remember that the gas sensors have a burn-in time when they are powered. That is to say that the sensor needs time to warm up (anywhere between 2 and 10 minutes). This is because the actual sensor has a heating element that helps detect these gases and while that is cold, it will give erroneous readings.
Now on to the alerts. Half of the alerting system was simple, add a beeper next to the sensor which would work perfectly when somebody is around. However I still had to sort out the second half, for the cases when somebody was not in the immediate vicinity. For that I wanted to have text/SMS alerts to my phone and possibly to a secondary phone, for example my wife’s phone. Now I realized that I should probably get one of our neighborhood FD guys phone number in there too. :)
Knowing that the Photon is not the most robust controller in terms of notifications (I know there’s the IFTTT or other alerting systems) but I wanted something small, reliable and really not depending on various 3rd parties in the cloud. So I made a small web app on my local web server that would send me specific text message when a URL is hit. Many cell carriers offer now the possibility to send somebody an email to a specific email address, and that email will be turned into an SMS and sent to the phone, so that turned out to be a good enough solution for me.
So the final sequence would be the following: gas sensor detects gas, starts beeping and at the same time triggers a URL which in turn sends an email that gets transformed by the carrier into a text message on my phone. The code that runs this will actually send an alert every 30 seconds while the gas sensor is being triggered. This way I know when the gas has dispersed. Here’s the photon firmware code:
#include "HttpClient/HttpClient.h" TCPClient client; //add your server address here. In my case it was a LAN address similar to 192.168.1.1 char server[] = ""; //add your server port, usually if it's a standard http request the port is probably 80 int port = 80; //Initialize timer to 0 int AlertSentSecsAgo = 0; //This is used to publish Spark events char strTxt[40]; //Define the PIN for the gas sensor int GasSensor = A0; //Define the PIN for the buzzer int Beeper = D0; //Define how often to send text alerts int AlertFrequency = 30; void setup() { pinMode(Beeper, OUTPUT); pinMode(GasSensor, INPUT); } void loop() { //I had to fine tune this value at which the alert is triggered based on some trial tests if (analogRead(GasSensor) > 750) { //If we sent an alert more than 30 seconds ago, send another one if (AlertSentSecsAgo >= AlertFrequency) { //Send alert every 30 seconds sendAlert(); AlertSentSecsAgo = 0; sprintf(strTxt, "%u", analogRead(GasSensor)); //Publish an event to the spark dashboard with the current value of the sensor Spark.publish("Gas Sensor Triggered", strTxt); } digitalWrite(Beeper, HIGH); //Sound the buzzer delay(250); digitalWrite(Beeper, LOW); //Silence the buzzer delay(250); AlertSentSecsAgo++; } else { sprintf(strTxt, "%u", analogRead(GasSensor)); Spark.publish("Gas Sensor:", strTxt); //Make sure that the alert will be sent next time the sensor is triggered AlertSentSecsAgo = AlertFrequency + 1; delay(5000); //Wait 5 seconds before checking the sensor again } } void sendAlert() { if (client.connect(server, port)) { client.println("GET /<your address to the text web app or service here> HTTP/1.0"); client.print("Host: "); client.println(server); client.println("Accept: text/html, text/plain"); client.println(); client.flush(); } }
Finally, for the case I chose a wall surface-mount phone jack box. The size perfectly matched the components that I had.
List of parts used in this project:
- Particle Photon ($19 on Particle Store)
- MQ4 Gas Sensor ($5.50 on Amazon)
- 5V Buzzer ($3.99 on Amazon)
- Wall mounted phone jack box ($3.00 at Home Depot)
- Right-angle USB Mini B cable ($3.99 on Amazon)
- Jumper wires
Wiring:
Here are a few pictures of the finished project.
Comments