--> basic arduino tutorial | Deskripsi Singkat Blog di Sini

basic arduino tutorial

Friday, September 8, 2017

Project V - 9. Home Security System with 3 Sensor and SMS based using Arduino

Project V - 9. Home Security System with 3 Sensor and SMS based using Arduino

Home Security System with 3 Sensor and SMS based using Arduino

|| 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
  • 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

SIM900A | Source                                      Gas Sensor MQ2 | Source



Buzzer | Source                                              LED | Source


PIR Sensor | Source                                   Flame Sensor | Source


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;
    }
  }
}

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 program
5. 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


Thursday, September 7, 2017

Project V - 8. Read SMS using SIM900A Based on Arduino

Project V - 8. Read SMS using SIM900A Based on Arduino

Read SMS using SIM900A


Arduino is used to process SMS data and display it on a 20x4 LCD connected to an I2C connection. The GSM module used is SIM900A.

Hardware Requirement
  • Arduino Uno Board
  • SIM900A GSM Module (you can also use Wavecom)
  • LCD 16*2 or 20*4 (with i2c connection is recomended)
  • Power supply +5 Volt
  • Jumper

SIM900A | Source

Block Diagram


  Schematic




Arduino LCD 20x4
 Learn more about I2C connections --> Project V - 14


Arduino - SIM900A


Source Code

#include <GPRS_Shield_Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4);
#define PIN_TX    9
#define PIN_RX    8
#define BAUDRATE  9600

#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
int messageIndex = 0;

char phone[16];
char datetime[24];

GPRS gprsTest(PIN_TX, PIN_RX, BAUDRATE); //RX,TX,PWR,BaudRate

void setup() {
  lcd.begin();
  delay(6000);
  lcd.setCursor(0, 0);
  lcd.print("Initialization.....");
  while (!gprsTest.init()) {
    delay(1000);
    lcd.setCursor(0, 1);
    lcd.print("   init error  ");
  }
  lcd.setCursor(0, 1);
  lcd.print(" Gprs init success");
  delay(3000);
}

void loop() {
  messageIndex = gprsTest.isSMSunread();
  lcd.setCursor(0, 0);
  lcd.print("  System is ready  ");
  lcd.setCursor(0, 1);
  lcd.print(" Waiting a message");
  if (messageIndex > 0) { //At least, there is one UNREAD SMS
    gprsTest.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
    //In order not to full SIM Memory, is better to delete it
    gprsTest.deleteSMS(messageIndex);
    lcd.setCursor(3, 2);
    lcd.print(phone);
    lcd.setCursor(2, 3);
    lcd.print(message);
    delay(5000);
    lcd.clear();
  }
}

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 program
5. First, Modem will initialize and it takes about 3 seconds



6. After a few second


7. When there is one incoming message, it will be directly displayed on the LCD of the sender
    number and the contents message

8. Lcd will return to normal view.


Video for Project V - 8. Read SMS using SIM900A Based on Arduino



Required file