Go Down

Topic: arduino gsm door alarm (Read 3 times) previous topic - next topic

mark7w

:~
I need some help with my arduino uno code. I am trying to get a sms sent to my mobile whenever the door is opened.
the goodnews is, that i can get it to send an sms when the door FIRST opens. but if i shut the door and open it again say 30 Seconds later, I dont get a new SMS.
The only way to get a new SMS sent is to re-set the Arduino. Am i missing something in my code. The code I have has be cut and paste from a couple of web sites.
at the moment when the Arduino is powered ON, There is a delay of 35 secs to allow to gsm to register on the network. Then if the door is opened for more than 3 sec, the sms is sent
I'm using and Arduino uno R3, Sparkfun SM5100B GSM, and a reed swith as the door contact. Here is the CODE

Code: [Select]


#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[] = "078xxxxxxx4";  // Replace xxxxxxxx with the recipient's mobile number

int sensor = 12; // pin sensor is attached to
int time = 0;
int time_threshold = 3000; //time in ms => 10 sec.
int val = 0;
int led = 11;




void setup()

  module.begin(9600); //Initialize serial ports for communication.
  pinMode(sensor,INPUT);
  pinMode(led,OUTPUT);
  digitalWrite(sensor,HIGH);


  delay(35000); // give the GSM module time to initialise, locate network etc.
}


void loop()
{
  digitalWrite(led,HIGH);
  val = digitalRead(sensor);
  while(val == LOW){
    val = digitalRead(sensor);
      }

  time = 0;
  while((val == HIGH)&&(time < time_threshold)){
    time = time + 100;
    val = digitalRead(sensor);
    delay(100);
    digitalWrite(led,HIGH);
   
   
  }
  if (time >= time_threshold){
    digitalWrite(led,LOW);
    for (int i=100;i>5000;i++);
   

    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
    module.flush();
    delay(10000); // the SMS module needs time to return to OK status
   
    do // You don't want to send out multiple SMSs.... or do you?
    {
      delay(1);
    }
    while (1>0);
   
  }
}

PaulS

Code: [Select]
int time_threshold = 3000; //time in ms => 10 sec.
Wrong. If you are going to have comments, make them match the code.

Code: [Select]
int sensor = 12; // pin sensor is attached to
Variable names for pins really should have pin somewhere in the name. Did you really go to Radio Shack (or equivalent) and ask for a sensor? Or did you buy a switch? Make the name reflect the actual thing connected to the pin.

Code: [Select]
    for (int i=100;i>5000;i++);
WTF?

Code: [Select]
    do // You don't want to send out multiple SMSs.... or do you?
    {
      delay(1);
    }
    while (1>0);

Well, this would be why you never get another SMS.

mark7w

points taken on board.

guess i dont need

"for (int i=100;i>5000;i++);"

if i remove this code should i expect to get SMS each time the door is opened?

"do // You don't want to send out multiple SMSs.... or do you?
    {
      delay(1);
    }
    while (1>0);"


PaulS

Quote
if i remove this code should i expect to get SMS each time the door is opened?

No. You should expect to get that over and over as long as the door is open.

Now, you need to learn about saving the previous state of a pin, and detect transitions. You want to send the message only when the switch pin transitions from released to pressed.

mark7w

Thanks, will learn as advised

Go Up