Hi Arduino wizards i have build a arduino based security system with a pir sensor, gyro, buzzer and a gsm module.
As for the project it works, i recieve notifications via sms when a sensor is trigerred the gsm module also connects fast in a couple of seconds to the network (from fast to slow blinking) it has a seperate 4.2v stable power supply with no voltage drops.
Now for the possible issue, the gsm module takes aprox 5min time to start sending notifications via sms is that a issue with my module? After its warmed up for 5min everything works fine with no delay. I did not find any info on my issue...
Module i use
.Code is not perfect, any improvements are welcome
//#include <DS1302.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
//DS1302 rtc(6, 5, 4);
int sensor = 13;
int val = 0;
bool sensorTriggered = false;
bool gyroTriggered = false;
int pirState = LOW; // we start, assuming no motion detected
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup()
{
pinMode(sensor, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
// Set the clock to run-mode, and disable the write protection
//rtc.halt(false);
//rtc.writeProtect(false);
Serial.begin(9600);
Serial.println("KRALJICA SYSTEM AKTIVAN");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Error Gyro BRE");
delay(1000);
}
mpu.setThreshold(1);
// Sets the time of your RTC module
// rtc.setDOW(WEDNESDAY);
// rtc.setTime(16, 36, 0); //Set the time 4:30 PM; Military Time
// rtc.setDate(01, 05, 2018); //dd, MM, yyyy format
}
void loop()
{
Vector rawGyro = mpu.readRawGyro();
Vector normGyro = mpu.readNormalizeGyro();
float normal_x = normGyro.XAxis;
float normal_y = normGyro.YAxis;
float normal_z = normGyro.ZAxis;
sensorTriggered = digitalRead(sensor) == HIGH;
gyroTriggered = abs(normal_x) + abs(normal_y) + abs(normal_z) > 0.2;
if (sensorTriggered && gyroTriggered) {
playSound();
sendMotionDetectionMsg("PIR I GYRO SENZOR AKTIVIRAN", "+381677067756");
} else if (sensorTriggered) {
playSound();
sendMotionDetectionMsg("PIR SENZOR AKTIVIRAN", "+381677067756");
} else if (gyroTriggered) {
playSound();
sendMotionDetectionMsg("GYRO SENZOR AKTIVIRAN", "+381677067756"); //send sms bre
}
delay(1000);
}
void playSound() {
playTone(300, 3000);
delay(150);
playTone(300, 3500);
delay(150);
playTone(300, 3000);
delay(150);
playTone(450, 3200);
delay(150);
}
void sendMotionDetectionMsg(String message, String phoneNumber) {
//String dowStr = rtc.getDOWStr();
//String dateStr = rtc.getDateStr();
//String timeStr = rtc.getTimeStr();
//String message = String("Kretanje Detektovano: "); //+ dowStr + String(" ") + dateStr + String(" ") + timeStr;
Serial.println(message);
//"+381644249159"
sendSms(message, phoneNumber);
}
void sendSms(String message, String number) {
mySerial.begin(9600);
delay(1000);
mySerial.println("AT");
delay(500);
mySerial.println("AT+CMGF=1");
delay(500);
mySerial.println("AT+CMGS=\""+ number +"\"\r");
delay(500);
mySerial.print(message);
delay(500);
mySerial.write(26);
}
//speaker
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}