GSM Shield Home Alarm

arduino gsm shield alarm movement pir sensor keypadThe other day my father and I were discussing about the problems of living in a third world country. Because of the social inequality, there is little security on the streets. Also one major problem is that no one is secure, even the 1% richer, because there are know cases of robbers getting inside homes even on private neighbourhoods. We ended up thinking about putting a home security system  in our house. The problem is those are very expensive and, in general, they just ring an alarm to scare potential thieves, but they don’t actually warn you in real-time that your house is being invaded. A complete system would be even more expensive. So I got the idea to make my own Arduino based home alarm. I just had to buy a GSM shield for the Arduino Uno, because all other parts I already had.

The parts

For this project you will need:

Connecting the components

First you will place the GSM shield above the Arduino Uno, then you will need to solder the wires GND and VCC together from the two sensors, the buzzer and the relay module input. After that, you connect those soldered wires on the corresponding connector from the GSM shield. Next you will make a connector from the input / output signals from those parts, and the last thing will be to connect the keypad above the shield. It will be:

  • Arduino Uno / GSM shield pins:
    • Pin 0: unconnected;
    • Pin 1: unconnected;
    • Pin 2: unconnected (the GSM will use this pin);
    • Pin 3: unconnected (the GSM will use this pin);
    • Pin 4: last row from keypad (keypad pin 4 – from 8);
    • Pin 5: unconnected;
    • Pin 6: second column from keypad (keypad pin 6 – from 8);
    • Pin 7: third column from keypad (keypad pin 7 – from 8);
    • Pin 8: unconnected (the GSM will use this pin);
    • Pin 9: unconnected (the GSM will use this pin);
    • Pin 10: PIR sensor number 2 data;
    • Pin 11: siren horn signal (goes to the relay module input);
    • Pin 12: PIR sensor number 1 data;
    • Pin 13: buzzer signal input;

As it is possible to see, although the keypad has 8 pins, I just connected three (one rom and two columns, allowing two numbers to be read – 1×2 matrix), because this way I can make passwords using these three wires, and there is no need to use all the pins from the keypad.  This is because once the movement sensor detects a person walking inside the room, the person will have just 5 seconds to deactivate the alarm, so there is not enough time for the thief to figure out the alarm code. Once the alarm is not disabled on the given time, the GSM shield will proceed to send SMS to you, or to call your phone.

The Arduino was programmed to call and once you answer the phone, it will finnish the call. This way it will spare the credits from the mobile operator.

Of course, it is possible to get false readings from the sensor, or even the code can be buggy, so I put an option to disable the alarm just by sending an SMS text from my phone to the Arduino. Also another option you can make is to order the shield to send you one message a day to ensure you it is working correctly.

The code

Just copy the code below and compile.

#include <Keypad.h>
#include <GSM.h>

//KEYPAD
const byte ROWS = 1; //four rows
const byte COLS = 2; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
                            {'0', '4'}
                            };
byte rowPins[ROWS] = {4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7}; //connect to the row pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, 
                      ROWS, COLS);
boolean movement1 = false;
boolean movement2 = false;
boolean locked = false;
boolean preparealarm = false;
unsigned long int timerpresskeys;
unsigned long int timermovemt;
unsigned long int timeralarmoff;
int numkeyspressed = 0;
int masterkey = {0};

//PIR SENSOR
#define PIR1 12
#define PIR2 10
#define LEDPIN 13

//BUZZER
#define BUZZER1 13
#define BUZZER2 11
unsigned long int alarmtimer = 0;

//GSM SHIELD
#define PINNUMBER ""
#define WAITTIME 5000
#define SIZEof 14
#define PHONE1 "+XXXXXXXXXXXXX" //phone number 1
#define PHONE2 "+XXXXXXXXXXXXX" //phone number 2
GSM gsmAccess;
GSM_SMS sms;
GSMVoiceCall vcs;
char senderNumber[SIZEof];
String snumber;

void setup() {
 boolean notConnected = true;
 Serial.begin(9600);

 pinMode(PIR1, INPUT);
 pinMode(PIR2, INPUT);
 pinMode(BUZZER1, OUTPUT);
 pinMode(BUZZER2, OUTPUT);
 digitalWrite(PIR1, LOW);
 digitalWrite(PIR2, LOW);
 digitalWrite(BUZZER1, HIGH);
 digitalWrite(BUZZER2, HIGH);

 //initialize GSM Shield
 Serial.println("SMS Messages Sender");
 while (notConnected) {
 if (gsmAccess.begin(PINNUMBER) == GSM_READY)
 notConnected = false;
 else {
 Serial.println("Not connected");
 delay(1000);
 }
 }
 Serial.println("GSM initialized");

 //give the PIR sensor some time to calibrate
 delay(5000);
 Serial.println("OK");

 //warn everything is ready
 buzzerSignal();
 buzzerSignal();
 buzzerSignal();
}

void loop() {
 char customKey = customKeypad.getKey(); //get keypad input
 checkintruder();
 if (customKey) {
   Serial.println(customKey);
   if (((customKey - 48) == masterkey)) {
      locked = !locked;
      Serial.println(locked);
      if (locked) {
         buzzerSignal();
         delay(WAITTIME);
         buzzerSignal();
      } else {
         buzzerSignal();
         buzzerSignal();
      }
    preparealarm = false;
    }
 }

 if ((millis() - timermovemt) > 3000) {
    movement1 = false;
    movement2 = false;
    preparealarm = false;
 }

 if ((locked) && (movement1 || movement2) && (!preparealarm) && (abs(mill is() - timeralarmoff) > WAITTIME)) {
     timerpresskeys = millis(); //will give time to press master key
     preparealarm = true;
     movement1 = false;
     movement2 = false;
 }

 if (((millis() - timerpresskeys) > 5000) && (locked) && (preparealarm)){
     ringthealarm();
     preparealarm = false;
     movement1 = false;
     movement2 = false;
     timeralarmoff = millis();
 }
}

boolean checkintruder(void) {
 if (digitalRead(PIR1) == HIGH) {
    movement1 = true;
    Serial.println("PIR1");
 }
 if (digitalRead(PIR2) == HIGH) {
    movement2 = true;
    Serial.println("PIR2");
 }
 timermovemt = millis();

 return (movement1 | movement2);
}

void buzzerSignal(void) {
 alarmtimer = millis();
 while (millis() - alarmtimer < 250) {
    digitalWrite(BUZZER1, LOW);
 }
 while (millis() - alarmtimer < 500) {
    digitalWrite(BUZZER1, HIGH);
 }
}


void buzzerAlarm(boolean soundalarm) {
 if (soundalarm) digitalWrite(BUZZER2, LOW);
 else digitalWrite(BUZZER2, HIGH);
}

void ringthealarm(void) {
 while (locked) {
    sms.beginSMS(PHONE1);
    sms.print("INTRUDER!!! ALARM!!");
    sms.endSMS();
    sms.beginSMS(PHONE2);
    sms.print("INTRUDER!!! ALARM!!");
    sms.endSMS();
    sms.beginSMS(PHONE1);
    sms.print("INTRUDER!!! ALARM!!");
    sms.endSMS();
    sms.beginSMS(PHONE2);
    sms.print("INTRUDER!!! ALARM!!");
    sms.endSMS();

    if (vcs.voiceCall(PHONE1)){
       Serial.println("Call Established. Enter line to end");
       vcs.hangCall();
    }

    if (vcs.voiceCall(PHONE2)) {
       Serial.println("Call Established. Enter line to end");
       vcs.hangCall();
    }
    //buzzerSignal(); for debug
    //buzzerSignal(); for debug
    //buzzerSignal(); for debug
    //buzzerSignal(); for debug
    Serial.println("Call Finished");
    buzzerAlarm(1);
    delay(50); //to comment
    buzzerAlarm(0); //to comment
    Serial.println("\nCOMPLETE!\n");
    timeralarmoff = millis();
    while (millis() - timeralarmoff < 2000) {
       delay(20);
       ReceiveSMS();
    }
 }
}

void ReceiveSMS(void) {
 char c;

 if (sms.available()){
    Serial.println("Message received from:");
    // Get remote number
    sms.remoteNumber(senderNumber, SIZEof);
    Serial.println(senderNumber);
    snumber = String(senderNumber);
    if ((snumber.equals(PHONE1)) || (snumber.equals(PHONE2))) {
       Serial.println("MASTER NUMBER");
       locked = !locked;
       if (locked) {
           //buzzerSignal(); for debug
           sms.beginSMS(senderNumber);
           sms.print("Alarm is ON!");
           sms.endSMS();
       } else {
           //buzzerSignal(); for debug
           //buzzerSignal(); for debug
           sms.beginSMS(senderNumber);
           sms.print("Alarm is OFF!");
           sms.endSMS();
       }
    }
    Serial.println("Discarded SMS");
    sms.flush();
    Serial.println("MESSAGE DELETED");
 }
}

Conclusion

Considering the code will make the Arduino Uno send SMS messages and call your phone in only five seconds after someone break into your house, I guess you will have plenty of time to call the police. Of course the siren horn will scare the thieves and they will run away taking whatever they can, but if you don’t use the siren, you will probably end up having them arrested. I hope this way you can have a more secure home. If you have some good idea please leave a comment, that I will improve my system here. Hope you guys find this article useful.

18 thoughts on “GSM Shield Home Alarm

  1. I might put something like this together, then hook it to a Pi or similar as a starter “Home hub”.

    To save the cost of the SIM card you could run it through an Internet SMS gateway via either a wired LAN connection or a WIFI dongle, & have it email you pictures, etc. But that’s a different thing, really.

    Liked by 1 person

      1. Can you at least send me some photos on my email or if you can send me some detail textual instructions from which to which pin to connect the parts, it is important for me to make this for school, so if you can help me somehow

        Like

  2. Can someone send me schematic, photo, video or something because I don’t understand His instructions for connecting the parts

    Thanks

    Like

  3. I get as far as “SMS Messages Sender” but nothing happens afterwards. Any suggestions? Thanks for the help.

    Like

  4. Can you help me get connected? Unless I change the setup code to read:
    void setup() { boolean notConnected = false;……
    I cannot get past “SMS Messages Sender”.
    When I change it to “false” I get:
    SMS Messages Sender
    GSM initialized
    OK
    PIR1
    but no SMS sent. What can I do??
    Thanks.

    Like

  5. If you are still monitoring this post, I am re-attempting this sketch and get an error. I am still not very familiar with using a GPRS shield on the Arduino. Can you assist please? Thanks for any help.

    In file included from /Users/noelkuck1/Documents/Arduino/libraries/GSM/src/GSM3MobileNetworkProvider.h:37:0,
    from /Users/noelkuck1/Documents/Arduino/libraries/GSM/src/GSM3MobileClientService.h:37,
    from /Users/noelkuck1/Documents/Arduino/libraries/GSM/src/GSM.h:42,
    from /Users/noelkuck1/Desktop/sketch_aug18c/sketch_aug18c.ino:2:
    /Users/noelkuck1/Documents/Arduino/libraries/GSM/src/GSM3MobileAccessProvider.h:37:36: error: redeclaration of ‘IDLE’
    enum GSM3_NetworkStatus_t { ERROR, IDLE, CONNECTING, GSM_READY, GPRS_READY, TRANSPARENT_CONNECTED, OFF};
    ^
    In file included from /Users/noelkuck1/Documents/Arduino/libraries/Keypad/src/Keypad.h:36:0,
    from /Users/noelkuck1/Desktop/sketch_aug18c/sketch_aug18c.ino:1:
    /Users/noelkuck1/Documents/Arduino/libraries/Keypad/src/Key.h:41:15: note: previous declaration ‘KeyState IDLE’
    typedef enum{ IDLE, PRESSED, HOLD, RELEASED } KeyState;
    ^
    In file included from /Users/noelkuck1/Documents/Arduino/libraries/GSM/src/GSM.h:46:0,
    from /Users/noelkuck1/Desktop/sketch_aug18c/sketch_aug18c.ino:2:
    /Users/noelkuck1/Documents/Arduino/libraries/GSM/src/GSM3ShieldV1BandManagement.h:49:125: warning: ‘typedef’ was ignored in this declaration
    typedef enum GSM3GSMBand {UNDEFINED, EGSM_MODE, DCS_MODE, PCS_MODE, EGSM_DCS_MODE, GSM850_PCS_MODE, GSM850_EGSM_DCS_PCS_MODE};
    ^
    /Users/noelkuck1/Desktop/sketch_aug18c/sketch_aug18c.ino: In function ‘void setup()’:
    /Users/noelkuck1/Desktop/sketch_aug18c/sketch_aug18c.ino:63:31: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    if (gsmAccess.begin(PINNUMBER) == GSM_READY)
    ^
    Multiple libraries were found for “Keypad.h”
    Used: /Users/noelkuck1/Documents/Arduino/libraries/Keypad
    Not used: /Users/noelkuck1/Documents/Arduino/libraries/FIIQBN0IEKIZGZZ
    Multiple libraries were found for “GSM.h”
    Used: /Users/noelkuck1/Documents/Arduino/libraries/GSM
    Not used: /Applications/Arduino.app/Contents/Java/libraries/GSM
    Using library Keypad at version 3.1.1 in folder: /Users/noelkuck1/Documents/Arduino/libraries/Keypad
    Using library GSM at version 1.0.5 in folder: /Users/noelkuck1/Documents/Arduino/libraries/GSM
    exit status 1
    Error compiling for board Arduino/Genuino Uno.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s