#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include "time.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define PIR_PIN 14
#define LED_PIN 2
#define BUZZER_PIN 18
#define SERVO_PIN 25
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String firebaseURL = "https://realtime-database-d6755-default-rtdb.firebaseio.com/intrusions.json";
String BOT_TOKEN = "8053069579:AAG1mejRaKhKYsACOfPKwedwnism00rvo-A";
String CHAT_ID = "7241417929";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 19800;
const int daylightOffset_sec = 0;
bool intrusionSent = false;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myServo;
// ===== GET DATE & TIME =====
String getFormattedDateTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return "Time_Error";
}
char buffer[40];
strftime(buffer, sizeof(buffer), "%d-%m-%Y %I:%M:%S %p", &timeinfo);
return String(buffer);
}
// ===== FIREBASE =====
void sendToFirebase(String dateTime) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient https;
https.begin(client, firebaseURL);
https.addHeader("Content-Type", "application/json");
String jsonData = "{\"status\":\"Intrusion Detected\",\"datetime\":\"" + dateTime + "\"}";
int responseCode = https.POST(jsonData);
Serial.print("Firebase Response: ");
Serial.println(responseCode);
https.end();
}
// ===== TELEGRAM =====
void sendTelegram(String dateTime) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient https;
String message = "INTRUSION ALERT - " + dateTime;
String encodedMessage = "";
char c;
char buf[4];
for (int i = 0; i < message.length(); i++) {
c = message.charAt(i);
if (isalnum(c)) {
encodedMessage += c;
} else {
sprintf(buf, "%%%02X", c);
encodedMessage += buf;
}
}
String url = "https://api.telegram.org/bot" + BOT_TOKEN +
"/sendMessage?chat_id=" + CHAT_ID +
"&text=" + encodedMessage;
https.begin(client, url);
int responseCode = https.GET();
Serial.print("Telegram Response: ");
Serial.println(responseCode);
https.end();
}
// ===== SETUP =====
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
myServo.attach(SERVO_PIN);
myServo.write(0);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("System Starting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
delay(2000);
lcd.clear();
lcd.print("System Ready");
}
// ===== LOOP =====
void loop() {
int motion = digitalRead(PIR_PIN);
if (motion == HIGH && !intrusionSent) {
String currentDateTime = getFormattedDateTime();
Serial.println("Motion Detected!");
Serial.println(currentDateTime);
// 🔴 Activate Devices
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
myServo.write(90);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("INTRUSION!");
lcd.setCursor(0,1);
lcd.print(currentDateTime.substring(11)); // show time only
sendToFirebase(currentDateTime);
sendTelegram(currentDateTime);
intrusionSent = true;
}
if (motion == LOW) {
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
myServo.write(0);
intrusionSent = false;
}
delay(500);
}