#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Replace with your network credentials
// Initialize Telegram BOT
#define BOTtoken "7005018225:AAGHc1zUIprU8sWcUHgCQGfZKdx16Vo_3vs" // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
#define CHAT_ID "1619624781"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
const int motionSensor = 27; // PIR Motion Sensor pin
const int ledPin = 26; // LED pin
const int buzzerPin = 25; // Buzzer pin
bool motionDetected = false;
bool buzzerOn = false; // Flag to control the buzzer based on Telegram commands
// Function that triggers when motion is detected
void IRAM_ATTR detectsMovement() {
motionDetected = true;
}
void setup() {
Serial.begin(115200);
// Configure PIR sensor, LED, and Buzzer pins
pinMode(motionSensor, INPUT_PULLUP);
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buzzerPin, OUTPUT); // Set Buzzer pin as output
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
// Attempt to connect to WiFi network:
Serial.print("Connecting Wifi: ");
WiFi.mode(WIFI_STA);
WiFi.begin("Wokwi-GUEST", "", 6);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(800);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Send a startup message to the Telegram bot
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
Serial.println("Received message: " + text);
// Check if message is from the expected chat
if (chat_id == CHAT_ID) {
if (text == "/ring") {
bot.sendMessage(CHAT_ID, "Buzzer ringing...", "");
buzzerOn = true; // Set flag to keep buzzer ringing
} else if (text == "/stop") {
bot.sendMessage(CHAT_ID, "Buzzer stopped.", "");
buzzerOn = false; // Clear flag to stop buzzer
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off
} else {
bot.sendMessage(CHAT_ID, "Unknown command. Use /ring to start buzzer and /stop to stop it.", "");
}
}
}
}
void loop() {
if (motionDetected) {
// Send message to Telegram bot
bot.sendMessage(CHAT_ID, "Motion detected!!", "");
Serial.println("Motion Detected");
// Turn on the LED
digitalWrite(ledPin, HIGH);
delay(2000); // Keep LED on for 2 seconds
digitalWrite(ledPin, LOW); // Turn off LED
// Turn on the Buzzer for 1 second when motion is detected
digitalWrite(buzzerPin, HIGH); // Buzzer ON
delay(1000); // Keep buzzer on for 1 second
digitalWrite(buzzerPin, LOW); // Buzzer OFF
// Reset motionDetected flag
motionDetected = false;
}
// Check if the buzzer should be ringing (based on Telegram command)
if (buzzerOn) {
digitalWrite(buzzerPin, HIGH); // Keep the buzzer on
}
// Check for new Telegram messages
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages) {
handleNewMessages(numNewMessages);
}
delay(1000); // Add a small delay for stability
}