Home Security System with 3 Sensor and SMS based using Arduino
|| AT-Command ||
How it Works
1. Connect the Arduino with Peripherals needed.
2. Plug in the Power Source on the device.
5. First, Modem will initialize and it takes about 3 seconds
8. When one of the sensors detects something, suppose the gas sensor detects the gas, the LCD will
show the detected gas, the buzzer will sound and will automatically send the message to a certain
number that contains that gas detected.
|| AT-Command ||
It is a security system that uses SMS as control and feedback from the system. Using 3 sensors, a Fire Sensor will detect a fire or fire, a Gas Sensor that will detect a leaking gas or more precisely LPG on the stove, and a PIR sensor that serves to detect human movement. When one of the three sensors detects these things, it is automatically programmed to turn on the buzzer and send a text message to a specific number that contains something detected depending on what sensor it detects. In addition to receiving a feed back of the message, the system is also controlled using SMS to: turn on the system, turn off the system, and turn off the alarm when. Turning off the alarm is used to turn off the buzzer when the sensor is detecting something. An LED is used as an indicator of whether the security system is active or not. In addition, the system is also equipped with a 20x4 LCD that is connected using I2C module on arduino, each process process and some information will be displayed on the LCD.
Hardware Requirement
Block Diagram
Schematic
Arduino - Sensors Wiring
Arduino - Output Component Wiring
Arduino - I2C Wiring
Learn more about I2C connections --> Project V - 14
Arduino - SIM900A Wiring
Source Code
- Modem SMS Getway (SIM900A)
- Flame Sensor
- Gas Sensor (MQ2)
- PIR Sensor
- LED
- Buzzer
- PC / Laptop with Arduino IDE Installed
- Modul LCD 20x4
- I2C Module
- Modul Arduino UNO
- Power supply +5Volt
Block Diagram
Schematic
Arduino - Sensors Wiring
Arduino - Output Component Wiring
Arduino - I2C Wiring
Learn more about I2C connections --> Project V - 14
Arduino - SIM900A Wiring
Source Code
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4);
SoftwareSerial mySerial(9, 8);
boolean stringComplete = false;
String inputString = "";
String fromGSM = "";
String my_number = "082322514735";
int pir = 10; //sensor gerak/orang
int mq = 11; //sensor gas
int api = 12; //sensor api
int buzzer = 13; //alarm
int led = 7; //led
unsigned long millisAwal, timeBuzz;
char str, f;
int i, j;
byte keluar = 1;
boolean sensorON = false;
void setup()
{
lcd.begin();
lcd.setCursor(0, 0);
lcd.print("Initialization Modem");
delay(1000);
pinMode(pir, INPUT);
pinMode(mq, INPUT);
pinMode(api, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println("Serial Connected");
mySerial.begin(9600);
inputString.reserve(200);
fromGSM.reserve(200);
mySerial.print("ATE0");
mySerial.print("\r");
delay(300);
lcd.setCursor(0, 2);
lcd.print(" DONE ");
delay(1000);
lcd.clear();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Initial Sensor ");
delay(2000);
while (!digitalRead(mq));
lcd.setCursor(0, 2);
lcd.print(" DONE ");
delay(2000);
lcd.clear();
}
void sendSMS(String& phone_number, char message[] )
{
mySerial.print("AT+CMGF=1");
mySerial.print("\r");
delay(500);
mySerial.print("AT+CMGS=\"" + phone_number + "\"\r");
delay(700);
mySerial.print(message);
mySerial.write(0x1A);
delay(500);
}
void recSMS()
{
if (mySerial.available()) {
char inChar = mySerial.read();
if (inChar == '\n') {
//check the state
if (fromGSM == "OK\r") {
Serial.println("AT Command, Done !");
}
else if (fromGSM == "ON\r") {
Serial.println("sensorOn = true");
sensorON = true;
digitalWrite(led, HIGH);
}
else if (fromGSM == "OFF\r") {
Serial.println("sensorOff = false");
sensorON = false;
digitalWrite(buzzer, LOW);
digitalWrite(led, LOW);
}
else if (fromGSM == "ALARMOFF\r") {
Serial.println("sensorOff = false");
keluar = 0;
digitalWrite(buzzer, LOW);
}
else if (fromGSM == "ERROR\r") {
Serial.println("GSM Error");
}
// Serial.println(fromGSM);
fromGSM = "";
} else {
fromGSM += inChar;
}
delay(20);
}
if (stringComplete) {
mySerial.print(inputString);
inputString = "";
stringComplete = false;
}
}
void loop ()
{
lcd.setCursor(0, 0);
lcd.print("Home Security System");
lcd.setCursor(0, 1);
lcd.print(" SMS Based");
lcd.setCursor(0, 3);
bacaSensor();
recSMS();
if (sensorON == false) {
lcd.setCursor(0, 2);
lcd.print(" Sensor OFF ");
lcd.setCursor(0, 3);
lcd.print(" nonactive ");
}
else {
lcd.setCursor(0, 2);
lcd.print(" Sensor ON ");
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
}
void bacaSensor() {
//----------------------------------baca sensor gerak
if (digitalRead(pir) == HIGH && sensorON == true) {
digitalWrite(buzzer, HIGH);
lcd.setCursor(0, 2);
lcd.print(" There is Movement");
lcd.setCursor(0, 3);
lcd.print("Conditions of Danger");
char myMessage[] = "There is Movement";
sendSMS(my_number, myMessage);
millisAwal = millis();
do {
timeBuzz = millis() - millisAwal;
recSMS();
//-------waktu lama buzzer ON 5 menit / 300000 mili detik
if (timeBuzz > 300000) {
keluar = 0;
}
}
while (keluar);
keluar = 1;
digitalWrite(buzzer, LOW);
lcd.clear();
}
else if (sensorON == true) {
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
//----------------------------------baca sensor asap
if (digitalRead(mq) == LOW && sensorON == true) {
digitalWrite(buzzer, HIGH);
lcd.setCursor(0, 2);
lcd.print(" There is Gas");
lcd.setCursor(0, 3);
lcd.print("Conditions of Danger");
char myMessage[] = "There is Gas";
sendSMS(my_number, myMessage);
millisAwal = millis();
do {
timeBuzz = millis() - millisAwal;
recSMS();
//-------waktu lama buzzer ON 5 menit / 300000 mili detik
if (timeBuzz > 300000) {
keluar = 0;
}
}
while (keluar);
keluar = 1;
digitalWrite(buzzer, LOW);
lcd.clear();
}
else if (sensorON == true) {
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
//----------------------------------baca sensor api
if (digitalRead(api) == LOW && sensorON == true) {
digitalWrite(buzzer, HIGH);
lcd.setCursor(0, 2);
lcd.print(" There is Fire");
lcd.setCursor(0, 3);
lcd.print("Conditions of Danger");
char myMessage[] = "There is Fire";
sendSMS(my_number, myMessage);
millisAwal = millis();
do {
timeBuzz = millis() - millisAwal;
recSMS();
//-------waktu lama buzzer ON 5 menit / 300000 mili detik
if (timeBuzz > 300000) {
keluar = 0;
}
}
while (keluar);
keluar = 1;
digitalWrite(buzzer, LOW);
lcd.clear();
}
else if (sensorON == true) {
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4);
SoftwareSerial mySerial(9, 8);
boolean stringComplete = false;
String inputString = "";
String fromGSM = "";
String my_number = "082322514735";
int pir = 10; //sensor gerak/orang
int mq = 11; //sensor gas
int api = 12; //sensor api
int buzzer = 13; //alarm
int led = 7; //led
unsigned long millisAwal, timeBuzz;
char str, f;
int i, j;
byte keluar = 1;
boolean sensorON = false;
void setup()
{
lcd.begin();
lcd.setCursor(0, 0);
lcd.print("Initialization Modem");
delay(1000);
pinMode(pir, INPUT);
pinMode(mq, INPUT);
pinMode(api, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println("Serial Connected");
mySerial.begin(9600);
inputString.reserve(200);
fromGSM.reserve(200);
mySerial.print("ATE0");
mySerial.print("\r");
delay(300);
lcd.setCursor(0, 2);
lcd.print(" DONE ");
delay(1000);
lcd.clear();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Initial Sensor ");
delay(2000);
while (!digitalRead(mq));
lcd.setCursor(0, 2);
lcd.print(" DONE ");
delay(2000);
lcd.clear();
}
void sendSMS(String& phone_number, char message[] )
{
mySerial.print("AT+CMGF=1");
mySerial.print("\r");
delay(500);
mySerial.print("AT+CMGS=\"" + phone_number + "\"\r");
delay(700);
mySerial.print(message);
mySerial.write(0x1A);
delay(500);
}
void recSMS()
{
if (mySerial.available()) {
char inChar = mySerial.read();
if (inChar == '\n') {
//check the state
if (fromGSM == "OK\r") {
Serial.println("AT Command, Done !");
}
else if (fromGSM == "ON\r") {
Serial.println("sensorOn = true");
sensorON = true;
digitalWrite(led, HIGH);
}
else if (fromGSM == "OFF\r") {
Serial.println("sensorOff = false");
sensorON = false;
digitalWrite(buzzer, LOW);
digitalWrite(led, LOW);
}
else if (fromGSM == "ALARMOFF\r") {
Serial.println("sensorOff = false");
keluar = 0;
digitalWrite(buzzer, LOW);
}
else if (fromGSM == "ERROR\r") {
Serial.println("GSM Error");
}
// Serial.println(fromGSM);
fromGSM = "";
} else {
fromGSM += inChar;
}
delay(20);
}
if (stringComplete) {
mySerial.print(inputString);
inputString = "";
stringComplete = false;
}
}
void loop ()
{
lcd.setCursor(0, 0);
lcd.print("Home Security System");
lcd.setCursor(0, 1);
lcd.print(" SMS Based");
lcd.setCursor(0, 3);
bacaSensor();
recSMS();
if (sensorON == false) {
lcd.setCursor(0, 2);
lcd.print(" Sensor OFF ");
lcd.setCursor(0, 3);
lcd.print(" nonactive ");
}
else {
lcd.setCursor(0, 2);
lcd.print(" Sensor ON ");
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
}
void bacaSensor() {
//----------------------------------baca sensor gerak
if (digitalRead(pir) == HIGH && sensorON == true) {
digitalWrite(buzzer, HIGH);
lcd.setCursor(0, 2);
lcd.print(" There is Movement");
lcd.setCursor(0, 3);
lcd.print("Conditions of Danger");
char myMessage[] = "There is Movement";
sendSMS(my_number, myMessage);
millisAwal = millis();
do {
timeBuzz = millis() - millisAwal;
recSMS();
//-------waktu lama buzzer ON 5 menit / 300000 mili detik
if (timeBuzz > 300000) {
keluar = 0;
}
}
while (keluar);
keluar = 1;
digitalWrite(buzzer, LOW);
lcd.clear();
}
else if (sensorON == true) {
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
//----------------------------------baca sensor asap
if (digitalRead(mq) == LOW && sensorON == true) {
digitalWrite(buzzer, HIGH);
lcd.setCursor(0, 2);
lcd.print(" There is Gas");
lcd.setCursor(0, 3);
lcd.print("Conditions of Danger");
char myMessage[] = "There is Gas";
sendSMS(my_number, myMessage);
millisAwal = millis();
do {
timeBuzz = millis() - millisAwal;
recSMS();
//-------waktu lama buzzer ON 5 menit / 300000 mili detik
if (timeBuzz > 300000) {
keluar = 0;
}
}
while (keluar);
keluar = 1;
digitalWrite(buzzer, LOW);
lcd.clear();
}
else if (sensorON == true) {
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
//----------------------------------baca sensor api
if (digitalRead(api) == LOW && sensorON == true) {
digitalWrite(buzzer, HIGH);
lcd.setCursor(0, 2);
lcd.print(" There is Fire");
lcd.setCursor(0, 3);
lcd.print("Conditions of Danger");
char myMessage[] = "There is Fire";
sendSMS(my_number, myMessage);
millisAwal = millis();
do {
timeBuzz = millis() - millisAwal;
recSMS();
//-------waktu lama buzzer ON 5 menit / 300000 mili detik
if (timeBuzz > 300000) {
keluar = 0;
}
}
while (keluar);
keluar = 1;
digitalWrite(buzzer, LOW);
lcd.clear();
}
else if (sensorON == true) {
lcd.setCursor(0, 3);
lcd.print(" Secure ");
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
How it Works
1. Connect the Arduino with Peripherals needed.
2. Plug in the Power Source on the device.
3. Add some library if needed
4. Compile and upload the script program5. First, Modem will initialize and it takes about 3 seconds
6. The LCD will show whether the security system has been On or Off.
7. Send SMS with the format "ON" to turn on the security system, LED Indicator will turn on and
LCD will show information that the security system has been ON.
8. When one of the sensors detects something, suppose the gas sensor detects the gas, the LCD will
show the detected gas, the buzzer will sound and will automatically send the message to a certain
number that contains that gas detected.
9. Send SMS with the format "ALARMOFF" to turn off the alarm which will then turn off the buzzer.
10 . Send SMS with the format "OFF" to turn off the security system which will also turn off the
LED Indicator.
Video for Project V - 9. Home Security System with 3 Sensor and SMS based using Arduino
Required file