Advertisement
safwan092

Untitled

Jan 23rd, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. /*
  2. Rui Santos
  3. Complete project details at https://RandomNerdTutorials.com/telegram-esp32-cam-photo-arduino/
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files.
  7.  
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. */
  11.  
  12. #include <Arduino.h>
  13. #include <WiFi.h>
  14. #include <WiFiClientSecure.h>
  15. #include "soc/soc.h"
  16. #include "soc/rtc_cntl_reg.h"
  17. #include "esp_camera.h"
  18. #include <UniversalTelegramBot.h>
  19. #include <ArduinoJson.h>
  20.  
  21.  
  22. #define esp32TakePhotoPin 13
  23. const char* ssid = "network";
  24. const char* password = "123456789";
  25.  
  26. // Initialize Telegram BOT
  27. String BOTtoken = "6354347710:AAG9J9EmrvckMeuez426_X0rHg8i5daVAvg"; // your Bot Token (Get from Botfather)
  28.  
  29. // Use @myidbot to find out the chat ID of an individual or a group
  30. // Also note that you need to click "start" on a bot before it can
  31. // message you
  32. String CHAT_ID = "-4135819266";
  33.  
  34. bool sendPhoto = false;
  35.  
  36. WiFiClientSecure clientTCP;
  37. UniversalTelegramBot bot(BOTtoken, clientTCP);
  38.  
  39. #define FLASH_LED_PIN 4
  40. bool flashState = LOW;
  41.  
  42. //Checks for new messages every 1 second.
  43. int botRequestDelay = 100;
  44. unsigned long lastTimeBotRan;
  45.  
  46. //CAMERA_MODEL_AI_THINKER
  47. #define PWDN_GPIO_NUM 32
  48. #define RESET_GPIO_NUM -1
  49. #define XCLK_GPIO_NUM 0
  50. #define SIOD_GPIO_NUM 26
  51. #define SIOC_GPIO_NUM 27
  52.  
  53. #define Y9_GPIO_NUM 35
  54. #define Y8_GPIO_NUM 34
  55. #define Y7_GPIO_NUM 39
  56. #define Y6_GPIO_NUM 36
  57. #define Y5_GPIO_NUM 21
  58. #define Y4_GPIO_NUM 19
  59. #define Y3_GPIO_NUM 18
  60. #define Y2_GPIO_NUM 5
  61. #define VSYNC_GPIO_NUM 25
  62. #define HREF_GPIO_NUM 23
  63. #define PCLK_GPIO_NUM 22
  64.  
  65.  
  66. void configInitCamera() {
  67. camera_config_t config;
  68. config.ledc_channel = LEDC_CHANNEL_0;
  69. config.ledc_timer = LEDC_TIMER_0;
  70. config.pin_d0 = Y2_GPIO_NUM;
  71. config.pin_d1 = Y3_GPIO_NUM;
  72. config.pin_d2 = Y4_GPIO_NUM;
  73. config.pin_d3 = Y5_GPIO_NUM;
  74. config.pin_d4 = Y6_GPIO_NUM;
  75. config.pin_d5 = Y7_GPIO_NUM;
  76. config.pin_d6 = Y8_GPIO_NUM;
  77. config.pin_d7 = Y9_GPIO_NUM;
  78. config.pin_xclk = XCLK_GPIO_NUM;
  79. config.pin_pclk = PCLK_GPIO_NUM;
  80. config.pin_vsync = VSYNC_GPIO_NUM;
  81. config.pin_href = HREF_GPIO_NUM;
  82. config.pin_sscb_sda = SIOD_GPIO_NUM;
  83. config.pin_sscb_scl = SIOC_GPIO_NUM;
  84. config.pin_pwdn = PWDN_GPIO_NUM;
  85. config.pin_reset = RESET_GPIO_NUM;
  86. config.xclk_freq_hz = 20000000;
  87. config.pixel_format = PIXFORMAT_JPEG;
  88. config.grab_mode = CAMERA_GRAB_LATEST;
  89.  
  90. //init with high specs to pre-allocate larger buffers
  91. if (psramFound()) {
  92. config.frame_size = FRAMESIZE_UXGA;
  93. config.jpeg_quality = 10; //0-63 lower number means higher quality
  94. config.fb_count = 1;
  95. } else {
  96. config.frame_size = FRAMESIZE_SVGA;
  97. config.jpeg_quality = 12; //0-63 lower number means higher quality
  98. config.fb_count = 1;
  99. }
  100.  
  101. // camera init
  102. esp_err_t err = esp_camera_init(&config);
  103. if (err != ESP_OK) {
  104. Serial.printf("Camera init failed with error 0x%x", err);
  105. delay(1000);
  106. ESP.restart();
  107. }
  108. }
  109.  
  110. void handleNewMessages(int numNewMessages) {
  111. Serial.print("Handle New Messages: ");
  112. Serial.println(numNewMessages);
  113.  
  114. for (int i = 0; i < numNewMessages; i++) {
  115. String chat_id = String(bot.messages[i].chat_id);
  116. if (chat_id != CHAT_ID) {
  117. bot.sendMessage(chat_id, "Unauthorized user", "");
  118. continue;
  119. }
  120.  
  121. // Print the received message
  122. String text = bot.messages[i].text;
  123. Serial.println(text);
  124.  
  125. String from_name = bot.messages[i].from_name;
  126. if (text == "/start") {
  127. String welcome = "Welcome , " + from_name + "\n";
  128. welcome += "Use the following commands to interact with the ESP32-CAM \n";
  129. welcome += "/photo : takes a new photo\n";
  130. welcome += "/flash : toggles flash LED \n";
  131. bot.sendMessage(CHAT_ID, welcome, "");
  132. }
  133. if (text == "/flash") {
  134. flashState = !flashState;
  135. digitalWrite(FLASH_LED_PIN, flashState);
  136. Serial.println("Change flash LED state");
  137. }
  138. if (text == "/photo") {
  139. sendPhoto = true;
  140. Serial.println("New photo request");
  141. }
  142. }
  143. }
  144.  
  145. String sendPhotoTelegram() {
  146. const char* myDomain = "api.telegram.org";
  147. String getAll = "";
  148. String getBody = "";
  149.  
  150. //Dispose first picture because of bad quality
  151. camera_fb_t * fb = NULL;
  152. fb = esp_camera_fb_get();
  153. esp_camera_fb_return(fb); // dispose the buffered image
  154.  
  155. // Take a new photo
  156. fb = NULL;
  157. fb = esp_camera_fb_get();
  158. if (!fb) {
  159. Serial.println("Camera capture failed");
  160. delay(1000);
  161. ESP.restart();
  162. return "Camera capture failed";
  163. }
  164.  
  165. Serial.println("Connect to " + String(myDomain));
  166.  
  167.  
  168. if (clientTCP.connect(myDomain, 443)) {
  169. Serial.println("Connection successful");
  170.  
  171. String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"chat_id\"; \r\n\r\n" + CHAT_ID + "\r\n--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"esp32-cam.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
  172. String tail = "\r\n--RandomNerdTutorials--\r\n";
  173.  
  174. size_t imageLen = fb->len;
  175. size_t extraLen = head.length() + tail.length();
  176. size_t totalLen = imageLen + extraLen;
  177.  
  178. clientTCP.println("POST /bot" + BOTtoken + "/sendPhoto HTTP/1.1");
  179. clientTCP.println("Host: " + String(myDomain));
  180. clientTCP.println("Content-Length: " + String(totalLen));
  181. clientTCP.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
  182. clientTCP.println();
  183. clientTCP.print(head);
  184.  
  185. uint8_t *fbBuf = fb->buf;
  186. size_t fbLen = fb->len;
  187. for (size_t n = 0; n < fbLen; n = n + 1024) {
  188. if (n + 1024 < fbLen) {
  189. clientTCP.write(fbBuf, 1024);
  190. fbBuf += 1024;
  191. }
  192. else if (fbLen % 1024 > 0) {
  193. size_t remainder = fbLen % 1024;
  194. clientTCP.write(fbBuf, remainder);
  195. }
  196. }
  197.  
  198. clientTCP.print(tail);
  199.  
  200. esp_camera_fb_return(fb);
  201.  
  202. int waitTime = 10000; // timeout 10 seconds
  203. long startTimer = millis();
  204. boolean state = false;
  205.  
  206. while ((startTimer + waitTime) > millis()) {
  207. Serial.print(".");
  208. delay(100);
  209. while (clientTCP.available()) {
  210. char c = clientTCP.read();
  211. if (state == true) getBody += String(c);
  212. if (c == '\n') {
  213. if (getAll.length() == 0) state = true;
  214. getAll = "";
  215. }
  216. else if (c != '\r')
  217. getAll += String(c);
  218. startTimer = millis();
  219. }
  220. if (getBody.length() > 0) break;
  221. }
  222. clientTCP.stop();
  223. Serial.println(getBody);
  224. }
  225. else {
  226. getBody = "Connected to api.telegram.org failed.";
  227. Serial.println("Connected to api.telegram.org failed.");
  228. }
  229. return getBody;
  230. }
  231.  
  232. void setup() {
  233. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  234. // Init Serial Monitor
  235. Serial.begin(115200);
  236.  
  237. pinMode(esp32TakePhotoPin, INPUT);
  238. // Set LED Flash as output
  239. pinMode(FLASH_LED_PIN, OUTPUT);
  240. digitalWrite(FLASH_LED_PIN, flashState);
  241. // Config and init the camera
  242. configInitCamera();
  243.  
  244. // Connect to Wi-Fi
  245. WiFi.mode(WIFI_STA);
  246. Serial.println();
  247. Serial.print("Connecting to ");
  248. Serial.println(ssid);
  249. WiFi.begin(ssid, password);
  250. clientTCP.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  251. while (WiFi.status() != WL_CONNECTED) {
  252. Serial.print(".");
  253. delay(500);
  254. }
  255. Serial.println();
  256. Serial.print("ESP32-CAM IP Address: ");
  257. Serial.println(WiFi.localIP());
  258. bot.sendMessage(CHAT_ID, "Camera Bot Started");
  259. }
  260.  
  261. void loop() {
  262. int Sensor_reading = digitalRead(esp32TakePhotoPin);
  263. Serial.println(Sensor_reading);
  264. if (sendPhoto || Sensor_reading) {
  265. Serial.println("Preparing photo");
  266. bot.sendMessage(CHAT_ID, "Object detected!!");
  267. sendPhotoTelegram();
  268. sendPhoto = false;
  269. }
  270. if (millis() > lastTimeBotRan + botRequestDelay) {
  271. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  272. while (numNewMessages) {
  273. Serial.println("got response");
  274. handleNewMessages(numNewMessages);
  275. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  276. }
  277. lastTimeBotRan = millis();
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement