#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// SSID dan Password WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Token Telegram BOT dan Chat ID
#define BOTtoken "7111827908:AAFHy4wG3M26dLu9LmwElZYQiGwmQCvHDZs"
#define CHAT_ID "11933555"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
const int motionSensor1 = 27; // PIR Motion Sensor 1
const int motionSensor2 = 16; // PIR Motion Sensor 2
const int motionSensor3 = 17; // PIR Motion Sensor 3
bool motionDetected1 = false;
bool motionDetected2 = false;
bool motionDetected3 = false;
// Indicates when motion is detected for each sensor
void IRAM_ATTR detectsMovement1() {
motionDetected1 = true;
}
void IRAM_ATTR detectsMovement2() {
motionDetected2 = true;
}
void IRAM_ATTR detectsMovement3() {
motionDetected3 = true;
}
void setup() {
Serial.begin(115200);
// PIR Motion Sensors mode INPUT_PULLUP
pinMode(motionSensor1, INPUT_PULLUP);
pinMode(motionSensor2, INPUT_PULLUP);
pinMode(motionSensor3, INPUT_PULLUP);
// Set motionSensor pins as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor1), detectsMovement1, RISING);
attachInterrupt(digitalPinToInterrupt(motionSensor2), detectsMovement2, RISING);
attachInterrupt(digitalPinToInterrupt(motionSensor3), detectsMovement3, RISING);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop() {
if(motionDetected1){
bot.sendMessage(CHAT_ID, "Motion detected on sensor 1!!", "");
Serial.println("Motion Detected on Sensor 1");
motionDetected1 = false;
}
if(motionDetected2){
bot.sendMessage(CHAT_ID, "Motion detected on sensor 2!!", "");
Serial.println("Motion Detected on Sensor 2");
motionDetected2 = false;
}
if(motionDetected3){
bot.sendMessage(CHAT_ID, "Motion detected on sensor 3!!", "");
Serial.println("Motion Detected on Sensor 3");
motionDetected3 = false;
}
}