#include <SoftwareSerial.h>
SoftwareSerial gsm(2,3); //RX, TX
namespace GSM{
void SendMessage(char NUM[],char SMS[]="ALERT. GAS LEAK")
{
Serial.println("Setting the GSM in text mode");
gsm.print("\n");
gsm.print("AT+CMGF=1\r");
// delay(2000);
String NUMBER="AT+CMGS=\"NUMBER\"\r";
NUMBER.replace("NUMBER",NUM);
gsm.print("\n");
gsm.print(NUMBER);
// Replace x with mobile number
// delay(2000);
gsm.print("\n");
gsm.print(SMS); // SMS Text
// delay(200);
gsm.print(">"); // ASCII code of CTRL+Z
// delay(2000);
}
};
class COMMS{
public:
int on=11;
int warn=10;
int relay=12;
int buzzer=9;
COMMS(){
pinMode(on, OUTPUT);
pinMode(warn, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void ALERT(){
digitalWrite(relay, HIGH);
digitalWrite(warn, HIGH);
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer,LOW);
digitalWrite(warn, LOW);
delay(500);
// gsm.print("w");
// Serial.println("WARNING");
}
};
// instantiate your class for external communication
COMMS comm;
int MQ6 =A0;
int DATA;
int threshold =200;
void setup() {
pinMode(MQ6, INPUT);
digitalWrite(comm.on, HIGH);
Serial.begin(9600);
gsm.begin(9600);
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
// read data, from sensor
DATA=analogRead(MQ6);
// Serial.println(DATA);
if(DATA>=threshold){
BEGINNING:
GSM::SendMessage("+263784370495");
int start=millis()/1000;
//repeat until data is below threshold
while(true){
int stop=millis()/1000;
int current_time=stop-start;
comm.ALERT();
DATA=analogRead(MQ6);
// Serial.println(DATA);
Serial.print("Time elapsed since SMS in seconds:");
Serial.print(current_time);
Serial.println("");
if(current_time>=60){
goto BEGINNING;
}
if(DATA<threshold){
digitalWrite(comm.relay, LOW);
// delay(1000);
stop=0;
break;
}
else{
delay(1000);
continue;
}
}
}
}