Stefan G.
Published

Home Improvement DIY: WiFi Gas Detector with Text Alerts

How to build a DIY Wi-Fi gas detector that sends SMS alerts using a Particle Photon, a gas sensor and a buzzer

BeginnerShowcase (no instructions)7,439
Home Improvement DIY: WiFi Gas Detector with Text Alerts

Things used in this project

Hardware components

Photon
Particle Photon
×1
MQ-4 Gas Sensor
×1
5V Buzzer
×1
Wall-mounted phone jack box
×1
Right-angle USB Mini B cable
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

Code

Plain text
#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();
     }
}

Photon Firmware Code

C/C++
Photon Firmware
#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();
     }
}

Credits

Stefan G.

Stefan G.

1 project • 5 followers

Comments