Advertisement
safwan092

Untitled

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