This shows you the differences between two versions of the page.
iothings:proiecte:2022sric:surveillance [2023/06/02 07:03] ruxandra.grigorie |
iothings:proiecte:2022sric:surveillance [2023/06/02 09:57] (current) ruxandra.grigorie |
||
---|---|---|---|
Line 6: | Line 6: | ||
==== Project Description ==== | ==== Project Description ==== | ||
- | The purpose of this project is to have an ESP32CAM that can be remotely controlled to take and send a picture using a Telegram bot, and also to send a picture when motion is detected in front of the camera. | + | The purpose of this project is to have an ESP32CAM that can be remotely controlled to take and send a picture to the Telegram App, using a Telegram bot, and also to send a picture when motion is detected in front of the camera. |
==== Hardware Description ==== | ==== Hardware Description ==== | ||
Line 12: | Line 12: | ||
* **HC-SR04 ultrasonic sensor** | * **HC-SR04 ultrasonic sensor** | ||
* **5V power source** | * **5V power source** | ||
+ | |||
+ | {{:iothings:proiecte:2022sric:ruxandra_grigorie_eagle.png?400|}} | ||
+ | |||
+ | ==== Software Description ==== | ||
+ | The main loop of the program checks after a fixed period if any command from the Telegram bot was issued. If the "/photo" command was received, a new photo will be taken using the ESP32CAM and sent to the telegram bot API using a POST HTTP request. The main loop of the program also checks after a fixed period if the ultrasonic sensor detected movement, and if it did, it takes and sends a picture to the telegram bot API. | ||
+ | |||
+ | * **Telegram Bot init** | ||
+ | A Telegram bot can be easily created using the library **<UniversalTelegramBot.h>** The **chatId** corresponds to our own chat identification, and will be used in order to prevent commands being issued to the bot from other chats except ours. | ||
+ | <code> | ||
+ | // Universal Telegram chat identification | ||
+ | String chatId = "1531821904"; | ||
+ | // Universal Telegram bot identification | ||
+ | String BOTtoken = "6257850545:AAHI7y5Yo9qIcwc4EECumoDjSS0gVwmpHcA"; | ||
+ | UniversalTelegramBot bot(BOTtoken, clientTCP); | ||
+ | </code> | ||
+ | * **Main loop** | ||
+ | <code> | ||
+ | void loop(){ | ||
+ | |||
+ | if (sendPhoto){ | ||
+ | sendPhotoTelegram(); | ||
+ | digitalWrite(33, HIGH); | ||
+ | sendPhoto = false; | ||
+ | } | ||
+ | |||
+ | checkMotion(); | ||
+ | checkBotMessages(); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | * **Motion detection** | ||
+ | If the ultrasonic sensor detects motion, a picture is sent to the Telegram Bot after **motionDetectDelay** miliseconds. | ||
+ | <code> | ||
+ | void checkMotion() { | ||
+ | currentDistance = getDistance() + 10; | ||
+ | if (currentDistance < initialDistance) { | ||
+ | motionDetected = true; | ||
+ | } | ||
+ | if (millis() > lastTimeMeasured + motionDetectDelay) { | ||
+ | if (motionDetected) { | ||
+ | bot.sendMessage(chatId, "Motion detected!!", ""); | ||
+ | sendPhotoTelegram(); | ||
+ | motionDetected = false; | ||
+ | digitalWrite(33, LOW); | ||
+ | } | ||
+ | lastTimeMeasured = millis(); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | * **Check if command was issued from Telegram bot** | ||
+ | The code checks if a command was issued to the Telelgram bot, and if so, checks if the command was issued from the chat id that we configured as ours, in order to prevent others from issuing commands to our bot. If a valid command was issued, it is performed. | ||
+ | <code> | ||
+ | void handleNewMessages(int numNewMessages){ | ||
+ | for (int i = 0; i < numNewMessages; i++){ | ||
+ | // Chat id of the requester | ||
+ | String chat_id = String(bot.messages[i].chat_id); | ||
+ | if (chat_id != chatId){ | ||
+ | bot.sendMessage(chat_id, "Unauthorized user", ""); | ||
+ | continue; | ||
+ | } | ||
+ | | ||
+ | // Print the received message | ||
+ | String text = bot.messages[i].text; | ||
+ | // Serial.println(text); | ||
+ | |||
+ | String fromName = bot.messages[i].from_name; | ||
+ | |||
+ | if (text == "/photo") { | ||
+ | sendPhoto = true; | ||
+ | } | ||
+ | if (text == "/start"){ | ||
+ | String welcome = "Welcome to the ESP32-CAM Telegram bot.\n"; | ||
+ | welcome += "/photo : takes a new photo\n"; | ||
+ | welcome += "/readings : request sensor readings\n\n"; | ||
+ | welcome += "You'll receive a photo whenever motion is detected.\n"; | ||
+ | bot.sendMessage(chatId, welcome, "Markdown"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== Results ==== | ||
+ | * **Asamblat** | ||
+ | {{:iothings:proiecte:2022sric:ruxandra_grigorie_asamblat.jpg?400|}} | ||
+ | * **Bot Telegram** | ||
+ | {{:iothings:proiecte:2022sric:ruxandra_grigorie_bot.png?400|}} | ||
+ | |||
+ | ===== References ===== | ||
+ | * https://randomnerdtutorials.com/esp32-cam-ai-thinker-pinout/ | ||
+ | * https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ | ||
+ | * https://randomnerdtutorials.com/esp32-cam-shield-pcb-telegram/ |