#include <Wire.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// Wi-Fi and Telegram Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BOTtoken "7644389856:AAFQY9JpnVjhhfX_-eckZgmpLeTexd6ImFo"
#define CHAT_ID "6841230108"
#ifdef ESP8266
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
#endif
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Pin dan variabel
int ledPin = 2;
int inputPin = 4;
int pirState = LOW;
int vallue = 0;
// Servo
Servo myServo;
int servoPin = 13;
// LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C bisa berbeda (0x3F, 0x27, dll)
void setup() {
Serial.begin(115200);
// Koneksi WiFi
#ifdef ESP8266
configTime(0, 0, "pool.ntp.org");
client.setTrustAnchors(&cert);
#endif
#ifdef ESP32
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
#endif
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nWiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Setup Pin
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
// Setup Servo
myServo.attach(servoPin);
myServo.write(0); // posisi awal
// Setup LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("M. ZIQRILLAH 56");
lcd.setCursor(0, 1);
lcd.print("INFRA RED SENSOR");
// Telegram notifikasi awal
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop() {
vallue = digitalRead(inputPin);
if (vallue == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
Serial.println("MOTION DETECTION!");
bot.sendMessage(CHAT_ID, "MUSUH TERLIHAT , SENSOR IR ACTIVE", "");
// Tampilkan pesan di LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selamat Datang!");
delay(2000);
lcd.print("");
// Gerakkan servo
myServo.write(90); // ke posisi 90 derajat
delay(1000);
myServo.write(0); // kembali ke posisi semula
delay(500);
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW);
if (pirState == HIGH) {
Serial.println("NO MOTION DETECTION!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selamat Tinggal!");
myServo.write(0);
pirState = LOW;
}
}
}