#include <Arduino.h>
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/telegram-esp32-motion-detection-arduino/
Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Initialize Telegram BOT
#define BOTtoken "xxxxx" // your Bot Token (Get from Botfather)
// Dùng ChatGPT để nhờ hướng dẫn tìm giá trị GROUP_ID này
#define GROUP_ID "group_chatid" //thường là một số âm
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
const int motionSensor = 27; // PIR Motion Sensor
bool motionDetected = false;
//Định dạng chuỗi %s,%d,...
String StringFormat(const char* fmt, ...){
va_list vaArgs;
va_start(vaArgs, fmt);
va_list vaArgsCopy;
va_copy(vaArgsCopy, vaArgs);
const int iLen = vsnprintf(NULL, 0, fmt, vaArgsCopy);
va_end(vaArgsCopy);
int iSize = iLen + 1;
char* buff = (char*)malloc(iSize);
vsnprintf(buff, iSize, fmt, vaArgs);
va_end(vaArgs);
String s = buff;
free(buff);
return String(s);
}
// Indicates when motion is detected
void IRAM_ATTR detectsMovement() {
//Serial.println("MOTION DETECTED!!!");
motionDetected = true;
}
void setup() {
Serial.begin(115200);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// 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: ");
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(100);
}
Serial.println("");
Serial.println("WiFi connected");
bot.sendMessage(GROUP_ID, "IoT Developer started up");
}
void loop() {
static uint count_ = 0;
if(motionDetected){
++count_;
Serial.print(count_);Serial.println(". MOTION DETECTED => Waiting to send to Telegram");
String msg = StringFormat("%u => Motion detected!",count_);
bot.sendMessage(GROUP_ID, msg.c_str());
Serial.print(count_);Serial.println(". Sent successfully to Telegram: Motion Detected");
motionDetected = false;
}
}