#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = " ";
// Replace with your Telegram bot token and chat ID
const String telegramToken = "YOUR_BOT_TOKEN";
const String chatId = "YOUR_CHAT_ID";
// PIR sensor pin
const int pirSensorPin = 23;
bool motionDetected = false;
void setup() {
Serial.begin(115200);
pinMode(pirSensorPin, INPUT); // Set PIR sensor pin as input
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
bool currentMotionStatus = digitalRead(pirSensorPin) == HIGH; // HIGH indicates motion detected
if (currentMotionStatus != motionDetected) {
motionDetected = currentMotionStatus;
if (motionDetected) {
sendTelegramNotification("Motion detected!");
} else {
sendTelegramNotification("Motion stopped.");
}
delay(10000); // Debounce delay
}
}
void sendTelegramNotification(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://api.telegram.org/bot" + telegramToken + "/sendMessage?chat_id=" + chatId + "&text=" + message;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
} else {
Serial.println("Error on HTTP request");
}
http.end();
} else {
Serial.println("Error: Not connected to WiFi");
}
}