Pages: 1 [2]   Go Down
Author Topic: arduino gsm door alarm  (Read 2762 times)
0 Members and 1 Guest are viewing this topic.
London-UK
Offline Offline
Full Member
***
Karma: 1
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
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

// 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
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

// 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.

1. open door
2. if after X seconds unit has not been turned of (one) SMS is sent.
3. if door closes and opens again
4. after X seconds (one) SMS is sent. etc

thanks again in advance smiley-sad-blue
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3741
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:
Code:
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

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;
    }
  }
}
Not tested, not even compiled.
Logged

London-UK
Offline Offline
Full Member
***
Karma: 1
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3741
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oops. Never checked SMSsent. Is this better?

Code:
// Now see if the door is open
if(!DoorIsClosed && !SMSSent)
Logged

Illinois, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mark

Have you accomplished your objectives in this project?
Quote
1. open door
2. if after X seconds unit has not been turned of (one) SMS is sent.
3. if door closes and opens again
4. after X seconds (one) SMS is sent. etc
Logged

Houston
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I took this thread and got it working. I do not have a GSM shield so replaced the SMS code with a simple LED connected to pin 8. Now when the door is opened longer than 10 seconds the LED will blink once. If the door is closed and then reopened the LED will blink again after 10 seconds.

Code:

 int doorContactPin = 2; // pin door contact is attached to
 int ledPin = 8; // led connected to pin 8 thru resistor to gnd
 unsigned long doorOpenTime = 0;
 unsigned long doorClosTime = 0;
 unsigned long time_threshold = 10000; //time in ms => 10 sec.
 int currDoorState = digitalRead(doorContactPin);
 int prevDoorState = digitalRead(doorContactPin); // Assumes that LOW means closed and door is closed when the Arduino starts
 boolean DoorIsClosed=true; // Let's assume it's closed for starting
 boolean SMSSent=false;     // Tells us whether we've sent an SMS for the latest instance of the door being open

void setup()

 pinMode(doorContactPin,INPUT);
 pinMode(ledPin, OUTPUT);
}


//next function was for sending an SMS but I didn't have one so replaced the code with a simple LED on/off
void sendSMS()
{
 digitalWrite(ledPin,HIGH);
 delay(500);
 digitalWrite(ledPin,LOW);
}

void loop()
{
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 && !SMSSent)
  {
  if(millis() - doorOpenTime >= time_threshold)
    {
//    digitalWrite(ledPin,HIGH);
//    delay(500);
//    digitalWrite(ledPin,LOW);
    sendSMS();
    SMSSent=true;
    }
  }
}
Logged

Pages: 1 [2]   Go Up
Jump to: