// EEE379 Computer System and Multimedia - Project
// Group 4
// Title: Automated Animal Intrusion Detection System
#define BLYNK_TEMPLATE_ID "TMPL6GQ9SvQDT"
#define BLYNK_TEMPLATE_NAME "EEE379"
#define BLYNK_AUTH_TOKEN "mipYwYqqBDrQw-lnvwcpqWlfPIcq9XH-"
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <ESP32Servo.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h>
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
//Initialize Pins Configuration
int servoPin1 = 25;
int servoPin2 = 17;
Servo servo1;
Servo servo2;
int ledPin = 26; // Pin LED is connected to
int piezoBuzzerPin = 13; // Pin Piezo Buzzer is connected to
int pirSensorPin=12; // PIN PIR Sensor is connected to
int echo = 18;
int trig = 5;
int seconds = 0;
long duration, distance;
//Setup WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* auth = BLYNK_AUTH_TOKEN;
//Setup Google Spreadsheet
String Sheet_Script_URL = "https://script.google.com/macros/s/AKfycbxmTxTbqOS44f64TDRIKVui8D8B6jC0DwUcRnJK9Z6VmMRp3sJWvWHiOe0ktnwFyKsNIA/exec";
unsigned long lastTriggerTime = 0; // Variable to store the last trigger time
const unsigned long triggerCooldown = 5000; // Cooldown period in milliseconds (e.g., 5 seconds)
int pos = 0;
void ConnectToWiFi()
{
Serial.print("Connecting to WiFi ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void triggerAlarm() {
//Light up LED in Blynk
Blynk.virtualWrite(V0, 1);
Serial.println("Motion Detected");
Blynk.logEvent("intrusion_alert");
// Activate buzzer
digitalWrite(ledPin, HIGH);
analogWrite(piezoBuzzerPin, 200);
delay(100);
analogWrite(piezoBuzzerPin, 25);
delay(100);
lcd.setCursor(1, 0);
lcd.print("Motion");
lcd.setCursor(8, 0);
lcd.print("Detected");
lcd.setCursor(1, 1);
lcd.print("ACTION TAKEN");
lcd.setCursor(1, 2);
lcd.print("Servo Activated");
for (pos = 0; pos <= 180; pos += 1) {
servo1.write(pos);
//delay(15);
servo2.write(pos);
delay(15);
}
for (pos = 180; pos <= 0; pos += 1) {
servo1.write(pos);
servo2.write(pos);
delay(15);
}
Blynk.virtualWrite(V0, 0);
}
long getUltrasonicDistance() {
// Measure distance
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) * 0.0346;
Blynk.virtualWrite(V1, distance);
return distance;
}
void sendData(String status) {
HTTPClient http;
String Send_Data_URL = Sheet_Script_URL + "?sts=write";
Send_Data_URL += "&motion=" + String(status);
Send_Data_URL += "&dist=" + String(distance);
Serial.println("Send data to Google Spreadsheet...");
Serial.print("URL : ");
Serial.println(Send_Data_URL);
// Getting response from google sheets.
if(http.begin(Send_Data_URL)) {
// Gets the HTTP status code.
int httpCode = http.GET();
Serial.print("Server response code: ");
Serial.println(httpCode);
http.end();
}
else Serial.println("Unable to connect to server");
}
BLYNK_WRITE(V2) {
int pinValue = param.asInt();
if(pinValue == 1) {
triggerAlarm();
sendData("Manual");
}
}
void setup() {
Serial.begin(115200);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(ledPin, OUTPUT); // Declare LED as output
pinMode(pirSensorPin, INPUT); // Declare the PIR sensor as input
pinMode(piezoBuzzerPin, OUTPUT); // Declare buzzer as output
Serial.begin(9600); // Set serial out if we want debugging
delay(800); // Allow time for the PIR Sensor to calibrate
ConnectToWiFi();
Blynk.begin(auth, ssid, password);
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("Distance");
lcd.setCursor(1, 1);
lcd.print("UltraSonic");
delay(2000);
lcd.clear();
servo1.attach(servoPin1, 500, 2500);
servo2.attach(servoPin2,500,2500);
}
void loop() {
Blynk.run();
long distance = getUltrasonicDistance();
lcd.clear();
lcd.setCursor(3, 3);
lcd.print(distance);
lcd.setCursor(6, 3);
lcd.print("cm");
// Check for motion
// Read the PIR sensor
if (digitalRead(pirSensorPin) == HIGH && distance <= 100) { // If motion detected and distance is <= 100 cm
unsigned long currentTime = millis(); // Get the current time
if (currentTime - lastTriggerTime >= triggerCooldown) { // Check if cooldown period has passed
triggerAlarm();
sendData("Detected");
lastTriggerTime = currentTime; // Update the last trigger time
}
}
analogWrite(piezoBuzzerPin, 0);
digitalWrite(ledPin, LOW);
delay(100); //delay to prevent bouncing
}