Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h> // Include the Keypad library
- #include <WiFi.h> // Include the WiFi library
- #include <UniversalTelegramBot.h> // Include the UniversalTelegramBot library
- #include <MFRC522.h> // Include the MFRC522 library
- #include <WiFiClientSecure.h>
- //----------------------------------------------------------------------------------------------------------------
- #define SS_PIN 21 //◄ Changed to match picture [DAWAER] // Define the SS pin for the MFRC522 RFID sensor
- //----------------------------------------------------------------------------------------------------------------
- #define RST_PIN 22 //◄ Changed to match picture [DAWAER] // Define the RST pin for the MFRC522 RFID sensor
- //----------------------------------------------------------------------------------------------------------------
- #define LED_PIN 27 //◄ Changed to pin with no boot problem [DAWAER]// Define the LED pin
- #define BUZZER_PIN 4 // Define the buzzer pin
- //----------------------------------------------------------------------------------------------------------------
- #define DOOR_PIN 13 //◄ Changed to free pin for GPS [DAWAER]// Define the pin for the door
- //----------------------------------------------------------------------------------------------------------------
- const byte ROWS = 4; // Define the number of rows in the keypad
- const byte COLS = 4; // Define the number of columns in the keypad
- char keys[ROWS][COLS] = { // Define the keys in the keypad
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- //----------------------------------------------------------------------------------------------------------------
- // ↓ Changed to match picture [DAWAER]
- byte rowPins[ROWS] = {14, 27, 26, 25}; // Define the row pins for the keypad
- //----------------------------------------------------------------------------------------------------------------
- byte colPins[COLS] = {33, 32, 2, 15}; // Define the column pins for the keypad
- Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Create a Keypad object
- const char* ssid = "network"; // Replace with your WiFi network name
- const char* password = "123456789"; // Replace with your WiFi network password
- const char* token = "16063247261:AAE-dCDewFBwZLQQzw_TwVpp40C8plhMhko"; // Replace with your Telegram bot token
- String chat_id = "0594170852"; // Replace with your Telegram chat ID
- WiFiClientSecure client; // Create a secure WiFi client
- UniversalTelegramBot bot(token, client); // Create a UniversalTelegramBot object
- MFRC522 rfid(SS_PIN, RST_PIN); // Create an MFRC522 object
- MFRC522::MIFARE_Key key; // Create an MFRC522 MIFARE key object
- int state = 0; // Initialize the state variable
- String password_str = ""; // Initialize the password string
- bool door_opened = false; // Initialize the door_opened boolean
- void setup() {
- Serial.begin(9600); // Start the serial communication
- pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
- pinMode(BUZZER_PIN, OUTPUT); // Set the buzzer pin as an output
- pinMode(DOOR_PIN, OUTPUT); // Set the door pin as an output
- digitalWrite(LED_PIN, LOW); // Turn off the LED
- digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
- digitalWrite(DOOR_PIN, LOW); // Close the door
- WiFi.begin(ssid, password); // Connect to the WiFi network
- while (WiFi.status() != WL_CONNECTED) { // Wait for the WiFi connection to be established
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- SPI.begin(); // Initialize the SPI bus
- rfid.PCD_Init(); // Initialize the MFRC522 RFID sensor
- for (byte i = 0; i < 6; i++) { // Initialize the MIFARE key
- key.keyByte[i] = 0xFF;
- }
- }
- void loop() {
- switch (state) {
- case 0:
- password_str = ""; // Reset the password string
- Serial.println("Enter password:");
- state = 1; // Move to the next state
- break;
- case 1:
- {
- char key_pressed = keypad.getKey(); // Read the key pressed on the keypad
- if (key_pressed != NO_KEY) { // If a key is pressed
- if (key_pressed == '#') { // If the '#' key is pressed
- if (password_str == "1234") { // If the password is correct
- Serial.println("Password correct");
- open_door(); // Open the door
- state = 2; // Move to the next state
- } else { // If the password is incorrect
- Serial.println("Password incorrect");
- buzz(); // Buzz the buzzer
- state = 0; // Reset the state
- }
- } else { // If a number key is pressed
- password_str += key_pressed; // Add the key to the password string
- }
- }
- }
- break;
- case 2:
- if (door_opened) { // If the door has been opened
- 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
- door_opened = false; // Reset the door_opened boolean
- }
- state = 3; // Move to the next state
- break;
- case 3:
- if (int x = bot.getUpdates(bot.last_message_received + 1)) { // Check for incoming Telegram messages
- handleNewMessages(x); // Handle the incoming message
- }
- break;
- }
- if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) { // If an RFID card is detected
- if (authenticate_card()) { // If the card is authenticated
- Serial.println("Card authenticated");
- open_door(); // Open the door
- 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
- delay(5000); // Wait for 5 seconds
- door_opened = false; // Reset the door_opened boolean
- } else { // If the card is not authenticated
- Serial.println("Card authentication failed");
- buzz(); // Buzz the buzzer
- }
- rfid.PICC_HaltA(); // Halt the RFID tag
- rfid.PCD_StopCrypto1(); // Stop the crypto1 process
- }
- }
- void open_door() {
- digitalWrite(LED_PIN, HIGH); // Turn on the LED
- digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
- digitalWrite(DOOR_PIN, HIGH); // Open the door
- delay(5000); // Wait for 5 seconds
- digitalWrite(LED_PIN, LOW); // Turn off the LED
- digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
- digitalWrite(DOOR_PIN, LOW); // Close the door
- door_opened = true; // Set the door_opened boolean to true
- }
- void buzz() {
- digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
- delay(500); // Wait for 0.5 seconds
- digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
- }
- bool authenticate_card() {
- MFRC522::StatusCode status;
- byte buffer[18];
- byte size = sizeof(buffer);
- status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(rfid.uid)); // Authenticate the card using the MIFARE key
- if (status != MFRC522::STATUS_OK) {
- return false;
- }
- status = rfid.MIFARE_Read(1, buffer, &size); // Read the card data
- if (status != MFRC522::STATUS_OK) {
- return false;
- }
- return true;
- }
- void handleNewMessages(int numNewMessages)
- {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
- for (int i = 0; i < numNewMessages; i++)
- {
- String chat_from = bot.messages[i].chat_id;
- String text = bot.messages[i].text;
- String from_name = bot.messages[i].from_name;
- if (from_name == "")
- from_name = "Guest";
- if (text == "/open" && chat_from == chat_id) { // If the message is "/open" and the chat ID is correct
- Serial.println("Door opened via telegram");
- open_door(); // Open the door
- 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
- }
- }
- }
- void send_telegram_message(String message) {
- bot.sendMessage(chat_id, "typing"); // Send a message to the Telegram chat
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement