#include <SoftwareSerial.
h>
SoftwareSerial sim800L(3, 2); // RX, TX
// Sensor and Relay pins
#define flame_sensor_pin 5
#define gas_sensor_pin A0
#define buzzer_pin 4
const int relay1 = 6;
const int relay2 = 7;
const int relay3 = 8;
const int relay4 = 9;
// Alarm receiver's phone number with country code
const String PHONE_1 = "+959796592860";
const String PHONE_2 = ""; //optional
const String PHONE_3 = ""; //optional
boolean fire_flag = false;
boolean gas_flag = false;
String textMessage;
void setup() {
Serial.begin(9600);
sim800L.begin(9600);
pinMode(flame_sensor_pin, INPUT);
pinMode(gas_sensor_pin, INPUT);
pinMode(buzzer_pin, OUTPUT);
digitalWrite(buzzer_pin, LOW);
// Initialize relays
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(relay1, HIGH); // Initially turn off all relays
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
Serial.println("Initializing...");
sim800L.println("AT+CMGF=1");
delay(1000);
sim800L.println("AT+CNMI=2,2,0,0,0");
delay(1000);
}
void loop() {
if (sim800L.available()) {
delay(100);
textMessage = sim800L.readString();
Serial.print(textMessage);
controlRelays(textMessage);
}
checkSensors();
}
void checkSensors() {
int flame_value = digitalRead(flame_sensor_pin);
int gas_value = analogRead(gas_sensor_pin);
if (flame_value == LOW || gas_value > 500) {
digitalWrite(buzzer_pin, HIGH);
} else {
digitalWrite(buzzer_pin, LOW);
}
if (flame_value == LOW && !fire_flag) {
fire_flag = true;
send_multi_sms("Fire Detected");
make_multi_call();
} else if (flame_value != LOW) {
fire_flag = false;
}
if (gas_value > 500 && !gas_flag) {
gas_flag = true;
send_multi_sms("Gas Leak Detected");
make_multi_call();
} else if (gas_value <= 500) {
gas_flag = false;
}
}
void controlRelays(String message) {
if (message.indexOf("RELAY1 ON") >= 0) { digitalWrite(relay1, LOW); }
if (message.indexOf("RELAY1 OFF") >= 0) { digitalWrite(relay1, HIGH); }
if (message.indexOf("RELAY2 ON") >= 0) { digitalWrite(relay2, LOW); }
if (message.indexOf("RELAY2 OFF") >= 0) { digitalWrite(relay2, HIGH); }
if (message.indexOf("RELAY3 ON") >= 0) { digitalWrite(relay3, LOW); }
if (message.indexOf("RELAY3 OFF") >= 0) { digitalWrite(relay3, HIGH); }
if (message.indexOf("RELAY4 ON") >= 0) { digitalWrite(relay4, LOW); }
if (message.indexOf("RELAY4 OFF") >= 0) { digitalWrite(relay4, HIGH); }
}
void send_multi_sms(String message) {
if (PHONE_1 != "") {
Serial.print("Phone 1: ");
send_sms(message, PHONE_1);
}
if (PHONE_2 != "") {
Serial.print("Phone 2: ");
send_sms(message, PHONE_2);
}
if (PHONE_3 != "") {
Serial.print("Phone 3: ");
send_sms(message, PHONE_3);
}
}
void make_multi_call() {
if (PHONE_1 != "") {
Serial.print("Phone 1: ");
make_call(PHONE_1);
}
if (PHONE_2 != "") {
Serial.print("Phone 2: ");
make_call(PHONE_2);
}
if (PHONE_3 != "") {
Serial.print("Phone 3: ");
make_call(PHONE_3);
}
}
void send_sms(String text, String phone) {
Serial.println("Sending SMS...");
sim800L.print("AT+CMGF=1\r");
delay(1000);
sim800L.print("AT+CMGS=\"" + phone + "\"\r");
delay(1000);
sim800L.print(text);
delay(100);
sim800L.write(0x1A); // ASCII code for Ctrl+Z
delay(5000);
}
void make_call(String phone) {
Serial.println("Making call...");
sim800L.println("ATD" + phone + ";");
delay(20000); // 20 sec delay
sim800L.println("ATH");
delay(1000); // 1 sec delay
}