Advertisement
safwan092

Untitled

Oct 22nd, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #include <ArduinoJson.h>
  5. #include <TinyGPSPlus.h>
  6.  
  7. #define WIFI_SSID "Hotel Net"
  8. #define WIFI_PASSWORD "0549084774"
  9. #define BOT_TOKEN "6630308049:AAHCPpjtIkrZspQyhFJNgFkNdtWAIyv3fns"
  10. #define CHAT_ID "-1002087168221"
  11.  
  12. #define Rpi_ESP32_GPS_Pin 21
  13. #define ESP32_Rpi_Door_Pin 22
  14.  
  15. const unsigned long BOT_MTBS = 100;
  16. unsigned long bot_lasttime;
  17. char google_maps_link[70];
  18. String google_maps_link_string = "";
  19. String Longitude = "39.241978";
  20. String Latitude = "21.495031";
  21. int Rpi_ESP32_GPS_Value = 0;
  22.  
  23.  
  24. TinyGPSPlus gps;
  25. WiFiClientSecure secured_client;
  26. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  27.  
  28. void setup() {
  29. setup_input_output();
  30. connect_wifi_telegram();
  31. bot.sendMessage(CHAT_ID, "Bot Online");
  32. }
  33.  
  34.  
  35. void loop() {
  36.  
  37. Rpi_ESP32_GPS_Value = digitalRead(Rpi_ESP32_GPS_Pin);
  38. if (Rpi_ESP32_GPS_Value) {
  39. bot.sendMessage(CHAT_ID, google_maps_link_string);
  40. }
  41. readMsgsFromTelegram();
  42. read_GPS_receiver();
  43. }//end of Loop
  44.  
  45.  
  46. void setup_input_output() {
  47. Serial.begin(115200);
  48. Serial2.begin(9600);
  49. pinMode(Rpi_ESP32_GPS_Pin, INPUT);
  50. pinMode(ESP32_Rpi_Door_Pin, OUTPUT);
  51. digitalWrite(ESP32_Rpi_Door_Pin, LOW);
  52. }
  53.  
  54. void read_GPS_receiver() {
  55. while (Serial2.available() > 0) {
  56. if (gps.encode(Serial2.read())) {
  57. if (gps.location.isValid()) {
  58. Latitude = String(gps.location.lat(), 6);
  59. Longitude = String(gps.location.lng(), 6);
  60. Serial.print(Latitude);
  61. Serial.print(",");
  62. Serial.print(Longitude);
  63. Serial.println();
  64. google_maps_link_string = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + Latitude + "+" + Longitude;
  65. google_maps_link_string.toCharArray(google_maps_link, 70);
  66. }
  67. else {
  68. //Serial.println(F("INVALID"));
  69. google_maps_link_string = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=21.462718+39.200071";
  70. }
  71. }
  72. }
  73. if (millis() > 5000 && gps.charsProcessed() < 10) {
  74. Serial.println(F("No GPS detected: check wiring."));
  75. while (true);
  76. }
  77. }
  78.  
  79. void connect_wifi_telegram() {
  80. Serial.print("Connecting to Wifi SSID ");
  81. Serial.print(WIFI_SSID);
  82. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  83. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  84. while (WiFi.status() != WL_CONNECTED)
  85. {
  86. Serial.print(".");
  87. delay(500);
  88. }
  89. Serial.print("\nWiFi connected. IP address: ");
  90. Serial.println(WiFi.localIP());
  91.  
  92. Serial.print("Retrieving time: ");
  93. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  94. time_t now = time(nullptr);
  95. while (now < 24 * 3600)
  96. {
  97. Serial.print(".");
  98. delay(100);
  99. now = time(nullptr);
  100. }
  101. Serial.println(now);
  102.  
  103. }
  104.  
  105.  
  106. void readMsgsFromTelegram() {
  107. if (millis() - bot_lasttime > BOT_MTBS)
  108. {
  109. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  110.  
  111. while (numNewMessages)
  112. {
  113. Serial.println("got response");
  114. handleNewMessages(numNewMessages);
  115. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  116. }
  117.  
  118. bot_lasttime = millis();
  119. }
  120. }
  121.  
  122.  
  123. void handleNewMessages(int numNewMessages)
  124. {
  125. Serial.println("handleNewMessages");
  126. Serial.println(String(numNewMessages));
  127.  
  128. for (int i = 0; i < numNewMessages; i++)
  129. {
  130. String chat_id = bot.messages[i].chat_id;
  131. String text = bot.messages[i].text;
  132.  
  133. String from_name = bot.messages[i].from_name;
  134. if (from_name == "")
  135. from_name = "Guest";
  136.  
  137. if (text == "/send_test_action")
  138. {
  139. bot.sendChatAction(chat_id, "typing");
  140. delay(4000);
  141. bot.sendMessage(chat_id, "Did you see the action message?");
  142. }
  143.  
  144. if (text == "/gps") {
  145. bot.sendMessage(chat_id, google_maps_link_string);
  146. }
  147.  
  148. if (text == "/password123") {
  149. digitalWrite(ESP32_Rpi_Door_Pin, HIGH);
  150. bot.sendMessage(chat_id, "Password Correct Door is now Open");
  151. delay(2000);
  152. digitalWrite(ESP32_Rpi_Door_Pin, LOW);
  153. }
  154.  
  155. if (text == "/start")
  156. {
  157. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  158. welcome += "This is Chat Action Bot example.\n\n";
  159. bot.sendMessage(chat_id, welcome);
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement