safwan092

Untitled

May 20th, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #define relay 2
  5. #define doorSensor 4
  6. #define face_detection_pin 19
  7. #define WIFI_SSID "ahmed2"
  8. #define WIFI_PASSWORD "123456789"
  9. // Telegram BOT Token (Get from Botfather)
  10. #define BOT_TOKEN "6233605202:AAFGQRfvyyP6UrqKvzwXS5LgLfqsO6tkyP8"
  11. const unsigned long BOT_MTBS = 1000;
  12. WiFiClientSecure secured_client;
  13. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  14. unsigned long bot_lasttime;
  15. bool Start = false;
  16.  
  17. int face_detection;
  18. int flag_door_opened = 0;
  19. int flag_door_state = 0;
  20. int state;
  21. String text;
  22. String chat_id;
  23. String chat_id_1 = "-998291504";
  24. //"5994373858"
  25.  
  26.  
  27.  
  28. void setup()
  29. {
  30. Serial.begin(9600);
  31.  
  32. pinMode(face_detection_pin, INPUT);
  33. pinMode(doorSensor, INPUT_PULLUP);//if door open sensor pin HIGH 3.3V !!
  34. pinMode(relay, OUTPUT);
  35. digitalWrite(relay, 0);
  36. initWiFi_and_Time_For_Telegram();
  37. bot.sendMessage(chat_id_1, "Project ONLINE");
  38. }
  39.  
  40. void loop() {
  41. //face_detection
  42. readSensors();
  43. if (state == 0 && flag_door_state == 1 && flag_door_opened == 1) {
  44. flag_door_state = 0;
  45. flag_door_opened = 0;
  46. bot.sendMessage(chat_id_1, "Door Closed");
  47. }
  48. if (state == 0){
  49. flag_door_opened = 0;
  50. }
  51. if (state == 1) {
  52. flag_door_opened = 1;
  53. if (flag_door_state == 1) {
  54. bot.sendMessage(chat_id_1, "Door Open");
  55. delay(5000);
  56. }
  57. else {
  58. if (face_detection == 0) {
  59. //send Telegram Notification Introder Alert!!
  60. bot.sendMessage(chat_id_1, "Door Open - [Introder Alert!!]");
  61. delay(5000);
  62. }
  63. }
  64. }
  65. if (face_detection == 1) {
  66. digitalWrite(relay, 1);
  67. //send Telegram Notification Access Granted - [Facecode OK]
  68. bot.sendMessage(chat_id_1, "Access Granted - [Facecode OK]");
  69. flag_door_state = 1;
  70. delay(5000);
  71. digitalWrite(relay, 0);
  72. }
  73. delay(100);
  74. }
  75.  
  76. void readSensors() {
  77. state = digitalRead(doorSensor);
  78. face_detection = digitalRead(face_detection_pin);
  79. }
  80.  
  81. void handel_Messages_Loop() {
  82. if (millis() - bot_lasttime > BOT_MTBS)
  83. {
  84. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  85.  
  86. while (numNewMessages)
  87. {
  88. Serial.println("got response");
  89. handleNewMessages(numNewMessages);
  90. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  91. }
  92.  
  93. bot_lasttime = millis();
  94. }
  95. }
  96.  
  97. void initWiFi_and_Time_For_Telegram() {
  98. Serial.println();
  99.  
  100. // attempt to connect to Wifi network:
  101. Serial.print("Connecting to Wifi SSID ");
  102. Serial.print(WIFI_SSID);
  103. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  104. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  105. while (WiFi.status() != WL_CONNECTED)
  106. {
  107. Serial.print(".");
  108. delay(500);
  109. }
  110. Serial.print("\nWiFi connected. IP address: ");
  111. Serial.println(WiFi.localIP());
  112.  
  113. Serial.print("Retrieving time: ");
  114. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  115. time_t now = time(nullptr);
  116. while (now < 24 * 3600)
  117. {
  118. Serial.print(".");
  119. delay(100);
  120. now = time(nullptr);
  121. }
  122. Serial.println(now);
  123.  
  124. }
  125.  
  126. void handleNewMessages(int numNewMessages)
  127. {
  128. Serial.println("handleNewMessages");
  129. Serial.println(String(numNewMessages));
  130.  
  131. for (int i = 0; i < numNewMessages; i++)
  132. {
  133. chat_id = bot.messages[i].chat_id;
  134. text = bot.messages[i].text;
  135.  
  136. String from_name = bot.messages[i].from_name;
  137. if (from_name == "")
  138. from_name = "Guest";
  139.  
  140. if (text == "/send_test_action")
  141. {
  142. bot.sendChatAction(chat_id, "typing");
  143. delay(4000);
  144. bot.sendMessage(chat_id, "Did you see the action message?");
  145.  
  146. // You can't use own message, just choose from one of bellow
  147.  
  148. //typing for text messages
  149. //upload_photo for photos
  150. //record_video or upload_video for videos
  151. //record_audio or upload_audio for audio files
  152. //upload_document for general files
  153. //find_location for location data
  154.  
  155. //more info here - https://core.telegram.org/bots/api#sendchataction
  156. }
  157. if (text == "/start")
  158. {
  159. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  160. welcome += "This is Chat Action Bot example.\n\n";
  161. welcome += "/send_test_action : to send test chat action message\n";
  162. bot.sendMessage(chat_id, welcome);
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment