Here is the code.
#include <Messenger.h>
//#include <Time.h> // Clock Function
#include <NewSoftSerial.h> //Include the NewSoftSerial library to send serial commands to the cellular module.
#include <string.h> //Used for string manipulations
#include <Bounce.h> //Used to de-bounce the door switches
#include <Metro.h> //Timing function
// Instantiate Messenger object with the default separator (the space character)
Messenger message = Messenger(44);
char incoming_char=0; //Will hold the incoming character from the Serial Port.
NewSoftSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
//Door Inputs from 4 thru 12 (10 in total)
#define DoorInput1 4 // Door read switch connected to digital pin
#define BusyLed 12 // Red LED connected to digial pin 12
#define ReadyLed 13 // Green LED connected to digital pin 13
#define TestSMS 14 // Test button - Red Button on Analouge 0 (digital14)
//TIming of door hold and SMS sending
#define timing 5000 // Set the doorHoldOpen timing
#define timingRepeat 20000 // Set the time to reset a message.
#define TestTiming 5000 // Set the SMS Test time lockout.
// DEFINE SMS AND GSM SETTINGS
#define maxLength 200
char PHONE[ ] = "1234567890"; // Mobile number to send to
#define SMSTime 10 // How many minutes before sending the first SMS
#define SMSRepeat 15 // How many minuted before sending additional SMS messages.
//String inString = String(maxLength);
#define MAXSIZE 30
char string[MAXSIZE];
int SMS1 = 0;
int SMS0 = 0;
int SIND = 0;
int isActive1 = 0;
int isActive0 = 0;
int state1 = 0; // Open or Closed State of a Door
int state0 = 0; // Open or Closed state of the test button
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce door1 = Bounce( DoorInput1,5 );
Bounce test = Bounce( TestSMS, 5 );
Metro Door1Metro = Metro(timing, 1);
Metro TestSMSMetro = Metro(100, 1);
// Define messenger function
void messageCompleted() {
// This loop will echo each element of the message separately
while ( message.available() ) {
if ( message.checkString("OK") )
{
digitalWrite (ReadyLed, HIGH);
digitalWrite (BusyLed, LOW);
Serial.println("GSM Ready - OK");
}
if ( message.checkString("+CME ERROR: 4") )
{
Serial.println(string); // Echo the string
Serial.println("+CME ERROR:4 -operation not supported. This thing Sux");
digitalWrite (ReadyLed, HIGH);
digitalWrite (BusyLed, HIGH);
cell.println("AT+COPS?");
}
if ( message.checkString("+SIND: 4") )
{
Serial.println("SIM Ready");
bootOneTime();
}
message.copyString(string,MAXSIZE);
Serial.print(string); // Echo the string
Serial.println(); // Terminate the message with a carriage return
}
}
void sendSMSpreamble() {
//StatCheckGSM();
delay(1500);
cell.print("at+cmgs="); // comand message
cell.print(34,BYTE); // "
cell.print(PHONE); // Insert phone number
cell.println(34,BYTE); // "
Serial.println("preamble");
delay(1500);
}
void sendSMSappend() {
delay(500);
cell.println(26,BYTE); // end of message command Ctrl-z
Serial.println("append");
delay(5000);
isActive0 = 0; //Finished Sending
}
void sendErrorSMS() {
sendSMSpreamble();
cell.print("Error: ");
// cell.print(errorMsg[error]);
// writeStatus();
sendSMSappend();
}
void StatCheckGSM() {
cell.println("atz");
delay(1000);
}
void sendTestSMS() {
SetMode();
sendSMSpreamble();
sendStatus();
sendSMSappend();
}
void sendStatus() {
cell.print("Test SMS: ");
cell.println(SMS0);
Serial.println("message");
}
void sendStatusSMS() {
sendSMSpreamble();
//writeStatus();
sendSMSappend();
}
void SetMode() {
cell.println("at+cmgf=1"); //Set message mode to Text (1)
Serial.println("setmode");
}
void bootOneTime() {
digitalWrite (ReadyLed, LOW);
digitalWrite (BusyLed, HIGH);
cell.println("at+cmgf=1"); //Set message mode to Text (1)
delay(1000);
cell.println("AT+CSQ"); // Check signal strength
Serial.println("System Ready....");
digitalWrite (ReadyLed, HIGH);
cell.println("AT+COPS?");
}
void setup()
{
//Initialize serial ports for communication.
Serial.begin(9600);
cell.begin(9600);
// Attach the callback function to the Messenger
// message.attach(messageReady);
message.attach(messageCompleted);
pinMode(ReadyLed, OUTPUT); // sets the digital pin 13 as output GREEN
pinMode(BusyLed, OUTPUT); // sets the digital pin 12 as output RED
pinMode(DoorInput1, INPUT); // sets the pin as input
pinMode(TestSMS, INPUT); // sets the digital pin 14 as input
digitalWrite(DoorInput1, HIGH); // Activate Pull UP Inputs wire to GND
digitalWrite(TestSMS, HIGH); // Activate Pull UP Inputs wire to GND
Serial.println("System Start....");
int boot = 1;
delay(1000);
cell.println("AT+SBAND=4"); //Set the GSM to AU band 4
delay(1000);
}
void loop() {
// Test SMS button.
// Update the debouncers
test.update ( ); // Get the update value
if ( test.read() != state0 && test.duration() > 100 ) { // has our orientation changed for more the 100th a second?
state0 = test.read();
if ( state0 ) {
Serial.println( "SMS Test OFF" );
}
else {
Serial.println( "SMS Test ON" ); // Start the door timer to trigger an SMS
isActive0 = 1; //
digitalWrite (ReadyLed, LOW);
digitalWrite (BusyLed, HIGH);
}
}
int value0 = test.read();
if ( value0 == HIGH) // Push button are N/O :: High is a UN-pushed Button
{
TestSMSMetro.reset();
}
if ( isActive0 ) { // Send Test SMS
++SMS0;
Serial.print("Test SMS Trigger ");
Serial.println(SMS0);
sendTestSMS(); // Send Test SMS
TestSMSMetro.interval(TestTiming);
}
// Update the debouncers
door1.update ( ); // Get the update value
if ( door1.read() != state1 && door1.duration() > 500 ) { // has our orientation changed for more the 1/2 a second?
state1 = door1.read();
if ( state1 ) {
Serial.println( "Door 1 Closed!" );
isActive1 = 0;
Door1Metro.reset(); // Door 1 timer
}
else {
Serial.println( "Door 1 Open!" ); // Start the door timer to trigger an SMS
isActive1 = 1; //
Door1Metro.interval(timing);
}
}
int value1 = door1.read();
if ( value1 == HIGH) // Read switches are N/O :: High is a Closed Switch
{
Door1Metro.reset(); // Door 1 timer
}
if ( isActive1 && Door1Metro.check() ) {
++SMS1;
Serial.print("Door-1 Timing Trigger ");
Serial.println(SMS1);
Door1Metro.interval(timingRepeat);
}
// Send the restore SMS
if ( SMS1 >= 1 && isActive1 == 0 ) {
Serial.print("Door-1 Restore. Now Closed after "); // Do something only if interval has elapsed and the metro is "active"
Serial.print(SMS1);
Serial.println(" times.");
Door1Metro.interval(timing);
cell.println("at+cmgf=1");
Serial.println("at+cmgf=1");
delay (100);
cell.print("at+cmgs=");
Serial.println("at+cmgs=");
cell.print(34,BYTE);
cell.print(PHONE);
Serial.println(PHONE);
cell.println(34,BYTE);
//cell.print(13,BYTE);
cell.print("Door 1 Restore. Door 1 Now secure. Door trigger was looped ");
cell.print(SMS1);
cell.print(" times.");
cell.println(26,BYTE);
cell.print("ok");
SMS1 = 0;
}
//cell.println("at+cmgf=1");
//cell.print("at+cmgs=");
//cell.print(34,BYTE);
//cell.print(PHONE);
//cell.println(34,BYTE);
//cell.print("Your Message Here");
//cell.print(SMS1); //insert counter
//cell.print(26,BYTE);
// The following line is the most effective way of using Serial and Messenger's callback
while ( cell.available() ) message.process(cell.read () );
}
M