This shows you the differences between two versions of the page.
iothings:proiecte:2022sric:surveillance [2023/06/02 07:53] 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 16: | Line 16: | ||
==== Software Description ==== | ==== 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. The main loop of the program also after a fixed period checks if the ultrasonic sensor detected movement, and if it did, it takes and sends a picture to the telegram bot API. | + | 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** | * **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> | <code> | ||
// Universal Telegram chat identification | // Universal Telegram chat identification | ||
Line 42: | Line 43: | ||
* **Motion detection** | * **Motion detection** | ||
+ | If the ultrasonic sensor detects motion, a picture is sent to the Telegram Bot after **motionDetectDelay** miliseconds. | ||
<code> | <code> | ||
void checkMotion() { | void checkMotion() { | ||
Line 59: | Line 61: | ||
} | } | ||
</code> | </code> | ||
- | + | * **Check if command was issued from Telegram bot** | |
- | * **Telegram bot picture send** | + | 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> | <code> | ||
- | String sendPhotoTelegram(){ | + | void handleNewMessages(int numNewMessages){ |
- | const char* myDomain = "api.telegram.org"; | + | for (int i = 0; i < numNewMessages; i++){ |
- | String getAll = ""; | + | // Chat id of the requester |
- | String getBody = ""; | + | 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); | ||
- | camera_fb_t * fb = NULL; | + | String fromName = bot.messages[i].from_name; |
- | fb = esp_camera_fb_get(); | + | |
- | if(!fb) { | + | |
- | delay(1000); | + | |
- | ESP.restart(); | + | |
- | return "Camera capture failed"; | + | |
- | } | + | |
- | + | ||
- | if (clientTCP.connect(myDomain, 443)) { | + | |
- | String head = "--CSIoT\r\nContent-Disposition: form-data; name=\"chat_id\"; \r\n\r\n" + chatId + "\r\n--CSIoT\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"esp32-cam.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"; | + | |
- | String tail = "\r\n--CSIoT--\r\n"; | + | |
- | uint16_t imageLen = fb->len; | + | if (text == "/photo") { |
- | uint16_t extraLen = head.length() + tail.length(); | + | sendPhoto = true; |
- | uint16_t totalLen = imageLen + extraLen; | + | } |
- | + | if (text == "/start"){ | |
- | clientTCP.println("POST /bot"+BOTtoken+"/sendPhoto HTTP/1.1"); | + | String welcome = "Welcome to the ESP32-CAM Telegram bot.\n"; |
- | clientTCP.println("Host: " + String(myDomain)); | + | welcome += "/photo : takes a new photo\n"; |
- | clientTCP.println("Content-Length: " + String(totalLen)); | + | welcome += "/readings : request sensor readings\n\n"; |
- | clientTCP.println("Content-Type: multipart/form-data; boundary=CSIoT"); | + | welcome += "You'll receive a photo whenever motion is detected.\n"; |
- | clientTCP.println(); | + | bot.sendMessage(chatId, welcome, "Markdown"); |
- | clientTCP.print(head); | + | |
- | + | ||
- | uint8_t *fbBuf = fb->buf; | + | |
- | size_t fbLen = fb->len; | + | |
- | for (size_t n=0;n<fbLen;n=n+1024) { | + | |
- | if (n+1024<fbLen) { | + | |
- | clientTCP.write(fbBuf, 1024); | + | |
- | fbBuf += 1024; | + | |
- | } | + | |
- | else if (fbLen%1024>0) { | + | |
- | size_t remainder = fbLen%1024; | + | |
- | clientTCP.write(fbBuf, remainder); | + | |
- | } | + | |
- | } | + | |
- | | + | |
- | clientTCP.print(tail); | + | |
- | + | ||
- | esp_camera_fb_return(fb); | + | |
- | + | ||
- | int waitTime = 10000; | + | |
- | long startTimer = millis(); | + | |
- | boolean state = false; | + | |
- | + | ||
- | while ((startTimer + waitTime) > millis()){ | + | |
- | delay(100); | + | |
- | while (clientTCP.available()) { | + | |
- | char c = clientTCP.read(); | + | |
- | if (state==true) getBody += String(c); | + | |
- | if (c == '\n') { | + | |
- | if (getAll.length()==0) state=true; | + | |
- | getAll = ""; | + | |
- | } | + | |
- | else if (c != '\r') | + | |
- | getAll += String(c); | + | |
- | startTimer = millis(); | + | |
- | } | + | |
- | if (getBody.length()>0) break; | + | |
} | } | ||
- | clientTCP.stop(); | ||
} | } | ||
- | else { | ||
- | getBody="Connected to api.telegram.org failed."; | ||
- | } | ||
- | return getBody; | ||
} | } | ||
</code> | </code> | ||
+ | |||
==== Results ==== | ==== Results ==== | ||
Line 140: | Line 99: | ||
* **Bot Telegram** | * **Bot Telegram** | ||
{{:iothings:proiecte:2022sric:ruxandra_grigorie_bot.png?400|}} | {{: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/ |