Thanks, I have added your code to my Sketch. As with other codes, I get the SMS but I get more than one SMS. As long as the door is open I will get SMS every X seconds.
Heres my code
#include <SoftwareSerial.h>
SoftwareSerial module(2,3); // We need to create a serial port on D2/D3 to talk to the GSM module
char mobilenumber[] = "07xxxxxxxx4"; // Replace xxxxxxxx with the recipient's mobile number
int doorContactPin = 4; // pin door contact is attached to
unsigned long doorOpenTime = 0;
unsigned long doorClosTime = 0;
unsigned long time_threshold = 10000; //time in ms => 10 sec.
int currDoorState;
int prevDoorState; // Assumes that LOW means closed and that the door is closed when the Arduino starts
void setup()
{
module.begin(9600); //Initialize serial ports for communication.
delay(35000); // give the GSM module time to initialise, locate network etc.
pinMode(doorContactPin,INPUT);
digitalWrite(doorContactPin,LOW);
}
void sendSMS()
{
module.println("AT+CMGF=1"); // set SMS mode to text
module.print("AT+CMGS="); // now send message...
module.write(34); // ASCII equivalent of "
module.print(mobilenumber);
module.write(34); // ASCII equivalent of "
module.println();
delay(500); // give the module some thinking time
module.println("Unit Door Open"); // our message to send
module.write(26); // ASCII equivalent of Ctrl-Z
delay(15000); // the SMS module needs time to return to OK status
}
void loop()
{
static bool DoorIsClosed=true; // Let's assume it's closed for starting
static bool SMSSent=false; // Tells us whether we've sent an SMS for the latest instance of the door being open
int currDoorState = digitalRead(doorContactPin);
if(currDoorState != prevDoorState) // Door was closed and is now open or door was open and is now closed
{
if(currDoorState == LOW)
{
// Door is now closed
DoorIsClosed=true;
}
else
{
// Door is now open
DoorIsClosed=false;
doorOpenTime = millis();
}
SMSSent=false; // Door state changed, we may have a new opportunity to send SMS
}
prevDoorState = currDoorState;
// Now see if the door is open
if(!DoorIsClosed)
{
if(millis() - doorOpenTime >= time_threshold)
{
sendSMS();
SMSSent=true;
}
}
}
I have a basic set up comprising, 1 x reed switch connected to Pin 4 with other wire end going to GND. My GSM module is on Pins 2 and 3. The reed switch is closed when the magnet is close by and open when the magnet removed. hope this helps alittle.