Advertisement
safwan092

Untitled

Dec 30th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #include <ArduinoJson.h>
  5. #include <ESP32Servo.h>
  6. // -------- Define Pins -----------
  7.  
  8. #define SERVO_PIN1 26
  9. #define SERVO_PIN2 27
  10. const int trigPin = 22;
  11. const int echoPin = 23;
  12. long duration;
  13. int distance;
  14. #define LED 21
  15. #define buzzer 25
  16. Servo servoMotor1;
  17. Servo servoMotor2;
  18. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  19. unsigned long bot_lasttime; // last time messages' scan has been done
  20.  
  21. // Wifi network station credentials
  22. #define WIFI_SSID "Zain-E6878-606A"
  23. #define WIFI_PASSWORD "DEMDEFJ09QT"
  24. #define BOT_TOKEN "6847705271:AAEbKxAIQ7TD2MJBw1YbGFrARi0OrHR8ENg"
  25. #define CHAT_ID "-1002124008517"
  26.  
  27.  
  28. WiFiClientSecure secured_client;
  29. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  30.  
  31. //////////////////////////////////////////////////////////
  32.  
  33. void handleNewMessages(int numNewMessages)
  34. {
  35. Serial.println("handleNewMessages");
  36. Serial.println(String(numNewMessages));
  37.  
  38. for (int i = 0; i < numNewMessages; i++)
  39. {
  40. String chat_id = bot.messages[i].chat_id;
  41. String text = bot.messages[i].text;
  42.  
  43. String from_name = bot.messages[i].from_name;
  44. if (from_name == "")
  45. from_name = "Guest";
  46.  
  47. if (text == "/send_test_action")
  48. {
  49. bot.sendChatAction(chat_id, "typing");
  50. delay(4000);
  51. bot.sendMessage(chat_id, "Did you see the action message?");
  52. }
  53.  
  54. if (text == "/open_door1") {
  55. servoMotor1.write(120);
  56. delay(15);
  57. bot.sendMessage(CHAT_ID, "door1 is open ");
  58. }
  59. if (text == "/close_door1") {
  60. servoMotor1.write(0);
  61. delay(15);
  62. bot.sendMessage(CHAT_ID, "door1 is open ");
  63. }
  64. if (text == "/close_door2") {
  65. servoMotor2.write(120);
  66. delay(15);
  67. bot.sendMessage(CHAT_ID, "door2 is close ");
  68. }
  69. if (text == "/open_door2") {
  70. servoMotor2.write(0);
  71. delay(15);
  72. bot.sendMessage(CHAT_ID, "door2 is open ");
  73. }
  74. if (text.equals("/options")) {
  75. String keyboardJson = "[[\"/open_door1\"],[\"/close_door1\"],[\"/open_door2\"],[\"/close_door2\"]]";
  76. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  77. }
  78. if (text == "/start")
  79. {
  80. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  81. welcome += "This is Chat Action Bot example.\n\n";
  82. welcome += "/options : returns a custom reply keyboard\n";
  83. bot.sendMessage(chat_id, welcome);
  84. }
  85. }
  86. }
  87.  
  88. //////////////////////////////////////////////////////////
  89.  
  90.  
  91.  
  92. void setup() {
  93. Serial.begin(115200);
  94. Serial.println();
  95. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  96. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  97. servoMotor1.attach(SERVO_PIN1);
  98. servoMotor2.attach(SERVO_PIN2);
  99. pinMode(LED, OUTPUT);
  100. pinMode(buzzer, OUTPUT);
  101. // attempt to connect to Wifi network:
  102. Serial.print("Connecting to Wifi SSID ");
  103. Serial.print(WIFI_SSID);
  104. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  105. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  106. while (WiFi.status() != WL_CONNECTED)
  107. {
  108. Serial.print(".");
  109. delay(500);
  110. }
  111. Serial.print("\nWiFi connected. IP address: ");
  112. Serial.println(WiFi.localIP());
  113.  
  114. Serial.print("Retrieving time: ");
  115. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  116. time_t now = time(nullptr);
  117. while (now < 24 * 3600)
  118. {
  119. Serial.print(".");
  120. delay(100);
  121. now = time(nullptr);
  122. }
  123. Serial.println(now);
  124. bot.sendMessage(CHAT_ID, "Bot started up", "");
  125. Serial.println("delay finished");
  126. }
  127.  
  128. void loop() {
  129. digitalWrite(trigPin, LOW);
  130. delayMicroseconds(2);
  131. digitalWrite(trigPin, HIGH);
  132. delayMicroseconds(10);
  133. digitalWrite(trigPin, LOW);
  134. duration = pulseIn(echoPin, HIGH);
  135. distance = duration * 0.034 / 2;
  136. Serial.println(distance);
  137. delay(10);
  138. if (distance < 50 && distance > 0) {
  139. digitalWrite(LED, HIGH);
  140. digitalWrite(buzzer, HIGH );
  141. bot.sendMessage(CHAT_ID, "human detector " + String(distance));
  142. }
  143. else {
  144. digitalWrite(LED, 0 );
  145. digitalWrite(buzzer, 0);
  146. }
  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.  
  163. }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement