Advertisement
safwan092

Untitled

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