Advertisement
safwan092

Project_10622_ESP32_Fingerprint_Relay_Telegram

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