So, i have messed around with the code. if I removed the sms block in the original code I dont get any SMS. So I have cut the code right down to this.
#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
// NEW CODE
unsigned long doorOpenTime = 0;
unsigned long doorClosTime = 0;
unsigned long time_threshold = 10000; //time in ms => 10 sec.
int currDoorState;
int prevDoorState = LOW; // 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()
{
//send sms message
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("Door Open Alert"); // our message to send
module.write(26); // ASCII equivalent of Ctrl-Z
delay(5000); // the SMS module needs time to return to OK status
}
void loop()
{
// ----------------------time delay before SMS is sent--------------------------
// telling millis not to start if prevDoorState == LOW
millis() == 0;
while((prevDoorState == LOW)&&(millis() < time_threshold)){
delay(100);// debounce
}
// if millis is longer than time_threshold, read the state of doorcontactpin and change currdoorstate should be HIGH
if (millis() >= time_threshold){
digitalRead(doorContactPin);
}
//---------------------new code -------------------------------------------------------------
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
doorClosTime = millis();
}
else
{
// Door is now open
doorOpenTime = millis();
}
}
prevDoorState = currDoorState;
//---------------------------- commented out incase re-use later---------------------------
// Now see if the door was opened
//if(doorOpenTime > 0);
// {
// It was. Was it closed?
// if(doorClosTime > 0);
// {
//Yes. How far apart?
// if(doorClosTime - doorOpenTime >= time_threshold)
// {
// sendSMS();
// doorOpenTime = 0;
// doorClosTime = 0;
// }
// }
// else
// {
//------------------------------------------------------------------------------------------
// Door is still open. How long has it been open?
if(millis() - doorOpenTime >= time_threshold)
{
sendSMS();
doorOpenTime = 0;
//doorClosTime = 0;
}
//} commented out
//} commented out
}
now I get SMS before the door is open..
So, I also load this code but dont get the delay from the time-threshold 10 sec. but do gey sms continuously
#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
// NEW CODE
unsigned long doorOpenTime = 0;
unsigned long doorClosTime = 0;
unsigned long time_threshold = 10000; //time in ms => 10 sec.
int currDoorState;
int prevDoorState = LOW; // 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()
{
//send sms message
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("Door Open Alert"); // our message to send
module.write(26); // ASCII equivalent of Ctrl-Z
delay(5000); // the SMS module needs time to return to OK status
}
void loop()
{
//new code
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
doorClosTime = millis();
}
else
{
// Door is now open
doorOpenTime = millis();
}
}
prevDoorState = currDoorState;
// Now see if the door was opened
if(doorOpenTime > 0)
{
// It was. Was it closed?
if(doorClosTime > 0)
{
//Yes. How far apart?
if(doorClosTime - doorOpenTime >= time_threshold)
{
sendSMS();
doorOpenTime = 0;
doorClosTime = 0;
}
}
else
{
// Door is still open. How long has it been open?
if(millis() - doorOpenTime >= time_threshold)
{
sendSMS();
doorOpenTime = 0;
doorClosTime = 0;
}
}
}
}
no matter what I do or Read about. I just cant get this to do what i want.
- open door
- if after X seconds unit has not been turned of (one) SMS is sent.
- if door closes and opens again
- after X seconds (one) SMS is sent. etc
thanks again in advance ![]()