#include <Wire.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
// LCD I2C configuration (address 0x27 typical)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Wi-Fi and Telegram Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BOTtoken "7223768065:AAENUdXAeRacGrA0SXacjFQhpz4WdvGT9po"
#define CHAT_ID "6750924550"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Pin declarations
const int ledPin = 2;
const int inputPin = 4;
const int buzzerPin = 13;
int pirState = LOW;
int value = 0;
void setup() {
Serial.begin(115200);
// LCD init
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
// TLS cert for Telegram (ESP32)
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int a = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
a++;
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("IP:");
lcd.print(WiFi.localIP());
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bot Ready...");
bot.sendMessage(CHAT_ID, "CONNECT TO BOT", "");
}
void loop() {
value = digitalRead(inputPin);
if (value == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
Serial.println("Motion detected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detected!");
lcd.setCursor(0, 1);
lcd.print("Sending Alert...");
// Buzzer pattern dengan tone
for (int i = 0; i < 2; i++) {
tone(buzzerPin, 1000); // 1000 Hz
delay(300);
noTone(buzzerPin);
delay(200);
}
delay(500);
for (int i = 0; i < 2; i++) {
tone(buzzerPin, 1000);
delay(300);
noTone(buzzerPin);
delay(200);
}
bot.sendMessage(CHAT_ID, "LED NYALA", "");
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW);
noTone(buzzerPin); // pastikan buzzer mati kalau tidak ada gerakan
if (pirState == HIGH) {
Serial.println("Motion ended!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Motion");
pirState = LOW;
}
}
}