/* GSM Module With PIR Sensor and arduino * this code will check the motion around the sensor and make a call to your phone to take an action. * also, you can SMS the device to activate and turn on or off an LED */ // Software Serial Library #include // Setup pin 2 and 3 as RX, TX for Software Serial SoftwareSerial GSerial(2, 3); // RX, TX //Defining Variables //the time we give the sensor to calibrate (10-60 secs according to the datasheet) int calibrationTime = 10; //the time when the sensor outputs a low impulse long unsigned int lowIn; //the amount of milliseconds the sensor has to be low //before we assume all motion has stopped long unsigned int pause = 5000; // the use for these variables will be defined in the LOOP Function boolean lockLow = true; boolean takeLowTime; //the digital pin connected to the PIR sensor's output int pirPin = 7; // This is the pin for the connected pir LED //تجديد منفذ 9 باسم pirled int pirled = 9; // This is the pin for the connected SMS LED //تجديد منفذ 10 باسم txtled int txtled = 10; //************** WARNING: put value = 0 if you don't want the GSM to delete any msgs from SIM Card********************* // the number of msgs deleted after recieving your control SMS this is to not max your SIM memory #define NUM_MSGS_DELETE 5 //recive buffer max number of char #define MAXCHAR 200 //recive gsm module back signal char buff[MAXCHAR]; //recive index int j = 0; //track time for the GSM to response int g_timeout = 0; // AT COMMANDS //////////////////////////////// Insert your Data ////////////////////////////////////////////// char CPIN[] = "AT+CPIN=\"0000\""; // pin code if required #define SEND_MSG_NUM "AT+CMGS=\"0503000000\"\r\n" // Phone Number to Receive SMS MSG #define SEND_MSG_TXT "Motheebotonics ! GSM is Working" // SMS TEXT DATA #define CALL_NUM "ATD0503000000;\r\n" // Emergency Call Number For Motion //////////////////////////////// Insert your Data // clean buffer void clearBuff(void) { for (j = 0; j <= MAXCHAR; j++) { buff[j] = 0x00; } j = 0; } // reads the input from GSM and saves it to buffer int readSerial(char result[]) { int i = 0; //delay(200); while (GSerial.available() > 0) { char inChar = GSerial.read(); Serial.write(inChar); if (inChar != '\r') { result[i] = inChar; i++; } if (i >= MAXCHAR - 1 ) { result[i] = '\0'; GSerial.flush(); return 0; } } result[i] = '\0'; GSerial.flush(); return 0; } // handles the wait for a response from GSM that exactly match the provided String int Hand(char *s) { clearBuff(); //delay(300); readSerial(buff); if (strstr(buff, s) != NULL) { g_timeout = 0; clearBuff(); return 1;// in case of a match return 1 } if (g_timeout > 50) { g_timeout = 0; return -1; // in case of a time out return -1 } g_timeout++; return 0; // in case of a not match add 1 to the time out and return 0 } // setup for GSM using AT Commands void AT(void) { clearBuff(); GSerial.println("ATE0"); //check if connected to GSM GSerial.flush(); Serial.println("ATE0"); delay(500); readSerial(buff); while (strstr(buff, "OK") == NULL) { clearBuff(); GSerial.println("ATE0"); GSerial.flush(); Serial.println("ATE0"); delay(500); readSerial(buff); } Serial.println("Module is Working"); ////////////////////////////UNCOMMENT BELOW IF SIM Require PIN Number///////////////////////// /* clearBuff(); GSerial.println(CPIN); // put pin number GSerial.flush(); Serial.println(CPIN); delay(500); readSerial(buff); while(strstr(buff,"OK")==NULL) { clearBuff(); GSerial.println(CPIN); GSerial.flush(); Serial.println(CPIN); delay(500); readSerial(buff); } Serial.println("PIN Done"); */ ////////////////////////////////////////////////////////////////////////////////////////// while (1) { clearBuff(); GSerial.println("AT+CREG?"); // Network registration GSerial.flush(); delay(500); readSerial(buff); // check if 1 (home network) or 5 (roaming) if ((strstr(buff, "0,1") != NULL) || (strstr(buff, "0,5") != NULL)) { clearBuff(); break; } else { delay(1000); } } clearBuff(); GSerial.println("AT+CMGF=1\r");// set SMS to TEXT MODE delay(500); while (Hand("OK") == 0); Serial.println("SMS TEXT MODE Done"); clearBuff(); GSerial.println("AT+CPMS=\"SM\"\r\n");// set memory to SIM delay(500); while (Hand("OK") == 0); return; } // this function is an extra, you can use it if needed // sends your text msg to the specified phone number above void send_txt() { clearBuff(); GSerial.println("AT+CMGF=1\r"); delay(500); while (Hand("OK") == 0); clearBuff(); GSerial.println(SEND_MSG_NUM); delay(500); while (Hand(">") == 0); GSerial.println(SEND_MSG_TXT); delay(1000); while (Hand("OK") == 0); return; } // calls your phone provided above in case of a detected motion void send_call() { clearBuff(); GSerial.println("AT+CPAS\r");// check phone status before calling twice GSerial.flush(); delay(500); if (Hand("0") != 1) { Serial.println("Already calling"); return; } GSerial.println(CALL_NUM); delay(2000); while (Hand("OK") == 0); delay(3000); return; } // reads your text and checks if it contains ON or OFF to control LED void read_txt(void) { clearBuff(); GSerial.println("AT+CMGL=\"REC UNREAD\"\r"); // an AT command to show all unread msgs and convert them to read GSerial.flush(); delay(500); clearBuff(); readSerial(buff); if ((strstr(buff, "ON") != NULL)) { // is there ON in SMS? digitalWrite(txtled, HIGH); // if yes then turn ON delete_msgs(); // delete all msgs return; } if ((strstr(buff, "OFF") != NULL)) { // is there OFF in SMS? digitalWrite(txtled, LOW); // if yes then turn LED OFF delete_msgs(); // delete all msgs return; } return; } // delete number of msgs specified earlier void delete_msgs() { int i; Serial.println("Deleting ALL MSGs"); for (i = 1; i < NUM_MSGS_DELETE + 1; i++) { GSerial.print("AT+CMGD="); GSerial.print(i); GSerial.print("\r\n"); delay(10); } } //******************** SETUP Arduino **************** void setup() { Serial.begin(9600); //Set BaudRate to default baud rate 9600 for both computer and GSM GSerial.begin(9600); GSerial.setTimeout(5000);// set Serial timeout pinMode(pirPin, INPUT); pinMode(pirled, OUTPUT); pinMode(txtled, OUTPUT); digitalWrite(pirPin, LOW); digitalWrite(txtled, LOW); digitalWrite(pirled, LOW); //give the sensor some time to calibrate Serial.print("calibrating sensor "); for (int i = 0; i < calibrationTime; i++) { Serial.print("."); delay(1000); } Serial.println("SENSOR ACTIVE"); delay(50); AT(); Serial.println("GSM ACTIVE"); } //******************** Main LOOP **************** void loop() { // check if GSM receive any SMS, if so, then read it if (GSerial.available() > 0) { Serial.println("check available"); delay(2000); clearBuff(); GSerial.println("AT+CPAS\r");// check phone status before calling twice GSerial.flush(); delay(500); if (Hand("0") != 1) { Serial.println("Already calling"); } else{ read_txt(); } } if (digitalRead(pirPin) == HIGH) { digitalWrite(pirled, HIGH); //motion detected, turn on LED if (lockLow) { //makes sure we wait for a transition to LOW before any further output is made: lockLow = false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis() / 1000); Serial.println(" sec"); delay(100); send_call(); // calling for emergency } takeLowTime = true; } if (digitalRead(pirPin) == LOW) { if (takeLowTime) { lowIn = millis(); //save the time of the transition from high to LOW takeLowTime = false; } //if the sensor is low for more than the given pause, //we assume that no more motion is going to happen if (!lockLow && millis() - lowIn > pause) { //makes sure this block of code is only executed again after //a new motion sequence has been detected lockLow = true; Serial.print("motion ended at "); Serial.print((millis() - pause) / 1000); Serial.println(" sec"); delay(100); digitalWrite(pirled, LOW);//turn off led } } delay(1000); }