/* SMS_ALARM_Magnetic_contact_supervisor.ino v 0.4/20160107 - c-uGSM shield v 1.13 /d-u3G shield 1.13 SOFTAWARE EXAMPLE COPYRIGHT (c) 2015-2016 Dragos Iosub / R&D Software Solutions srl You are legaly entitled to use this SOFTWARE ONLY IN CONJUNCTION WITH c-uGSM or d-u3G DEVICES USAGE. Modifications, derivates and redistribution of this software must include unmodified this COPYRIGHT NOTICE. You can redistribute this SOFTWARE and/or modify it under the terms of this COPYRIGHT NOTICE. Any other usage may be permited only after written notice of Dragos Iosub / R&D Software Solutions srl. This SOFTWARE is distributed is provide "AS IS" in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Dragos Iosub, Bucharest 2016. http://itbrainpower.net */ //#define du3GShieldUsage //un-comment this if you use the d-u3G shield static char phoneNumber[15] = "0712345678"; //destination number static char nunderstand[40] = "? enable cmd: #1, disable cmd: #0."; static char armed[10] = "ARMED"; static char unarmed[10] = "NOTARMED"; static char alarm[20] = "ALARM STATE"; static char nalarm[20] = "NORMAL STATE"; static char boot[20] = "BOOT EVENT"; #define magneticCONTACT 4 //Arduino Digital pin used for Magenetic Contact /*#####NO PARAMS TO CHANGE BELLOW#####*/ #define powerPIN 7 //Arduino Digital pin used to power up / power down the modem #define resetPIN 6 //Arduino Digital pin used to reset the modem #define statusPIN 5 //Arduino Digital pin used to monitor if modem is powered int supervisorMask = 1; //mask contact monitoring 1 - monitor enabled, 0 - monitor disabled #if defined (du3GShieldUsage) #include "du3G_basic_lbr.h" #include "du3G_SMS_lbr.h" #else #include "cuGSM_basic_lbr.h" #include "cuGSM_SMS_lbr.h" #endif #if (ARDUINO >= 100) #include "Arduino.h" #include #else #include "WProgram.h" #include #endif #define UNO_MODE //used by modem library #define BUFFDSIZE 162 //used by modem library SoftwareSerial agsmSerial(2, 3); //RX==>2 ,TX soft==>3 char ch; char buffd[BUFFDSIZE]; char readBuffer[162]; //static char mBuffer[162]; int state=0, i=0, powerState = 0; int alarmState=0, prevalarmState=0; int getAlarmStatus = 0; void procRespose(char* msg){ Serial.println(msg);Serial.flush();delay(150); memset(readBuffer, 0x00, sizeof(readBuffer)); alarmState = digitalRead(magneticCONTACT); if(prevalarmState!= alarmState){ //debouce here delay(50); alarmState = digitalRead(magneticCONTACT); } if(alarmState == HIGH) sprintf(readBuffer, "%s - %s", msg, alarm/*"Alarm State"*/); else sprintf(readBuffer, "%s - %s", msg, nalarm/*"Normal State"*/); Serial.println(readBuffer);Serial.flush();delay(150); sendSMS(phoneNumber, readBuffer); //confirm command executed delay(100); } void setup(){ Serial.begin(19200); //start serial connection pinMode(magneticCONTACT, INPUT_PULLUP); //configure magneticCONTACT as an input PULL UP pinMode(13, OUTPUT); //configure the UNO D13 as output - UNO embedded LED used as contact status indicator agsmSerial.begin(9600); //next, modem setup clearagsmSerial(); clearSerial(); delay(10); modemHWSetup(); //configure UNO IN and OUT to be used with modem Serial.flush(); agsmSerial.flush(); delay(500); Serial.println(F("wait until c-uGSM/d-u3G is ready")); Serial.flush(); powerOnModem(); clearBUFFD(); while(strlen(buffd)<1){ getIMEI(); delay(500); } //Serial.println(F("modem is up. prepare it for SMS processing")); //Serial.flush(); ready4SMS = 0; ready4Voice = 0; //Serial.print(F("modem IMEI: ")); Serial.flush(); setupMODEMforSMSusage(); Serial.println(F("modem ready.. let's proceed")); Serial.flush(); delay(100); procRespose((char*)boot); alarmState = digitalRead(magneticCONTACT); prevalarmState = alarmState; supervisorMask = 1; } void loop(){ alarmState = digitalRead(magneticCONTACT); if(prevalarmState!= alarmState){ //debouce here delay(50); alarmState = digitalRead(magneticCONTACT); } if(getAlarmStatus != 0 || prevalarmState!= alarmState){ prevalarmState = alarmState; //copy alarmState for next cycle getAlarmStatus = 0; memset(readBuffer, 0x00, sizeof(readBuffer)); if (alarmState == HIGH) { if(supervisorMask != 0) digitalWrite(13, LOW); //show to LED sprintf(readBuffer, "%s", alarm/*"Alarm State"*/); } else { if(supervisorMask != 0) digitalWrite(13, HIGH); //show to LED sprintf(readBuffer, "%s", nalarm/*"Normal State"*/); } if(supervisorMask != 0){ sendSMS(phoneNumber,readBuffer); } Serial.println(readBuffer); Serial.flush(); delay(100); } /*process SMS commands - start*/ listSMS(); //find the last used SMS location clearagsmSerial(); int cnt; cnt = noSMS; while (cnt>0){ readSMS(cnt); //the SMS content will be returned in buffd /*here process SMS the content - start*/ Serial.print(F("SMS content: "));Serial.flush();delay(50); Serial.println(buffd);Serial.flush();delay(50); delay(50); clearagsmSerial(); delay(50); if(strlen(buffd) > 0){ //non empty SMS if(buffd[0] == '#' && buffd[1]== '1'){ //disable send SMS on magnetic contact event detection procRespose((char*)armed); supervisorMask = 1; //getAlarmStatus = 1; } else { if(buffd[0]== '#' && buffd[1]== '0'){ //disable send SMS on magnetic contact event detection procRespose((char*)unarmed); supervisorMask = 0; //getAlarmStatus = 1; }else{ //unknown command procRespose((char*)nunderstand); } } deleteSMS(cnt); //free the SMS location } /*here process SMS the content - end*/ clearBUFFD(); clearagsmSerial(); cnt--; } /*process SMS commands - stop*/ delay(20); }