Microcontroller › PIC › home alarm security doesn’t work
- This topic has 2 replies, 2 voices, and was last updated 8 years, 2 months ago by firdaus.
-
AuthorPosts
-
August 2, 2016 at 12:23 pm #4531firdausParticipant
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 commandsconst char ATat[] = "AT"; // Every AT command starts with "AT"const char ATecho[] = "ATE0"; // Disable command echoconst char ATtxt[] = "AT+CMGF=1"; // Change SMS mode to Text message modeconst char ATphone[]= "AT+CMGS="01945335243""; // Send message to cell number : 123456789 (Enter your number instead of 123456789!)// Alarm messageconst char alarm_Message[] = "Alarm!! Intruder Detected in your Home!!!";//// Responses to parseconst GSM_OK = 0;const GSM_Ready_To_Receive_Message = 1;//Declare the pins to use for PIR, Push Button and Sirensbit 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 handlervoid 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 "> " responsesswitch (gsm_state) {case 0: {response = -1; // Clear responseif (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" responsegsm_state = 20; // Expecting CR+LF}elsegsm_state = 0; // Reset state machinebreak;}case 10: {if (receivedByte == ' ') {response_rcvd = 1; // We have "> " responseresponse = GSM_Ready_To_Receive_Message; // Set reception flagresponseID = response; // Set response ID}gsm_state = 0; // Reset state machinebreak;}case 20: {if (receivedByte == 13) // We have 13, it could be CR+LFgsm_state = 21; // Expecting LFelsegsm_state = 0; // Reset state machinebreak;}case 21: {if (receivedByte == 10) { // We have LF, response is completeresponse_rcvd = 1; // Set reception flagresponseID = response; // Set response ID}gsm_state = 0; // Reset state machinebreak;}default: { // Unwanted charactergsm_state = 0; // Reset state machinebreak;}}}}// Send command or data to the GSM Modulevoid Send_to_GSM(const char *x){// Send command or data stringwhile(*x) {UART1_Write(*x++);}// Terminatation by CR(Enter)UART1_Write(0x0D);}// Get GSM response, if there is anyshort Get_response() {if (response_rcvd) {response_rcvd = 0;return responseID;}elsereturn -1;}// Wait for GSM responsevoid Wait_response(char rspns) {while (Get_response() != rspns);}// Send alarm SMS!void Send_Alarm_Message(){Send_to_GSM(ATtxt); //Set SMS mode to textWait_response(GSM_OK); //Wait for modem to reply "OK"Send_to_GSM(ATphone); //Send message to cell numberWait_response(GSM_Ready_To_Receive_Message); //Wait for modem to reply "> "//Send the alarm messageSend_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 messageUART1_Write(0x0D); // EnterWait_response(GSM_OK); //Wait for modem to reply "OK"}// 30 Seconds delayvoid Wait_30S(){char i;for(i = 0; i < 30; i++){Delay_ms(1000);}}// 1 min delayvoid 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 digitalCMCON |= 7; // Disable comparators// Enable Uart Rx interruptPIE1.RCIE = 1;INTCON.PEIE = 1;INTCON.GIE = 1;PIR_Input_Direction = 1; //Set RB0 bit as input to read the state of the PIRPush_Button_Direction = 1; //Set RB1 bit as input to read the state of the Push ButtonSiren_Direction = 0; //Set RB2 bit as output to control the Siren relaySiren = OFF; //Switch OFF the SirenStatus_LED_Direction = 0; //Set RB3 bit as output to switch ON/OFF the status LEDStatus_LED = OFF; //Switch OFF the status LEDUART1_Init(19200); // Initialize USART moduleDelay_ms(100);// synchronize baud ratewhile(1) {Send_to_GSM(ATat); // Send "AT" string until GSM862 sets up its baud radeDelay_ms(100); // and gets it correctlyif (Get_response() == GSM_OK) // If GSM862 says "OK" on our baud rate we program can continuebreak;}// Disable command echoSend_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 misfireif (PIR) { // If motion is still detectablecount=1;delay_ms(50);}else{count=0;return;}}if (count){Wait_30S(); //Wait for 30 SecondsSend_Alarm_Message(); // send alarm messageWait_30S(); //Wait for 30 Secondsif (alarm_armed ){Siren = ON; // Activate the Siren (Relay)}}}}if (Push_Button){if (alarm_armed == 0){alarm_armed = 1; //Arm the alarm systemWait_1minute(); //If armed, wait for 1 minute before going into alarm modefor(i = 0; i < 60; i++){ //If armed, Blink the Status LED for 1 minute before going into alarm modeStatus_LED = ~ Status_LED;Delay_ms(1000);}Status_LED = ON; //In alarm mode, switch ON the status LED}else{alarm_armed = 0; //Disarm the alarm systemSiren = OFF; //Switch OFF the sirenStatus_LED = OFF; //Switch OFF the status LED}}}}August 19, 2016 at 4:35 pm #14119KiriroParticipanthye 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 #14120firdausParticipantsir, how can i contact with you? because this scematic maybe should not have here because i afraid with copyright reason.
-
AuthorPosts
- You must be logged in to reply to this topic.