Hi everybody!
I'm working on a simple GSM Alarm based on a PIR + GPRS shield.
Basically the PIR sensor detects a (human) movement. After that an external interrupt is sent to wake up Arduino which is normally sleeping.
Finally, Arduino tells the GSM/GPRS Shield to send a text message.
Here comes the issue: when the first text message is sent the GPRS shield keeps sending messages in a loop.
The odd thing is that when I simulate the method call " SendTextMessage(); " with a simple print like : "now, I'll send a message" it works correctly and it goes to sleep after a while.
Am I missing something concerning the whole timing or something like that?
My code is posted below.
Thank you for your time
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/power.h>
#include <SoftwareSerial.h>
#define LEDPIN 13
#define PIRPIN 3
#define RX 7
#define TX 8
#define SERIAL_BAUD 19200
#define MY_SERIAL_BAUD 19200
#define SMS_DELAY 100
SoftwareSerial mySerial(RX,TX);
bool flag = false;
void setup() {
mySerial.begin(MY_SERIAL_BAUD);
Serial.begin(SERIAL_BAUD);
pinMode(PIRPIN,INPUT);
digitalWrite(PIRPIN, LOW);
pinMode(LEDPIN,OUTPUT);
//Signal PIR startup by flashing an LED
digitalWrite(LEDPIN,HIGH);
delay(500);
digitalWrite(LEDPIN,LOW);
delay(100);
Serial.println(F("END Setup"));
}
void loop()
{
Serial.println(F("START Loop"));
//Come out of sleep and read state of PIR pin
flag = digitalRead(PIRPIN);
if (flag == true) {
Serial.println(F("FLAG True"));
digitalWrite(LEDPIN,HIGH);
}
else
{
Serial.println(F("FLAG False"));
digitalWrite(LEDPIN,LOW);
}
sleepNow();
}
void sleepNow()
{
Serial.println(F("Sleep"));
//and attach handler:
attachInterrupt(digitalPinToInterrupt(PIRPIN), wakeUp, RISING);
delay(100);
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Set sleep enable (SE) bit
// Put the device to sleep
// Upon waking up, sketch continues from this point.
sleep_mode();
// Sending the text message: Alarm!
SendTextMessage();
}
void wakeUp()
{
detachInterrupt(digitalPinToInterrupt(PIRPIN));
Serial.println(F("Start Wakeup"));
}
void SendTextMessage()
{
Serial.println(F("Sending..sms"));
mySerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
//delay 100
delay(SMS_DELAY);
mySerial.println("AT + CMGS = \"+39XXXXXXXXXX\"");//send sms message, be careful need to add a country code before the cellphone number
delay(SMS_DELAY);
mySerial.println("Alarm!!");//the content of the message
delay(SMS_DELAY);
mySerial.println((char)26);//the ASCII code of the ctrl+z is 26
delay(500);
}