Advertisement
pleasedontcode

Smart Lock rev_118

Oct 7th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Smart Lock
  13.     - Source Code compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-07 22:26:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The servo motor installed in the door will lock or */
  21.     /* unlock based on the correct PIN code received by */
  22.     /* the button. If the door is unlocked and the PIN is */
  23.     /* correct, it will lock. If the door is locked and */
  24.     /* the PIN is correct, it will unlock. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* The webserver provides a web page about lock or */
  27.     /* unlock state of the door. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  34. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  35. #include <WiFi.h>  // Include WiFi library for web server
  36. #include <WebServer.h> // Include WebServer library
  37. #include "ServoControl.h" // Include the ServoControl header
  38. #include "WebServerControl.h" // Include the WebServerControl header
  39. #include "KeypadHandler.h" // Include the KeypadHandler header
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44.  
  45. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  46. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  47. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  48. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  49. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  50. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  51.  
  52. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  53. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  54. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  55. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  56.  
  57. // Define raw data variables for digital outputs
  58. int keypad_Keypad3x4_C1_PIN_D8_rawData = 0; // Define based on your logic
  59. int keypad_Keypad3x4_C2_PIN_D9_rawData = 0; // Define based on your logic
  60. int keypad_Keypad3x4_C3_PIN_D10_rawData = 0; // Define based on your logic
  61.  
  62. const char* ssid = "your_SSID";  // Replace with your network SSID
  63. const char* password = "your_PASSWORD";  // Replace with your network password
  64.  
  65. /****** DEFINITION OF OUTPUT VARIABLES *****/
  66. bool doorLocked = true; // Initial state of the door
  67. ServoControl servoControl(9); // Assuming the servo is connected to pin 9
  68. WebServerControl webServerControl; // Create web server control instance
  69.  
  70. // Keypad configuration
  71. const uint8_t KEYPAD3X4_ROWS = 4; // Four rows
  72. const uint8_t KEYPAD3X4_COLS = 3; // Three columns
  73. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  74.     {'1','2','3'},
  75.     {'4','5','6'},
  76.     {'7','8','9'},
  77.     {'*','0','#'},
  78. };
  79.  
  80. // Create KeypadHandler object
  81. byte rowPins[4] = {keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7};
  82. byte colPins[3] = {keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10};
  83. KeypadHandler keypadHandler(Keys_Keypad3x4, rowPins, colPins);
  84.  
  85. void setup(void)
  86. {
  87.     // put your setup code here, to run once:
  88.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  89.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  90.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  91.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  92.     pinMode(pinButton_PushButton_PIN_D2,    INPUT_PULLUP);
  93.  
  94.     pinMode(keypad_Keypad3x4_C1_PIN_D8,  OUTPUT);
  95.     pinMode(keypad_Keypad3x4_C2_PIN_D9,  OUTPUT);
  96.     pinMode(keypad_Keypad3x4_C3_PIN_D10,     OUTPUT);
  97.  
  98.     // Initialize WiFi
  99.     WiFi.begin(ssid, password);
  100.     while (WiFi.status() != WL_CONNECTED) {
  101.         delay(1000);
  102.     }
  103.     webServerControl.begin(); // Start the web server
  104. }
  105.  
  106. void loop(void)
  107. {
  108.     // put your main code here, to run repeatedly:
  109.     updateOutputs(); // Refresh output data
  110.     webServerControl.handleClient(); // Handle web server requests
  111.  
  112.     // Check for keypad input here and unlock/lock the door based on the PIN
  113.     char key = keypadHandler.getKey(); // Use the KeypadHandler to get the key
  114.     if (key) {
  115.         handleKeypadInput(key); // Function to handle keypad input
  116.     }
  117. }
  118.  
  119. void updateOutputs()
  120. {
  121.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  122.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  123.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  124. }
  125.  
  126. void handleKeypadInput(char key)
  127. {
  128.     // Assuming '1234' is the correct PIN code
  129.     static String input;
  130.     if (key == '*') { // Reset input
  131.         input = "";
  132.     } else if (key == '#') { // Check PIN
  133.         if (input == "1234") { // Check if input matches the PIN
  134.             if (doorLocked) {
  135.                 servoControl.unlock(); // Unlock the door
  136.                 doorLocked = false;
  137.             } else {
  138.                 servoControl.lock(); // Lock the door
  139.                 doorLocked = true;
  140.             }
  141.             webServerControl.setLockState(doorLocked); // Update web server state
  142.         }
  143.     } else { // Append digit to input
  144.         input += key;
  145.     }
  146. }
  147.  
  148. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement