Advertisement
safwan092

Untitled

Jul 24th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. #include <Keypad.h> // Include the Keypad library
  2. #include <WiFi.h> // Include the WiFi library
  3. #include <UniversalTelegramBot.h> // Include the UniversalTelegramBot library
  4. #include <MFRC522.h> // Include the MFRC522 library
  5. #include <WiFiClientSecure.h>
  6. //----------------------------------------------------------------------------------------------------------------
  7. #define SS_PIN 21 //◄ Changed to match picture [DAWAER] // Define the SS pin for the MFRC522 RFID sensor
  8. //----------------------------------------------------------------------------------------------------------------
  9. #define RST_PIN 22 //◄ Changed to match picture [DAWAER] // Define the RST pin for the MFRC522 RFID sensor
  10. //----------------------------------------------------------------------------------------------------------------
  11. #define LED_PIN 27 //◄ Changed to pin with no boot problem [DAWAER]// Define the LED pin
  12. #define BUZZER_PIN 4 // Define the buzzer pin
  13. //----------------------------------------------------------------------------------------------------------------
  14. #define DOOR_PIN 13 //◄ Changed to free pin for GPS [DAWAER]// Define the pin for the door
  15. //----------------------------------------------------------------------------------------------------------------
  16. const byte ROWS = 4; // Define the number of rows in the keypad
  17. const byte COLS = 4; // Define the number of columns in the keypad
  18. char keys[ROWS][COLS] = { // Define the keys in the keypad
  19. {'1', '2', '3', 'A'},
  20. {'4', '5', '6', 'B'},
  21. {'7', '8', '9', 'C'},
  22. {'*', '0', '#', 'D'}
  23. };
  24. //----------------------------------------------------------------------------------------------------------------
  25. // ↓ Changed to match picture [DAWAER]
  26. byte rowPins[ROWS] = {14, 27, 26, 25}; // Define the row pins for the keypad
  27. //----------------------------------------------------------------------------------------------------------------
  28. byte colPins[COLS] = {33, 32, 2, 15}; // Define the column pins for the keypad
  29. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Create a Keypad object
  30.  
  31. const char* ssid = "network"; // Replace with your WiFi network name
  32. const char* password = "123456789"; // Replace with your WiFi network password
  33. const char* token = "16063247261:AAE-dCDewFBwZLQQzw_TwVpp40C8plhMhko"; // Replace with your Telegram bot token
  34. String chat_id = "0594170852"; // Replace with your Telegram chat ID
  35. WiFiClientSecure client; // Create a secure WiFi client
  36. UniversalTelegramBot bot(token, client); // Create a UniversalTelegramBot object
  37.  
  38. MFRC522 rfid(SS_PIN, RST_PIN); // Create an MFRC522 object
  39. MFRC522::MIFARE_Key key; // Create an MFRC522 MIFARE key object
  40.  
  41. int state = 0; // Initialize the state variable
  42. String password_str = ""; // Initialize the password string
  43. bool door_opened = false; // Initialize the door_opened boolean
  44.  
  45. void setup() {
  46. Serial.begin(9600); // Start the serial communication
  47. pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
  48. pinMode(BUZZER_PIN, OUTPUT); // Set the buzzer pin as an output
  49. pinMode(DOOR_PIN, OUTPUT); // Set the door pin as an output
  50. digitalWrite(LED_PIN, LOW); // Turn off the LED
  51. digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
  52. digitalWrite(DOOR_PIN, LOW); // Close the door
  53. WiFi.begin(ssid, password); // Connect to the WiFi network
  54. while (WiFi.status() != WL_CONNECTED) { // Wait for the WiFi connection to be established
  55. delay(1000);
  56. Serial.println("Connecting to WiFi...");
  57. }
  58. Serial.println("Connected to WiFi");
  59. SPI.begin(); // Initialize the SPI bus
  60. rfid.PCD_Init(); // Initialize the MFRC522 RFID sensor
  61. for (byte i = 0; i < 6; i++) { // Initialize the MIFARE key
  62. key.keyByte[i] = 0xFF;
  63. }
  64. }
  65.  
  66. void loop() {
  67. switch (state) {
  68. case 0:
  69. password_str = ""; // Reset the password string
  70. Serial.println("Enter password:");
  71. state = 1; // Move to the next state
  72. break;
  73. case 1:
  74. {
  75. char key_pressed = keypad.getKey(); // Read the key pressed on the keypad
  76. if (key_pressed != NO_KEY) { // If a key is pressed
  77. if (key_pressed == '#') { // If the '#' key is pressed
  78. if (password_str == "1234") { // If the password is correct
  79. Serial.println("Password correct");
  80. open_door(); // Open the door
  81. state = 2; // Move to the next state
  82. } else { // If the password is incorrect
  83. Serial.println("Password incorrect");
  84. buzz(); // Buzz the buzzer
  85. state = 0; // Reset the state
  86. }
  87. } else { // If a number key is pressed
  88. password_str += key_pressed; // Add the key to the password string
  89. }
  90. }
  91. }
  92. break;
  93. case 2:
  94. if (door_opened) { // If the door has been opened
  95. bot.sendMessage(chat_id, "Door opened via keypad"); //Send a message to the Telegram chat to notify the user that the door has been opened via the keypad
  96. door_opened = false; // Reset the door_opened boolean
  97. }
  98. state = 3; // Move to the next state
  99. break;
  100. case 3:
  101. if (int x = bot.getUpdates(bot.last_message_received + 1)) { // Check for incoming Telegram messages
  102. handleNewMessages(x); // Handle the incoming message
  103. }
  104. break;
  105. }
  106. if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) { // If an RFID card is detected
  107. if (authenticate_card()) { // If the card is authenticated
  108. Serial.println("Card authenticated");
  109. open_door(); // Open the door
  110. bot.sendMessage(chat_id, "Door opened via RFID"); // Send a message to the Telegram chat to notify the user that the door has been opened via RFID
  111. delay(5000); // Wait for 5 seconds
  112. door_opened = false; // Reset the door_opened boolean
  113. } else { // If the card is not authenticated
  114. Serial.println("Card authentication failed");
  115. buzz(); // Buzz the buzzer
  116. }
  117. rfid.PICC_HaltA(); // Halt the RFID tag
  118. rfid.PCD_StopCrypto1(); // Stop the crypto1 process
  119. }
  120. }
  121.  
  122. void open_door() {
  123. digitalWrite(LED_PIN, HIGH); // Turn on the LED
  124. digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
  125. digitalWrite(DOOR_PIN, HIGH); // Open the door
  126. delay(5000); // Wait for 5 seconds
  127. digitalWrite(LED_PIN, LOW); // Turn off the LED
  128. digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
  129. digitalWrite(DOOR_PIN, LOW); // Close the door
  130. door_opened = true; // Set the door_opened boolean to true
  131. }
  132.  
  133. void buzz() {
  134. digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
  135. delay(500); // Wait for 0.5 seconds
  136. digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
  137. }
  138.  
  139. bool authenticate_card() {
  140. MFRC522::StatusCode status;
  141. byte buffer[18];
  142. byte size = sizeof(buffer);
  143. status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(rfid.uid)); // Authenticate the card using the MIFARE key
  144. if (status != MFRC522::STATUS_OK) {
  145. return false;
  146. }
  147. status = rfid.MIFARE_Read(1, buffer, &size); // Read the card data
  148. if (status != MFRC522::STATUS_OK) {
  149. return false;
  150. }
  151. return true;
  152. }
  153.  
  154. void handleNewMessages(int numNewMessages)
  155. {
  156. Serial.println("handleNewMessages");
  157. Serial.println(String(numNewMessages));
  158.  
  159. for (int i = 0; i < numNewMessages; i++)
  160. {
  161. String chat_from = bot.messages[i].chat_id;
  162. String text = bot.messages[i].text;
  163.  
  164. String from_name = bot.messages[i].from_name;
  165. if (from_name == "")
  166. from_name = "Guest";
  167.  
  168. if (text == "/open" && chat_from == chat_id) { // If the message is "/open" and the chat ID is correct
  169. Serial.println("Door opened via telegram");
  170. open_door(); // Open the door
  171. bot.sendMessage(chat_id, "Door opened via telegram"); // Send a message to the Telegram chat to notify the user that the door has been opened via Telegram
  172. }
  173.  
  174. }
  175.  
  176.  
  177. }
  178.  
  179.  
  180. void send_telegram_message(String message) {
  181. bot.sendMessage(chat_id, "typing"); // Send a message to the Telegram chat
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement