safwan092

Untitled

May 23rd, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 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. #include <TinyGPSPlus.h>
  21. #include <HardwareSerial.h>
  22. #include "cam_code.h"
  23. HardwareSerial SerialPort(1); // use UART1
  24.  
  25. #define buttonPin 13
  26. int butState = 0;
  27.  
  28. TinyGPSPlus gps;
  29. String stringMessage = "";
  30. String lonGPS = "";
  31. String latGPS = "";
  32. char Buf[70];
  33.  
  34. const char* ssid = "network";
  35. const char* password = "123456789";
  36.  
  37. // Initialize Telegram BOT
  38. String BOTtoken = "6040033498:AAF3PdrAwMaF8YF4XA1MR0gquGeTPY2vaPM"; // your Bot Token (Get from Botfather)
  39.  
  40. // Use @myidbot to find out the chat ID of an individual or a group
  41. // Also note that you need to click "start" on a bot before it can
  42. // message you
  43. String CHAT_ID = "506769933";
  44.  
  45. bool sendPhoto = false;
  46.  
  47. WiFiClientSecure clientTCP;
  48. UniversalTelegramBot bot(BOTtoken, clientTCP);
  49.  
  50. void handleNewMessages(int numNewMessages) {
  51. Serial.print("Handle New Messages: ");
  52. Serial.println(numNewMessages);
  53.  
  54. for (int i = 0; i < numNewMessages; i++) {
  55. String chat_id = String(bot.messages[i].chat_id);
  56. if (chat_id != CHAT_ID) {
  57. bot.sendMessage(chat_id, "Unauthorized user", "");
  58. continue;
  59. }
  60.  
  61. // Print the received message
  62. String text = bot.messages[i].text;
  63. Serial.println(text);
  64.  
  65. String from_name = bot.messages[i].from_name;
  66. if (text == "/start") {
  67. String welcome = "Welcome , " + from_name + "\n";
  68. welcome += "Use the following commands to interact with the ESP32-CAM \n";
  69. welcome += "/photo : takes a new photo\n";
  70. welcome += "/flash : toggles flash LED \n";
  71. bot.sendMessage(CHAT_ID, welcome, "");
  72. }
  73. if (text == "/flash") {
  74. flashState = !flashState;
  75. digitalWrite(FLASH_LED_PIN, flashState);
  76. Serial.println("Change flash LED state");
  77. }
  78. if (text == "/photo") {
  79. sendPhoto = true;
  80. Serial.println("New photo request");
  81. }
  82. }
  83. }
  84.  
  85. String sendPhotoTelegram() {
  86. const char* myDomain = "api.telegram.org";
  87. String getAll = "";
  88. String getBody = "";
  89.  
  90. camera_fb_t * fb = NULL;
  91. fb = esp_camera_fb_get();
  92. if (!fb) {
  93. Serial.println("Camera capture failed");
  94. delay(1000);
  95. ESP.restart();
  96. return "Camera capture failed";
  97. }
  98.  
  99. Serial.println("Connect to " + String(myDomain));
  100.  
  101.  
  102. if (clientTCP.connect(myDomain, 443)) {
  103. Serial.println("Connection successful");
  104.  
  105. 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";
  106. String tail = "\r\n--RandomNerdTutorials--\r\n";
  107.  
  108. uint16_t imageLen = fb->len;
  109. uint16_t extraLen = head.length() + tail.length();
  110. uint16_t totalLen = imageLen + extraLen;
  111.  
  112. clientTCP.println("POST /bot" + BOTtoken + "/sendPhoto HTTP/1.1");
  113. clientTCP.println("Host: " + String(myDomain));
  114. clientTCP.println("Content-Length: " + String(totalLen));
  115. clientTCP.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
  116. clientTCP.println();
  117. clientTCP.print(head);
  118.  
  119. uint8_t *fbBuf = fb->buf;
  120. size_t fbLen = fb->len;
  121. for (size_t n = 0; n < fbLen; n = n + 1024) {
  122. if (n + 1024 < fbLen) {
  123. clientTCP.write(fbBuf, 1024);
  124. fbBuf += 1024;
  125. }
  126. else if (fbLen % 1024 > 0) {
  127. size_t remainder = fbLen % 1024;
  128. clientTCP.write(fbBuf, remainder);
  129. }
  130. }
  131.  
  132. clientTCP.print(tail);
  133.  
  134. esp_camera_fb_return(fb);
  135.  
  136. int waitTime = 10000; // timeout 10 seconds
  137. long startTimer = millis();
  138. boolean state = false;
  139.  
  140. while ((startTimer + waitTime) > millis()) {
  141. Serial.print(".");
  142. delay(100);
  143. while (clientTCP.available()) {
  144. char c = clientTCP.read();
  145. if (state == true) getBody += String(c);
  146. if (c == '\n') {
  147. if (getAll.length() == 0) state = true;
  148. getAll = "";
  149. }
  150. else if (c != '\r')
  151. getAll += String(c);
  152. startTimer = millis();
  153. }
  154. if (getBody.length() > 0) break;
  155. }
  156. clientTCP.stop();
  157. Serial.println(getBody);
  158. }
  159. else {
  160. getBody = "Connected to api.telegram.org failed.";
  161. Serial.println("Connected to api.telegram.org failed.");
  162. }
  163. return getBody;
  164. }
  165. void setup() {
  166.  
  167. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  168. // Init Serial Monitor
  169. Serial.begin(115200);
  170. SerialPort.begin(9600, SERIAL_8N1, 14, 15);
  171. // Set LED Flash as output
  172. pinMode(buttonPin, INPUT);
  173. pinMode(FLASH_LED_PIN, OUTPUT);
  174. digitalWrite(FLASH_LED_PIN, flashState);
  175.  
  176. // Config and init the camera
  177. configInitCamera();
  178.  
  179. // Connect to Wi-Fi
  180. WiFi.mode(WIFI_STA);
  181. Serial.println();
  182. Serial.print("Connecting to ");
  183. Serial.println(ssid);
  184. WiFi.begin(ssid, password);
  185. clientTCP.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  186. while (WiFi.status() != WL_CONNECTED) {
  187. Serial.print(".");
  188. delay(500);
  189. }
  190. Serial.println();
  191. Serial.print("ESP32-CAM IP Address: ");
  192. Serial.println(WiFi.localIP());
  193. bot.sendMessage(CHAT_ID, "Project Online");
  194. }
  195.  
  196. void loop() {
  197. butState = digitalRead(buttonPin);
  198. Serial.println(butState);
  199. while (SerialPort.available() > 0)
  200. if (gps.encode(SerialPort.read()))
  201. displayInfo();
  202. if (millis() > 5000 && gps.charsProcessed() < 10) {
  203. Serial.println(F("No GPS detected: check wiring."));
  204. //while (true);
  205. }
  206. if (sendPhoto || !butState) {
  207. Serial.println("Preparing photo");
  208. sendPhotoTelegram();
  209. delay(500);
  210. bot.sendMessage(CHAT_ID, "Help");//Buf);
  211. delay(500);
  212. bot.sendMessage(CHAT_ID, Buf);
  213. sendPhoto = false;
  214. }
  215. /*
  216. if (millis() > lastTimeBotRan + botRequestDelay) {
  217. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  218. while (numNewMessages) {
  219. Serial.println("got response");
  220. handleNewMessages(numNewMessages);
  221. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  222. }
  223. lastTimeBotRan = millis();
  224. }
  225. */
  226. }
  227.  
  228.  
  229. void displayInfo() {
  230. Serial.print(F("Location: "));
  231. if (gps.location.isValid()) {
  232. lonGPS = String(gps.location.lng(), 6);
  233. latGPS = String(gps.location.lat(), 6);
  234. Serial.print("Lat: ");
  235. Serial.print(latGPS);
  236. Serial.print(F(","));
  237. Serial.print("Lng: ");
  238. Serial.print(lonGPS);
  239. Serial.println();
  240. stringMessage = "http://www.google.com/maps/place/" + latGPS + "," + lonGPS;
  241. stringMessage.toCharArray(Buf, 70);
  242. }
  243. else {
  244. Serial.print(F("INVALID"));
  245. stringMessage = "http://www.google.com/maps/place/26.376332,43.963880";
  246. stringMessage.toCharArray(Buf, 70);
  247. }
  248. }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment