Home Automation

Im using Arduino Uno and Arduino Gsm/GPRS SIM900 and PIR sensor. I have a problem in this project. Let me brief my project. After the PIR sensor detected any motion it will send a message to my hand phone. After that its depends on me to reply the message as " on the alarm" or " reset ". Than the Arduino should perform accordingly to my reply. I have a coding but its not working. I'm new to arduino and coding so i can't identify the problem. Anyone please solve my problem. Need it very urgent. Please guys. Advance thank for those help me. :slight_smile:

latest coding.txt (4.69 KB)

The code you attached does not compile. Also, better get used to indent your code or you won't know what's in what in the code. I fixed and indented the code, also modified some parts that didn't make any sense.
While it does compile now, I didn't test it (i don't have a PIR sensor).
Also, your SIM900 module does not need to be turned on before starting using it?

#include <SoftwareSerial.h>
#include "pins_arduino.h"
#include <String.h>

SoftwareSerial SIM900(7, 8);
String msg = String("");
char data;
//int alarm_light = 5;
int led = 13;
int pir_snsr=10;
int val,pos,output,pir_output;
int count_pir=0;
int ind_pir=0;
String index;
boolean sms_content_flag_on = 0;
boolean sms_content_flag_off = 0;
boolean sms_content_flag_critical = 0;
boolean sms_content_flag_pir = 0;

void setup() {
  SIM900.begin(19200); // the GPRS baud rate
  Serial.begin(19200); // the Serial port of Arduino baud rate.
  delay(500);
  SIM900.println("AT+CPIN=4510"); //the pin code for the sim
  delay(5000);
  SIM900.println("AT+CMGF=1"); //sets the text mode
  delay(1000);
  pinMode(led,OUTPUT);
  pinMode(pir_snsr,INPUT);
  //pinMode(alarm_light,OUTPUT);
  //digitalWrite(led,HIGH);
  //delay(500);
  SIM900.println("AT+CMGDA=\"DEL ALL\""); //delete all the messages available in the storage area
  delay(1000);
}

void loop() {
  read_pirsensor(); //function to read the pir sensor
  if (SIM900.available()) { // if date is comming from softwareserial port ==> data is comming from gprs shield
    while(SIM900.available()) {
      data = SIM900.read(); //read the data available in the SIM900 buffer and store it in string msg
      msg += data;
    }
    Serial.println(msg);
    if(msg.indexOf("+CMTI") >= 0) { //if the sms is received then go to process gprs
      Serial.println("goes inside cmti");
      process_gprs();
    } else {
      clear_msg(); //Modified by Spyd
    }
  }
}

/**********************************************************
 READ THE PIR SENSOR
*************************************************************/
void read_pirsensor() {
  pir_output = digitalRead(pir_snsr);
  delay(1000);
  Serial.println(pir_output);

  if(pir_output == HIGH){
    count_pir = count_pir+1;
    Serial.println(count_pir);
    if(count_pir > 0){ //Modified by Spyd
      sms_content_flag_pir = 1;
      send_sms();
    }
  }
}

/**********************************************************************
 SEARCH THE INDEX OF THE SMS AND READ THE CONTENT OF THE SMS MESSAGE
*********************************************************************/
void process_gprs(){
  pos = msg.indexOf(",");
  //search for the position of , to find the index of sms
  //Serial.println(pos);
  String index = msg.substring(pos+1);
  //the index of sms to be processed
  Serial.println(index);
  SIM900.print("AT+CMGR = ");
  SIM900.println(index);
  //read the incoming sms
  delay(1000);
  if(msg.indexOf("+CMGR") >=0){
    Serial.println("goes inside cmgr");
    process_sms();
  }
}

/******************************************************************
 PERFORMS THE ACTION ACCORDING TO THE CONTENT OF THE MESSAGE
*****************************************************************/
void process_sms() {
  Serial.println("goes inside process_sms");
  if(msg.indexOf("Turn light on") >= 0){
    Serial.println("goes inside on");
    digitalWrite(led,HIGH);
  }
  if (msg.indexOf("Turn light off") >= 0){
    Serial.println("goes inside off");
    digitalWrite(led,LOW);
  }
  /*switch(val){ //Spyd: val variable is never used
    case 0:
      Serial.println("it is inside the case0");
      sms_content_flag_off=1;
      send_sms();
      break;
    case 1:
      sms_content_flag_on =1;
      send_sms();
      Serial.println("it is inside the case1");
      Serial.println(val);
      break;
  }*/
  SIM900.print("AT+CMGD=");
  SIM900.println(index); //removes the message after it is executed to keep the sim memory free
  SIM900.flush(); //removes all the data from the RX buffer of softwareserial whether it is read or unread
  delay(1000);
  clear_msg();
  Serial.println("the buffer is deleted cleared");
}

void clear_msg(){
  msg="";
}

/***********************************************
SENDS THE SMS MESSAGES TO THE MOBILE PHONE
**********************************************/
void send_sms(){
  //SIM900.println("AT+CMGF=1");
  SIM900.println("AT + CMGS = \"+60********70\""); //FIX THE PHONE NUMBER
  //send sms message, be careful need to
  //add a country code before the cellphone number
  delay(100);
  if(sms_content_flag_on == 1) {
    SIM900.println("The light is on ");//the content of the message
  }
  if(sms_content_flag_off == 1) {
    SIM900.println("The light is off ");
  }
  if(sms_content_flag_pir == 1){
    ind_pir++;
    SIM900.println("Someone is moving around your house");
  }
  SIM900.println((char)26);//the ASCII code of the ctrl+z is 26
  delay(100);
  SIM900.println();
  clear_sms_content_flags();
  msg="";
}

/*****************************************************
CLEARS THE FLAGS SET
****************************************************/
void clear_sms_content_flags(){
  sms_content_flag_on = 0;
  sms_content_flag_off = 0;
  sms_content_flag_critical = 0;
  sms_content_flag_pir = 0;
}

oh.. sorry. I just start to learning. Thank you for the reply. I will test it and inform the result back here. Thank you very much friend. :slight_smile:

hmmm. The coding still not functioning. Sensor is working properly because it detecting the motion but i din get any message from the arduino. Please check for me the coding.

coding from forum.txt (4.88 KB)