EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / home alarm security doesn’t work

home alarm security doesn’t work

|

Microcontroller › PIC › home alarm security doesn’t work

  • This topic has 2 replies, 2 voices, and was last updated 8 years, 10 months ago by firdaus.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • August 2, 2016 at 12:23 pm #4531
    firdaus
    Participant

    hello i have problem..i was did project home alarm security using gsm, when PIR sensor detect movement it will send sms and a siren will sound but when i do this project it doesn't work..i dont know what wrong is this may be coding for this PIC but i don't know how to read coding..can anyone help me?? this is my coding:

    // Set of AT commands
    const char ATat[]   = "AT";                        // Every AT command starts with "AT"
    const char ATecho[] = "ATE0";                      // Disable command echo
    const char ATtxt[]  = "AT+CMGF=1";                 // Change SMS mode to Text message mode
    const char ATphone[]= "AT+CMGS="01945335243"";     // Send message to cell number : 123456789 (Enter your number instead of 123456789!)
     
    // Alarm message
    const char alarm_Message[] = "Alarm!!  Intruder Detected in your Home!!!";
    //
    // Responses to parse
    const GSM_OK = 0;
    const GSM_Ready_To_Receive_Message = 1;
     
    //Declare the pins to use for PIR, Push Button and Siren
    sbit PIR at RB0_bit;
    sbit PIR_Input_Direction at TRISB0_bit;
    sbit Push_Button at RB1_bit;
    sbit Push_Button_Direction at TRISB1_bit;
    sbit Siren at RB2_bit;
    sbit Siren_Direction at TRISB2_bit;
    sbit Status_LED at RB3_bit;
    sbit Status_LED_Direction at TRISB3_bit;
     
    #define ON 1;
    #define OFF 0;
     
    char gsm_state = 0;
    char response_rcvd = 0;
    short responseID = -1, response = -1;
     
    // uart rx interrupt handler
    void interrupt(){
    char receivedByte;
      if (PIR1.RCIF == 1) {                  // Check if we have uart rx interrupt request?
       receivedByte = UART1_Read();         // Get received byte
     
    // process reception of mode feedback, "OK" and "> " responses
        switch (gsm_state) {
          case  0: {
                    response = -1;                   // Clear response
                    if (receivedByte == 'O')         // We have 'O', it could be "OK"
                      gsm_state = 1;                 // Expecting 'K'
                    if (receivedByte == '>')         // We have '>', it could be "> "
                      gsm_state = 10;                // Expecting ' '
                    break;
                   }
          case  1: {
                    if (receivedByte == 'K') {       // We have 'K' ->
                      response = GSM_OK;             // We have "OK" response
                      gsm_state = 20;                // Expecting CR+LF
                    }
                    else
                      gsm_state = 0;                 // Reset state machine
                    break;
                   }
          case 10: {
                    if (receivedByte == ' ') {
                      response_rcvd = 1;             // We have "> " response
                      response = GSM_Ready_To_Receive_Message; // Set reception flag
                      responseID = response;         // Set response ID
                    }
                    gsm_state = 0;                   // Reset state machine
                    break;
                    }
     
          case 20: {
                    if (receivedByte == 13)          // We have 13, it could be CR+LF
                      gsm_state = 21;                // Expecting LF
                    else
                      gsm_state = 0;                 // Reset state machine
                    break;
                   }
          case 21: {
                    if (receivedByte == 10) {         // We have LF, response is complete
                      response_rcvd = 1;             // Set reception flag
                      responseID = response;         // Set response ID
                    }
                    gsm_state = 0;                   // Reset state machine
                    break;
                   }
          default: {                                 // Unwanted character
                    gsm_state = 0;                   // Reset state machine
                    break;
                   }
        }
      }
    }
     
    // Send command or data to the GSM Module
    void Send_to_GSM(const char *x)
    {
    // Send command or data string
       while(*x) {
          UART1_Write(*x++);
       }
    // Terminatation by CR(Enter)
       UART1_Write(0x0D);
    }
    // Get GSM response, if there is any
    short Get_response() {
      if (response_rcvd) {
        response_rcvd = 0;
        return responseID;
      }
      else
        return -1;
    }
    // Wait for GSM response
    void Wait_response(char rspns) {
      while (Get_response() != rspns)
        ;
    }
    // Send alarm SMS!
    void Send_Alarm_Message(){
           Send_to_GSM(ATtxt);    //Set SMS mode to text
           Wait_response(GSM_OK); //Wait for modem to reply "OK"
           Send_to_GSM(ATphone);  //Send message to cell number
           Wait_response(GSM_Ready_To_Receive_Message); //Wait for modem to reply "> "
     
           //Send the alarm message
           Send_to_GSM("Alarm!!rn");
           Send_to_GSM("Intruder Detected in your House!!!rn");
           Send_to_GSM("Siren will be activated in few seconds.");
           UART1_Write(0x1A);          //Cntrl + Z to send message
           UART1_Write(0x0D);          // Enter
           Wait_response(GSM_OK);      //Wait for modem to reply "OK"
    }
     
    // 30 Seconds delay
    void Wait_30S(){
    char i;
        for(i = 0; i < 30; i++){
          Delay_ms(1000);
        }
    }
    // 1 min delay
    void Wait_1minute(){
    char i;
        for(i = 0; i < 60; i++){
          Delay_ms(1000);
        }
    }
     
    char alarm_armed = 0;
    int count,i;
     
    void main() {
      
      WDTCON = 0;
      ADCON1 |= 0x0F;                 // Configure all ports with analog function as digital
      CMCON  |= 7;                    // Disable comparators
     
    // Enable Uart Rx interrupt
      PIE1.RCIE   = 1;
      INTCON.PEIE = 1;
      INTCON.GIE  = 1;
     
      PIR_Input_Direction = 1;        //Set RB0 bit as input to read the state of the PIR
      Push_Button_Direction = 1;      //Set RB1 bit as input to read the state of the Push Button
      Siren_Direction = 0;            //Set RB2 bit as output to control the Siren relay
      Siren = OFF;                    //Switch OFF the Siren
      Status_LED_Direction = 0;       //Set RB3 bit as output to switch ON/OFF the status LED
      Status_LED = OFF;               //Switch OFF the status LED
     
      UART1_Init(19200);             // Initialize USART module
      Delay_ms(100);
     
    // synchronize baud rate
      while(1) {
        Send_to_GSM(ATat);                            // Send "AT" string until GSM862 sets up its baud rade
        Delay_ms(100);                               // and gets it correctly
        if (Get_response() == GSM_OK)                // If GSM862 says "OK" on our baud rate we program can continue
          break;
      }
     
    // Disable command echo
     Send_to_GSM(ATecho);
     Wait_response(GSM_OK);
     
     
     while(1){
     if (alarm_armed) {     //Check if the alarm is armed!
      count=0;
     if(PIR) {           // If motion was detected !
     for( i = 0; i < 10; i++){  // Test few times if there is still motion (500ms), so you don't misfire
       if (PIR) {       // If motion is still detectable
         count=1;
         delay_ms(50);
        }
       else
        {
         count=0;
         return;
        }
     }
     if (count)
      {
        Wait_30S();             //Wait for 30 Seconds
        Send_Alarm_Message();   // send alarm message
        Wait_30S();             //Wait for 30 Seconds
        if (alarm_armed ){
          Siren = ON;          // Activate the Siren (Relay)
        }
     
      }
       }
       }
       if (Push_Button)
      {
      if (alarm_armed == 0)
      {
      alarm_armed = 1;      //Arm the alarm system
      Wait_1minute();        //If armed, wait for 1 minute before going into alarm mode
      
      for(i = 0; i < 60; i++){  //If armed, Blink the Status LED for 1 minute before going into alarm mode
          Status_LED = ~ Status_LED;
          Delay_ms(1000);
        }
        Status_LED = ON;   //In alarm mode, switch ON the status LED
      }
      else
      {
      alarm_armed = 0;      //Disarm the alarm system
      Siren = OFF;        //Switch OFF the siren
      Status_LED = OFF;   //Switch OFF the status LED
      }
     }
    }
    }
     
    August 19, 2016 at 4:35 pm #14119
    Kiriro
    Participant

    hye firdaus , from ur coding , it seem like its already correct . it maybe problem between ur pic and gsm.. and i dont clearly understand ur project without a schematic . do provide it . I think ur problem is because PIC provide 5v and Most GSM operate in 2.8v to 3.9v . both device cannot comunicate and if u do directly from pic to gsm . it may broke ur gsm . so i suggesting u use LOGIC LEVEL CONVERTER ( LC04A) . it just 2$ . and remember to use TTL pin in ur gsm and not using serial comunication pin . in my gsm ( sim900a) the ttl pin have 6pin … the two top provide 3v , middle is 5v . and bottom is GND and VDD .  when ur using LOGIC LEVEL CONVERTER .  use 3v TTL pin Gsm to LLC LV . and 5v from pic to LLC HV.  '-' on both lv and hv should be in the same GND.  for more detail contact me .

    August 19, 2016 at 5:34 pm #14120
    firdaus
    Participant

    sir, how can i contact with you? because this scematic maybe should not have here because i afraid with copyright reason.

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Can I make two inputs from one?? June 22, 2025
  • Fun with AI and swordfish basic June 22, 2025
  • Simple LED Analog Clock Idea June 22, 2025
  • Is AI making embedded software developers more productive? June 22, 2025
  • Behlke swich June 21, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise