itbrainpower.net
THE ALPHABET PROJECT - professional Arduino & Raspberry PI shields

SMS ALARM SYSTEM (MAGNETIC CONTACTS) WITH ARDUINO AND 3G / GSM SHIELD

 

About the project

Just in idea to fast build your own alarm system. You can supervise one or more magnetic contacts and page via SMS the "burgler event". You can ENABLE/DISABLE the alarm system from remote via single SMS.

Use your alarm with pin 2 pin compatible GSM shields (c-uGSM or h-nanoGSM) or 3G shield (d-u3G).

 

Needed parts

 

About c-uGSM, d-u3G and h-nanoGSM shields

There are pin 2 pin compatible shields, first one it is quad band GSM only(worl wide compatible) and has support for 2SIMs and the second one it is SINGLE SIM 3G only (North American version) / 3G+GSM (European and rest of the world version).

Those shields are compact as 1.25"x1.57"(31.75x39.88mm) / 1.35"x1.57"(34.29x39.88mm), weight around 10g, have USB support (communication and powering), auto level 3.3V-5V digital interfaces and Lithium Polimer charger integrated.

d-u3G 3G/UMTS shield   d-u3G 3G/UMTS shield

h-nanoGSM - worldwide GSM + BTH 3.0 nano shield  

The third one - h-nanoGSM - became commercially available in 2016, August. It is a quad band GSM only(world wide compatible) + Bluetooth 3.0 nano shield, packed in an compact format 1.25"x1.16"(31.75x29.46mm) and with weight around 8g. Same as his bigger brothers (c-uGSM and d-u3G), it is not only a break-board, but a full shield having powerful features embedded, as: USB support (communication and powering), auto level 2.8V-5V digital interfaces and Lithium Polimer charger integrated.

 

Hardware connections

In the picture bellow you can observe all the needed connections.

SMS Alarm with Arduino wiring

 

Above, we use the c-uGSM shield in "WITH Lithium Polymer" configuration. In the d-u3G shield case, just connect the wires to the very same pins on the 3G shield. The same wiring schema it is valid for h-nanoGSM usage, but GSM PIN3 connection (the BLUE wire), can be un-connected.

To use it in "WITHOUT Lithium Polymer configuration", remove the RED wire (+5V) and connect the shield Vcc(4V) to a 4V power adapter. Read advanced interfacing and wiring info (click to expand) and the reference: c-uGSM, h-nanoGSM and d-u3G how to start.

display/hide advanced interfacing and wiring info


The magnetic contact it is connected between ARDUINO UNO pins D4 and GND. HINT: if you would like to have many magnetic contacts, just connect them in paralel mode (alternate way: use separate Arduino digital pins and modify the software...).

 

Software

a. Start making a folder named "SMS_ALARM_Magnetic_contact_supervisor".

b. Then download the "c-uGSM kickstart for Arduino" from c-uGSM CODE SAMPLES and UTILITIES, or "h-nanoGSM kickstart for Arduino" from h-nanoGSM CODE SAMPLES and UTILITIES, or "d-u3G kickstart for Arduino" from d-u3G CODE SAMPLES and UTILITIES if you use the d-u3G shield. Product EMEI and your email adress is requiered.

c. Uncompress the archive and copy "cuGSM_basic_lbr.h", "cuGSM_basic_lbr.ino", "cuGSM_SMS_lbr.h" and "cuGSM_SMS_lbr.ino" ("du3_basic_lbr.h", "du3G_basic_lbr.ino", "du3G_SMS_lbr.h" and "du3G_SMS_lbr.ino" - 4 d-u3G users, or "hnanoGSM_basic_lbr.h", "hnanoGSM_basic_lbr.ino", "hnanoGSM_SMS_lbr.h" and "hnanoGSM_SMS_lbr.ino" 4 the h-nanoGSM users) files to the folder previously created.

d. Copy the code bellow and make a file called "SMS_ALARM_Magnetic_contact_supervisor.ino" in the very same folder as above. Alternate, you can download from here (right click & save as):
SMS_ALARM_Magnetic_contact_supervisor.ino : ARDUINO SMS ALARM & GSM/3G SHIELD - main code

SMS_ALARM_Magnetic_contact_supervisor.ino

1/*
2SMS_ALARM_Magnetic_contact_supervisor.ino v 0.4/20160107 - c-uGSM shield v 1.13 /d-u3G shield 1.13 SOFTAWARE EXAMPLE
3COPYRIGHT (c) 2015-2016 Dragos Iosub / R&D Software Solutions srl
4
5You are legaly entitled to use this SOFTWARE ONLY IN CONJUNCTION WITH c-uGSM or d-u3G DEVICES USAGE. Modifications, derivates and redistribution 
6of this software must include unmodified this COPYRIGHT NOTICE. You can redistribute this SOFTWARE and/or modify it under the terms 
7of this COPYRIGHT NOTICE. Any other usage may be permited only after written notice of Dragos Iosub / R&D Software Solutions srl.
8
9This SOFTWARE is distributed is provide "AS IS" in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
10warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
12Dragos Iosub, Bucharest 2016.
13http://itbrainpower.net
14*/
15
16
17//#define du3GShieldUsage       //un-comment this if you use the d-u3G shield
18
19static char phoneNumber[15] = "0712345678";   //destination number
20
21static char nunderstand[40] = "? enable cmd: #1, disable cmd:  #0.";
22static char armed[10] = "ARMED";
23static char unarmed[10] = "NOTARMED";
24static char alarm[20] = "ALARM STATE";
25static char nalarm[20] = "NORMAL STATE";
26static char boot[20] = "BOOT EVENT";
27
28#define magneticCONTACT    4     //Arduino Digital pin used for Magenetic Contact
29
30/*#####NO PARAMS TO CHANGE BELLOW#####*/
31
32#define powerPIN         7  //Arduino Digital pin used to power up / power down the modem
33#define resetPIN         6  //Arduino Digital pin used to reset the modem 
34#define statusPIN        5  //Arduino Digital pin used to monitor if modem is powered 
35
36int supervisorMask = 1;   //mask contact monitoring 1 - monitor enabled, 0 - monitor disabled
37
38#if defined (du3GShieldUsage)
39 #include "du3G_basic_lbr.h"
40 #include "du3G_SMS_lbr.h"
41#else
42 #include "cuGSM_basic_lbr.h"
43 #include "cuGSM_SMS_lbr.h"
44#endif
45
46#if (ARDUINO >= 100)
47 #include "Arduino.h"
48 #include <SoftwareSerial.h>
49#else
50 #include "WProgram.h"
51 #include <NewSoftSerial.h>
52#endif
53
54#define UNO_MODE                   //used by modem library
55#define BUFFDSIZE 162              //used by modem library
56
57SoftwareSerial agsmSerial(2, 3);   //RX==>2 ,TX soft==>3
58
59char ch;
60char buffd[BUFFDSIZE];
61char readBuffer[162];
62//static char mBuffer[162];
63int state=0, i=0, powerState = 0; 
64int alarmState=0, prevalarmState=0;
65int getAlarmStatus = 0;
66
67void procRespose(char* msg){
68 Serial.println(msg);Serial.flush();delay(150);
69 memset(readBuffer, 0x00, sizeof(readBuffer));
70
71 alarmState = digitalRead(magneticCONTACT);
72
73 if(prevalarmState!= alarmState){      //debouce here
74  delay(50);
75  alarmState = digitalRead(magneticCONTACT);
76 }
77
78 if(alarmState == HIGH)
79            sprintf(readBuffer, "%s - %s", msg, alarm/*"Alarm State"*/);
80 else
81            sprintf(readBuffer, "%s - %s", msg, nalarm/*"Normal State"*/);
82 Serial.println(readBuffer);Serial.flush();delay(150);
83 sendSMS(phoneNumber, readBuffer);      //confirm command executed
84 delay(100);
85}
86
87void setup(){
88 Serial.begin(19200);     //start serial connection
89
90 pinMode(magneticCONTACT, INPUT_PULLUP); //configure magneticCONTACT as an input PULL UP
91 pinMode(13, OUTPUT);     //configure the UNO D13 as output - UNO embedded LED used as contact status indicator  
92
93 agsmSerial.begin(9600);     //next, modem setup
94 clearagsmSerial();
95 clearSerial();
96 delay(10);
97
98 modemHWSetup();       //configure UNO IN and OUT to be used with modem
99
100 Serial.flush();
101 agsmSerial.flush();
102 delay(500);
103
104 Serial.println(F("wait until c-uGSM/d-u3G is ready"));
105 Serial.flush();
106
107 powerOnModem();
108
109 clearBUFFD();
110 while(strlen(buffd)<1){
111  getIMEI();
112  delay(500);
113 }
114
115 //Serial.println(F("modem is up. prepare it for SMS processing")); 
116 //Serial.flush();
117
118 ready4SMS = 0; 
119 ready4Voice = 0;
120
121 //Serial.print(F("modem IMEI: ")); Serial.flush();
122
123 setupMODEMforSMSusage();
124
125 Serial.println(F("modem ready.. let's proceed"));
126 Serial.flush();
127 delay(100);
128
129 procRespose((char*)boot);
130
131 alarmState = digitalRead(magneticCONTACT);
132 prevalarmState = alarmState;
133
134 supervisorMask = 1;  
135}
136
137void loop(){
138 alarmState = digitalRead(magneticCONTACT);
139 if(prevalarmState!= alarmState){      //debouce here
140  delay(50);
141  alarmState = digitalRead(magneticCONTACT);
142 }
143
144 if(getAlarmStatus != 0 || prevalarmState!= alarmState){
145  prevalarmState = alarmState;         //copy alarmState for next cycle
146  getAlarmStatus = 0;
147  memset(readBuffer, 0x00, sizeof(readBuffer));
148  if (alarmState == HIGH) {
149   if(supervisorMask != 0) digitalWrite(13, LOW);       //show to LED
150   sprintf(readBuffer, "%s", alarm/*"Alarm State"*/);
151  
152  else {
153   if(supervisorMask != 0) digitalWrite(13, HIGH);       //show to LED
154   sprintf(readBuffer, "%s", nalarm/*"Normal State"*/);
155  }
156
157  if(supervisorMask != 0){
158   sendSMS(phoneNumber,readBuffer);
159  }
160
161  Serial.println(readBuffer);
162  Serial.flush();
163  delay(100);
164 }
165
166 /*process SMS commands - start*/
167 listSMS();            //find the last used SMS location
168 clearagsmSerial();
169 int cnt;
170 cnt = noSMS;
171 while (cnt>0){
172
173  readSMS(cnt);              //the SMS content will be returned in buffd 
174  /*here process SMS the content - start*/
175
176  Serial.print(F("SMS content: "));Serial.flush();delay(50);
177  Serial.println(buffd);Serial.flush();delay(50);
178
179  delay(50);
180  clearagsmSerial();
181  delay(50);
182
183  if(strlen(buffd) > 0){            //non empty SMS
184     if(buffd[0] == '#' && buffd[1]== '1'){       //disable send SMS on magnetic contact event detection
185    procRespose((char*)armed);
186    supervisorMask = 1;
187    //getAlarmStatus = 1;
188   }
189   else {
190      if(buffd[0]== '#' && buffd[1]== '0'){       //disable send SMS on magnetic contact event detection
191     procRespose((char*)unarmed);
192     supervisorMask = 0;
193     //getAlarmStatus = 1;    
194    }else{               //unknown command
195     procRespose((char*)nunderstand);
196    }
197   }
198
199   deleteSMS(cnt);             //free the SMS location
200  }
201  /*here process SMS the content - end*/
202  clearBUFFD();
203  clearagsmSerial();
204  cnt--;
205 }
206 /*process SMS commands - stop*/
207  delay(20);
208}
209

e. Edit the "SMS_ALARM_Magnetic_contact_supervisor.ino" (double click on the file; this one will be open by the ARDUINO environment).
Write in your desired destination mobile number on line 19. (Before this you may check the supported SMS number format by your NMO, using our kickstart software.)
You may edit the SMS commands and the SMS messages assigned to the alarm events.

Un-comment line 17 only if you use the d-u3G shield.

 

Usage

#0 DISARM the ALARM, #1 ARM the ALARM. Any other SMS, makes the ALARM to reply with a sort of "help message".


Any replied SMS has apended the SYSTEM STATUS: "ALARM STATE"/"NORMAL STATE".

TUTORIAL and SOFTWARE ARE PROVIDED WITHOUT ANY WARRANTY!!! USE IT AT YOUR OWN RISK!!!!

 

Possible improvements

  • Restrict SMS processing only from "phoneNumber" (master number)... Just look in the function readSMS (SMS library), after seacond readline(), the buffd variable contains the SMS header (sender number, date+time).. Just processe there in order to extract the SMS sender number...
  • Add a TIP122 at Arduino D13-output (serialized via 1k resistor to the transistor base), in order to drive a local siren??

 

 

 

 

Dragos Iosub & itbrainpower.net team original how to.

 

 

 

 

document version 1.2 / 2016-08-22 © R&D Software Solutions srl