Advertisement
safwan092

Untitled

Feb 11th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. #include <Adafruit_Fingerprint.h>
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5.  
  6. #define mySerial Serial2
  7. #define RXD2 16
  8. #define TXD2 17
  9. #define relay 2
  10. #define doorSensor 4
  11. #define keypad_detection_pin 18
  12. #define face_detection_pin 19
  13. #define buzzer 21
  14.  
  15. #define WIFI_SSID "Fadi1"
  16. #define WIFI_PASSWORD "Dingdong"
  17. // Telegram BOT Token (Get from Botfather)
  18. #define BOT_TOKEN "5923908776:AAGcEbtVx-BygoVmAw8-xiqbWY1L1ltuyto"
  19. const unsigned long BOT_MTBS = 1000;
  20. WiFiClientSecure secured_client;
  21. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  22. unsigned long bot_lasttime;
  23. bool Start = false;
  24.  
  25. int fingerprint_detection, keypad_detection, face_detection;
  26. int flag_door_opened = 0;
  27. int flag_door_state = 0;
  28. int state;
  29. String text;
  30. String chat_id;
  31. String chat_id_1 = "-840276109";//"146756548";
  32.  
  33. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
  34.  
  35. void setup()
  36. {
  37. Serial.begin(9600);
  38. pinMode(keypad_detection_pin, INPUT);
  39. pinMode(face_detection_pin, INPUT);
  40. pinMode(doorSensor, INPUT_PULLUP);//if door open sensor pin HIGH 3.3V !!
  41. pinMode(relay, OUTPUT);
  42. pinMode(buzzer,OUTPUT);
  43. digitalWrite(buzzer, 0);
  44. digitalWrite(relay, 0);
  45. initWiFi_and_Time_For_Telegram();
  46. initFingerPrint();
  47. bot.sendMessage(chat_id_1, "Project ONLINE");
  48. }
  49.  
  50. void loop() {
  51. //fingerprint_detection
  52. //keypad_detection
  53. //face_detection
  54. readSensors();
  55. if (state == 0 && flag_door_state == 1 && flag_door_opened == 1) {
  56. flag_door_state = 0;
  57. flag_door_opened = 0;
  58. bot.sendMessage(chat_id_1, "Door Closed");
  59. }
  60. if (state == 0){
  61. flag_door_opened = 0;
  62. }
  63. if (state == 1) {
  64. flag_door_opened = 1;
  65. if (flag_door_state == 1) {
  66. bot.sendMessage(chat_id_1, "Door Open");
  67. delay(5000);
  68. }
  69. else {
  70. if (keypad_detection == 0 || face_detection == 0 || int(getFingerprintID) == -1) {
  71. //send Telegram Notification Introder Alert!!
  72. bot.sendMessage(chat_id_1, "Door Open - [Introder Alert!!]");
  73. digitalWrite(buzzer, 1);
  74. delay(3000);
  75. digitalWrite(buzzer, 0);
  76. }
  77. }
  78. }
  79. if (keypad_detection == 1) {
  80. //send Telegram Notification Access Granted - [Passcode OK]
  81. bot.sendMessage(chat_id_1, "Access Granted - [Passcode OK]");
  82. flag_door_state = 1;
  83. delay(5000);
  84. }
  85. if (face_detection == 1) {
  86. digitalWrite(relay, 1);
  87. //send Telegram Notification Access Granted - [Facecode OK]
  88. bot.sendMessage(chat_id_1, "Access Granted - [Facecode OK]");
  89. flag_door_state = 1;
  90. delay(5000);
  91. digitalWrite(relay, 0);
  92. }
  93. delay(100);
  94. }
  95.  
  96. void readSensors() {
  97. state = digitalRead(doorSensor);
  98. keypad_detection = digitalRead(keypad_detection_pin);
  99. face_detection = digitalRead(face_detection_pin);
  100. getFingerprintID();
  101. }
  102.  
  103. uint8_t getFingerprintID() {
  104. uint8_t p = finger.getImage();
  105. switch (p) {
  106. case FINGERPRINT_OK:
  107. Serial.println("Image taken");
  108. break;
  109. case FINGERPRINT_NOFINGER:
  110. //Serial.println("No finger detected");
  111. return 0;
  112. case FINGERPRINT_PACKETRECIEVEERR:
  113. Serial.println("Communication error");
  114. return -1;
  115. case FINGERPRINT_IMAGEFAIL:
  116. Serial.println("Imaging error");
  117. return -1;
  118. default:
  119. Serial.println("Unknown error");
  120. return -1;
  121. }
  122.  
  123. // OK success!
  124.  
  125. p = finger.image2Tz();
  126. switch (p) {
  127. case FINGERPRINT_OK:
  128. Serial.println("Image converted");
  129. break;
  130. case FINGERPRINT_IMAGEMESS:
  131. Serial.println("Image too messy");
  132. return -1;
  133. case FINGERPRINT_PACKETRECIEVEERR:
  134. Serial.println("Communication error");
  135. return -1;
  136. case FINGERPRINT_FEATUREFAIL:
  137. Serial.println("Could not find fingerprint features");
  138. return -1;
  139. case FINGERPRINT_INVALIDIMAGE:
  140. Serial.println("Could not find fingerprint features");
  141. return -1;
  142. default:
  143. Serial.println("Unknown error");
  144. return -1;
  145. }
  146.  
  147. // OK converted!
  148. p = finger.fingerSearch();
  149. if (p == FINGERPRINT_OK) {
  150. Serial.println("Found a print match!");
  151. digitalWrite(relay, 1);
  152. bot.sendMessage(chat_id_1, "Access Granted - [Fingerprint OK]");
  153. flag_door_state = 1;
  154. delay(5000);
  155. digitalWrite(relay, 0);
  156. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  157. Serial.println("Communication error");
  158. return -1;
  159. } else if (p == FINGERPRINT_NOTFOUND) {
  160. Serial.println("Did not find a match");
  161. bot.sendMessage(chat_id_1, "Access Denied - [Fingerprint Not Found!!]");
  162. return -1;
  163. } else {
  164. Serial.println("Unknown error");
  165. return -1;
  166. }
  167.  
  168. // found a match!
  169. Serial.print("Found ID #"); Serial.print(finger.fingerID);
  170. Serial.print(" with confidence of "); Serial.println(finger.confidence);
  171. return finger.fingerID;
  172. }
  173.  
  174. void initFingerPrint() {
  175. Serial2.begin(57600, SERIAL_8N1, RXD2, TXD2);
  176. while (!Serial);
  177. delay(100);
  178. Serial.println("\n\nAdafruit finger detect test");
  179. delay(5);
  180. if (finger.verifyPassword()) {
  181. Serial.println("Found fingerprint sensor!");
  182. } else {
  183. Serial.println("Did not find fingerprint sensor :(");
  184. while (1) {
  185. delay(1);
  186. }
  187. }
  188.  
  189. Serial.println(F("Reading sensor parameters"));
  190. finger.getParameters();
  191. Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  192. Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  193. Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  194. Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  195. Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  196. Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  197. Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);
  198.  
  199. finger.getTemplateCount();
  200.  
  201. if (finger.templateCount == 0) {
  202. Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
  203. }
  204. else {
  205. Serial.println("Waiting for valid finger...");
  206. Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  207. }
  208. }
  209.  
  210.  
  211. void handel_Messages_Loop() {
  212. if (millis() - bot_lasttime > BOT_MTBS)
  213. {
  214. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  215.  
  216. while (numNewMessages)
  217. {
  218. Serial.println("got response");
  219. handleNewMessages(numNewMessages);
  220. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  221. }
  222.  
  223. bot_lasttime = millis();
  224. }
  225. }
  226.  
  227. void initWiFi_and_Time_For_Telegram() {
  228. Serial.println();
  229.  
  230. // attempt to connect to Wifi network:
  231. Serial.print("Connecting to Wifi SSID ");
  232. Serial.print(WIFI_SSID);
  233. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  234. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  235. while (WiFi.status() != WL_CONNECTED)
  236. {
  237. Serial.print(".");
  238. delay(500);
  239. }
  240. Serial.print("\nWiFi connected. IP address: ");
  241. Serial.println(WiFi.localIP());
  242.  
  243. Serial.print("Retrieving time: ");
  244. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  245. time_t now = time(nullptr);
  246. while (now < 24 * 3600)
  247. {
  248. Serial.print(".");
  249. delay(100);
  250. now = time(nullptr);
  251. }
  252. Serial.println(now);
  253.  
  254. }
  255.  
  256. void handleNewMessages(int numNewMessages)
  257. {
  258. Serial.println("handleNewMessages");
  259. Serial.println(String(numNewMessages));
  260.  
  261. for (int i = 0; i < numNewMessages; i++)
  262. {
  263. chat_id = bot.messages[i].chat_id;
  264. text = bot.messages[i].text;
  265.  
  266. String from_name = bot.messages[i].from_name;
  267. if (from_name == "")
  268. from_name = "Guest";
  269.  
  270. if (text == "/send_test_action")
  271. {
  272. bot.sendChatAction(chat_id, "typing");
  273. delay(4000);
  274. bot.sendMessage(chat_id, "Did you see the action message?");
  275.  
  276. // You can't use own message, just choose from one of bellow
  277.  
  278. //typing for text messages
  279. //upload_photo for photos
  280. //record_video or upload_video for videos
  281. //record_audio or upload_audio for audio files
  282. //upload_document for general files
  283. //find_location for location data
  284.  
  285. //more info here - https://core.telegram.org/bots/api#sendchataction
  286. }
  287. if (text == "/start")
  288. {
  289. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  290. welcome += "This is Chat Action Bot example.\n\n";
  291. welcome += "/send_test_action : to send test chat action message\n";
  292. bot.sendMessage(chat_id, welcome);
  293. }
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement