Advertisement
safwan092

Untitled

Jan 11th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5. #include <ArduinoJson.h>
  6. #include <NTPClient.h>
  7. #include <WiFiUdp.h>
  8.  
  9. #define commandToESP32Camera 22
  10. #define triggerPin 23
  11.  
  12. // Define NTP Client to get time
  13. WiFiUDP ntpUDP;
  14. NTPClient timeClient(ntpUDP);
  15.  
  16. // Variables to save date and time
  17. String formattedDate;
  18. String dayStamp;
  19. String timeStamp;
  20.  
  21. char google_maps_link[70];
  22. String google_maps_link_string = "";
  23. String Longitude = "1";
  24. String Latitude = "1";
  25.  
  26. unsigned long dataMillis = 0;
  27. int count = 0;
  28.  
  29. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  30. unsigned long bot_lasttime; // last time messages' scan has been done
  31.  
  32. // Wifi network station credentials
  33. #define WIFI_SSID "yaseen1421"
  34. #define WIFI_PASSWORD "yaseen1421"
  35. // Telegram BOT Token (Get from Botfather)
  36. #define BOT_TOKEN "6890911094:AAGIdkiGi-Uwdw1S0d4yZh4uqBMvZ3iac2U"
  37.  
  38. // Use @myidbot (IDBot) to find out the chat ID of an individual or a group
  39. // Also note that you need to click "start" on a bot before it can
  40. // message you
  41. #define CHAT_ID "-1002022958868"
  42.  
  43. TinyGPSPlus gps;
  44. WiFiClientSecure secured_client;
  45. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  46.  
  47.  
  48.  
  49. void handleNewMessages(int numNewMessages)
  50. {
  51. Serial.println("handleNewMessages");
  52. Serial.println(String(numNewMessages));
  53.  
  54. for (int i = 0; i < numNewMessages; i++)
  55. {
  56. String chat_id = bot.messages[i].chat_id;
  57. String text = bot.messages[i].text;
  58.  
  59. String from_name = bot.messages[i].from_name;
  60. if (from_name == "")
  61. from_name = "Guest";
  62.  
  63. if (text == "/Test") {
  64. bot.sendMessage(CHAT_ID, "Bot Test [OK]");
  65. }
  66.  
  67. if (text == "/start")
  68. {
  69. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  70. welcome += "This is Chat Action Bot example.\n\n";
  71. bot.sendMessage(chat_id, welcome);
  72. }
  73. }
  74. }
  75.  
  76.  
  77. void setup() {
  78. Serial.begin(9600);
  79. Serial2.begin(9600);
  80.  
  81. pinMode(triggerPin, INPUT_PULLUP);
  82. pinMode(commandToESP32Camera, OUTPUT);
  83. digitalWrite(commandToESP32Camera, HIGH);
  84. // attempt to connect to Wifi network:
  85. Serial.print("Connecting to Wifi SSID ");
  86. Serial.print(WIFI_SSID);
  87. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  88. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  89. while (WiFi.status() != WL_CONNECTED)
  90. {
  91. Serial.print(".");
  92. delay(500);
  93. }
  94. Serial.print("\nWiFi connected. IP address: ");
  95. Serial.println(WiFi.localIP());
  96.  
  97. Serial.print("Retrieving time: ");
  98. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  99. time_t now = time(nullptr);
  100. while (now < 24 * 3600)
  101. {
  102. Serial.print(".");
  103. delay(100);
  104. now = time(nullptr);
  105. }
  106. Serial.println(now);
  107.  
  108. bot.sendMessage(CHAT_ID, "Bot started up");
  109. delay(1000);
  110. Serial.println("delay finished");
  111.  
  112. // Initialize a NTPClient to get time
  113. timeClient.begin();
  114. // Set offset time in seconds to adjust for your timezone, for example:
  115. // GMT +1 = 3600
  116. // GMT +8 = 28800
  117. // GMT -1 = -3600
  118. // GMT 0 = 0
  119. timeClient.setTimeOffset(10800);
  120. }
  121.  
  122. void loop() {
  123. read_GPS_receiver();
  124.  
  125. //Weapon Out => triggerState = 0
  126. //Weapon In => triggerState = 1
  127. int triggerState = digitalRead(triggerPin);
  128. Serial.println(triggerState);
  129.  
  130. while (!timeClient.update()) {
  131. timeClient.forceUpdate();
  132. }
  133. // The formattedDate comes with the following format:
  134. // 2018-05-28T16:00:13Z
  135. // We need to extract date and time
  136. formattedDate = timeClient.getFormattedDate();
  137. Serial.println(formattedDate);
  138. // Extract date
  139. int splitT = formattedDate.indexOf("T");
  140. dayStamp = formattedDate.substring(0, splitT);
  141. Serial.print("DATE: ");
  142. Serial.println(dayStamp);
  143. // Extract time
  144. timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1);
  145. Serial.print("HOUR: ");
  146. Serial.println(timeStamp);
  147. /*
  148. if (millis() - bot_lasttime > BOT_MTBS)
  149. {
  150. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  151.  
  152. while (numNewMessages)
  153. {
  154. Serial.println("got response");
  155. handleNewMessages(numNewMessages);
  156. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  157. }
  158.  
  159. bot_lasttime = millis();
  160. }
  161. */
  162. if (triggerState == 0) {
  163. //Send Command to ESP32-Cam to take Photo
  164. digitalWrite(commandToESP32Camera, LOW);
  165. //Send GPS Location
  166. String textToSend = "Weapon is OUT\n\n";
  167. textToSend += "DATE: ";
  168. textToSend += dayStamp;
  169. textToSend += "\nTime: ";
  170. textToSend += timeStamp;
  171. textToSend += "\n\n";
  172. textToSend += google_maps_link;
  173. bot.sendMessage(CHAT_ID, textToSend);
  174. delay(5000);
  175. }
  176. else {
  177. digitalWrite(commandToESP32Camera, HIGH);
  178. }
  179.  
  180. }
  181.  
  182. void read_GPS_receiver() {
  183. while (Serial2.available() > 0) {
  184. if (gps.encode(Serial2.read())) {
  185. if (gps.location.isValid()) {
  186. Latitude = String(gps.location.lat(), 6);
  187. Longitude = String(gps.location.lng(), 6);
  188. Serial.print(Latitude);
  189. Serial.print(",");
  190. Serial.print(Longitude);
  191. Serial.println();
  192. google_maps_link_string = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + Latitude + "+" + Longitude;
  193. google_maps_link_string.toCharArray(google_maps_link, 70);
  194. }
  195. else {
  196. Serial.println(F("INVALID"));
  197. Latitude = "1.23";
  198. Longitude = "1.23";
  199. google_maps_link_string = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + Latitude + "+" + Longitude;
  200. google_maps_link_string.toCharArray(google_maps_link, 70);
  201. }
  202. }
  203. }
  204. if (millis() > 5000 && gps.charsProcessed() < 10) {
  205. Serial.println(F("No GPS detected: check wiring."));
  206. while (true);
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement