0% found this document useful (0 votes)
8 views

Code

This document describes an Arduino sketch that uses sensors and modules to detect intrusions, trigger an alarm, capture an image, get GPS coordinates, and send SMS messages with the details.

Uploaded by

moskovbringer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Code

This document describes an Arduino sketch that uses sensors and modules to detect intrusions, trigger an alarm, capture an image, get GPS coordinates, and send SMS messages with the details.

Uploaded by

moskovbringer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <SoftwareSerial.

h>
#include <TinyGPS++.h>

// Pin definitions
#define MOTION_SENSOR_PIN 2
#define BUZZER_PIN 3
#define DOOR_SWITCH_PIN 4
#define RED_LED_PIN 5
#define BLUE_LED_PIN 6
#define CAMERA_TRIGGER_PIN 7 // Pin to trigger image capture

// GSM module settings


#define GSM_RX_PIN 8
#define GSM_TX_PIN 9
#define GSM_RESET_PIN 10
#define GSM_BAUD_RATE 9600
#define RECIPIENT_NUMBER "+1234567890" // Replace with your recipient number

// GPS module settings


#define GPS_RX_PIN 11
#define GPS_TX_PIN 12
#define GPS_BAUD_RATE 9600

// Time thresholds
#define MOTION_TIMEOUT_MS 5000 // 5 seconds
#define DOOR_TIMEOUT_MS 5000 // 5 seconds

// Initialize objects
SoftwareSerial gsmSerial(GSM_RX_PIN, GSM_TX_PIN);
TinyGPSPlus gps;

// Function prototypes
void sendSMS(String message);
void activateAlarm();
void deactivateAlarm();
void captureImage();

// Variables to track sensor states


bool motionDetected = false;
bool doorOpened = false;

// Timestamps for sensor triggers


unsigned long motionTriggerTime = 0;
unsigned long doorTriggerTime = 0;
void setup() {
pinMode(MOTION_SENSOR_PIN, INPUT);
pinMode(DOOR_SWITCH_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(CAMERA_TRIGGER_PIN, OUTPUT);

// Initialize GSM module


gsmSerial.begin(GSM_BAUD_RATE);
gsmSerial.println("AT+CMGF=1"); // Set SMS mode to text
delay(1000);

// Initialize GPS module


Serial.begin(GPS_BAUD_RATE);
}

void loop() {
// Check motion sensor
if (digitalRead(MOTION_SENSOR_PIN) == HIGH) {
motionDetected = true;
motionTriggerTime = millis();
} else if (millis() - motionTriggerTime > MOTION_TIMEOUT_MS) {
motionDetected = false;
}

// Check door switch


if (digitalRead(DOOR_SWITCH_PIN) == HIGH) {
doorOpened = true;
doorTriggerTime = millis();
} else if (millis() - doorTriggerTime > DOOR_TIMEOUT_MS) {
doorOpened = false;
}

// Intrusion detection
if (motionDetected && doorOpened) {
activateAlarm();
String message = "Intruder detected at your property!";
sendSMS(message);
captureImage();
delay(5000);
}

// Check GPS data


while (gps.available() > 0) {
if (gps.location.isValid()) {
String message = "Your property is located at: " +
String(gps.location.lat(), 6) + ", " +
String(gps.location.lng(), 6 + "," + String(gps.date.month()) + "/" +
String(gps.date.day()) + "/" +
String(gps.date.year() + "," + String(gps.time.hour()) + ":" +
String(gps.time.minute()) + ":" + String(gps.time.second());
sendSMS(message);
}
}
}

void sendSMS(String message) {


gsmSerial.print("AT+CMGS=\"");
gsmSerial.print(RECIPIENT_NUMBER);
gsmSerial.println("\"");
delay(1000);
gsmSerial.println(message);
gsmSerial.println((char)26);
delay(1000);
}

void activateAlarm() {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, HIGH);
delay(1000);
}

void deactivateAlarm() {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
}

void captureImage() {
digitalWrite(CAMERA_TRIGGER_PIN, HIGH);
delay(100);
digitalWrite(CAMERA_TRIGGER_PIN, LOW);
}

You might also like