Advertisement
safwan092

Untitled

May 9th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 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. const char* ssid = "Asdf";
  22. const char* password = "11127261";
  23.  
  24. // Initialize Telegram BOT
  25. String BOTtoken = "5393841147:AAHZFP5QcSuo6IGfMpVqDNSZ2J-Se1f_RqA"; // your Bot Token (Get from Botfather)
  26.  
  27. // Use @myidbot to find out the chat ID of an individual or a group
  28. // Also note that you need to click "start" on a bot before it can
  29. // message you
  30. String CHAT_ID = "983851904";
  31. //Group
  32. //"-657811937";
  33. //Your ID
  34. //"983851904";
  35.  
  36. bool sendPhoto = false;
  37.  
  38. WiFiClientSecure clientTCP;
  39. UniversalTelegramBot bot(BOTtoken, clientTCP);
  40.  
  41. #define camTrigger 12
  42. #define FLASH_LED_PIN 4
  43. bool flashState = LOW;
  44.  
  45. //Checks for new messages every 1 second.
  46. int botRequestDelay = 1000;
  47. unsigned long lastTimeBotRan;
  48.  
  49. //CAMERA_MODEL_AI_THINKER
  50. #define PWDN_GPIO_NUM 32
  51. #define RESET_GPIO_NUM -1
  52. #define XCLK_GPIO_NUM 0
  53. #define SIOD_GPIO_NUM 26
  54. #define SIOC_GPIO_NUM 27
  55.  
  56. #define Y9_GPIO_NUM 35
  57. #define Y8_GPIO_NUM 34
  58. #define Y7_GPIO_NUM 39
  59. #define Y6_GPIO_NUM 36
  60. #define Y5_GPIO_NUM 21
  61. #define Y4_GPIO_NUM 19
  62. #define Y3_GPIO_NUM 18
  63. #define Y2_GPIO_NUM 5
  64. #define VSYNC_GPIO_NUM 25
  65. #define HREF_GPIO_NUM 23
  66. #define PCLK_GPIO_NUM 22
  67.  
  68.  
  69. void configInitCamera() {
  70. camera_config_t config;
  71. config.ledc_channel = LEDC_CHANNEL_0;
  72. config.ledc_timer = LEDC_TIMER_0;
  73. config.pin_d0 = Y2_GPIO_NUM;
  74. config.pin_d1 = Y3_GPIO_NUM;
  75. config.pin_d2 = Y4_GPIO_NUM;
  76. config.pin_d3 = Y5_GPIO_NUM;
  77. config.pin_d4 = Y6_GPIO_NUM;
  78. config.pin_d5 = Y7_GPIO_NUM;
  79. config.pin_d6 = Y8_GPIO_NUM;
  80. config.pin_d7 = Y9_GPIO_NUM;
  81. config.pin_xclk = XCLK_GPIO_NUM;
  82. config.pin_pclk = PCLK_GPIO_NUM;
  83. config.pin_vsync = VSYNC_GPIO_NUM;
  84. config.pin_href = HREF_GPIO_NUM;
  85. config.pin_sscb_sda = SIOD_GPIO_NUM;
  86. config.pin_sscb_scl = SIOC_GPIO_NUM;
  87. config.pin_pwdn = PWDN_GPIO_NUM;
  88. config.pin_reset = RESET_GPIO_NUM;
  89. config.xclk_freq_hz = 20000000;
  90. config.pixel_format = PIXFORMAT_JPEG;
  91.  
  92. //init with high specs to pre-allocate larger buffers
  93. if (psramFound()) {
  94. config.frame_size = FRAMESIZE_UXGA;
  95. config.jpeg_quality = 10; //0-63 lower number means higher quality
  96. config.fb_count = 2;
  97. } else {
  98. config.frame_size = FRAMESIZE_SVGA;
  99. config.jpeg_quality = 12; //0-63 lower number means higher quality
  100. config.fb_count = 1;
  101. }
  102.  
  103. // camera init
  104. esp_err_t err = esp_camera_init(&config);
  105. if (err != ESP_OK) {
  106. Serial.printf("Camera init failed with error 0x%x", err);
  107. delay(1000);
  108. ESP.restart();
  109. }
  110.  
  111. // Drop down frame size for higher initial frame rate
  112. sensor_t * s = esp_camera_sensor_get();
  113. s->set_framesize(s, FRAMESIZE_CIF); // UXGA|SXGA|XGA|SVGA|VGA|CIF|QVGA|HQVGA|QQVGA
  114. }
  115.  
  116. void handleNewMessages(int numNewMessages) {
  117. Serial.print("Handle New Messages: ");
  118. Serial.println(numNewMessages);
  119.  
  120. for (int i = 0; i < numNewMessages; i++) {
  121. String chat_id = String(bot.messages[i].chat_id);
  122. if (chat_id != CHAT_ID) {
  123. bot.sendMessage(chat_id, "Unauthorized user", "");
  124. continue;
  125. }
  126.  
  127. // Print the received message
  128. String text = bot.messages[i].text;
  129. Serial.println(text);
  130.  
  131. String from_name = bot.messages[i].from_name;
  132. if (text == "/start") {
  133. String welcome = "Welcome , " + from_name + "\n";
  134. welcome += "Use the following commands to interact with the ESP32-CAM \n";
  135. welcome += "/photo : takes a new photo\n";
  136. welcome += "/flash : toggles flash LED \n";
  137. bot.sendMessage(CHAT_ID, welcome, "");
  138. }
  139. if (text == "/flash") {
  140. flashState = !flashState;
  141. digitalWrite(FLASH_LED_PIN, flashState);
  142. Serial.println("Change flash LED state");
  143. }
  144. if (text == "/photo") {
  145. sendPhoto = true;
  146. Serial.println("New photo request");
  147. }
  148. }
  149. }
  150.  
  151. String sendPhotoTelegram() {
  152. const char* myDomain = "api.telegram.org";
  153. String getAll = "";
  154. String getBody = "";
  155.  
  156. camera_fb_t * 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. uint16_t imageLen = fb->len;
  175. uint16_t extraLen = head.length() + tail.length();
  176. uint16_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. pinMode(camTrigger, INPUT);
  237. // Set LED Flash as output
  238. pinMode(FLASH_LED_PIN, OUTPUT);
  239. digitalWrite(FLASH_LED_PIN, flashState);
  240.  
  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. }
  259.  
  260. void loop() {
  261. if (sendPhoto || digitalRead(camTrigger)) {
  262. Serial.println("Preparing photo");
  263. sendPhotoTelegram();
  264. sendPhoto = false;
  265. }
  266. if (millis() > lastTimeBotRan + botRequestDelay) {
  267. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  268. while (numNewMessages) {
  269. Serial.println("got response");
  270. handleNewMessages(numNewMessages);
  271. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  272. }
  273. lastTimeBotRan = millis();
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement