Advertisement
pleasedontcode

Smart Lock rev_119

Oct 7th, 2024
65
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 NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-07 22:34:01
  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.  
  31. /********* User code review feedback **********
  32. #### Feedback 1 ####
  33. - remove servomotorcontrol library which is not used by .ino file.
  34. ********* User code review feedback **********/
  35.  
  36. /* START CODE */
  37.  
  38. /****** DEFINITION OF LIBRARIES *****/
  39. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  40. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  41. #include <WiFi.h>  // Include WiFi library for web server
  42. #include <WebServer.h> // Include WebServer library
  43. #include "ServoControl.h" // Include the ServoControl header
  44. #include "WebServerControl.h" // Include the WebServerControl header
  45. #include "KeypadHandler.h" // Include the KeypadHandler header
  46.  
  47. /****** FUNCTION PROTOTYPES *****/
  48. void setup(void);
  49. void loop(void);
  50.  
  51. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  52. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  53. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  54. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  55. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  56. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  57.  
  58. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  59. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  60. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  61. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  62.  
  63. // Define raw data variables for digital outputs
  64. int keypad_Keypad3x4_C1_PIN_D8_rawData = 0; // Define based on your logic
  65. int keypad_Keypad3x4_C2_PIN_D9_rawData = 0; // Define based on your logic
  66. int keypad_Keypad3x4_C3_PIN_D10_rawData = 0; // Define based on your logic
  67.  
  68. const char* ssid = "your_SSID";  // Replace with your network SSID
  69. const char* password = "your_PASSWORD";  // Replace with your network password
  70.  
  71. /****** DEFINITION OF OUTPUT VARIABLES *****/
  72. bool doorLocked = true; // Initial state of the door
  73. ServoControl servoControl(9); // Assuming the servo is connected to pin 9
  74. WebServerControl webServerControl; // Create web server control instance
  75.  
  76. // Keypad configuration
  77. const uint8_t KEYPAD3X4_ROWS = 4; // Four rows
  78. const uint8_t KEYPAD3X4_COLS = 3; // Three columns
  79. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  80.     {'1','2','3'},
  81.     {'4','5','6'},
  82.     {'7','8','9'},
  83.     {'*','0','#'},
  84. };
  85.  
  86. // Create KeypadHandler object
  87. byte rowPins[4] = {keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7};
  88. byte colPins[3] = {keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10};
  89. KeypadHandler keypadHandler(Keys_Keypad3x4, rowPins, colPins);
  90.  
  91. void setup(void)
  92. {
  93.     // put your setup code here, to run once:
  94.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  95.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  96.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  97.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  98.     pinMode(pinButton_PushButton_PIN_D2,    INPUT_PULLUP);
  99.  
  100.     pinMode(keypad_Keypad3x4_C1_PIN_D8,  OUTPUT);
  101.     pinMode(keypad_Keypad3x4_C2_PIN_D9,  OUTPUT);
  102.     pinMode(keypad_Keypad3x4_C3_PIN_D10,     OUTPUT);
  103.  
  104.     // Initialize WiFi
  105.     WiFi.begin(ssid, password);
  106.     while (WiFi.status() != WL_CONNECTED) {
  107.         delay(1000);
  108.     }
  109.     webServerControl.begin(); // Start the web server
  110. }
  111.  
  112. void loop(void)
  113. {
  114.     // put your main code here, to run repeatedly:
  115.     updateOutputs(); // Refresh output data
  116.     webServerControl.handleClient(); // Handle web server requests
  117.  
  118.     // Check for keypad input here and unlock/lock the door based on the PIN
  119.     char key = keypadHandler.getKey(); // Use the KeypadHandler to get the key
  120.     if (key) {
  121.         handleKeypadInput(key); // Function to handle keypad input
  122.     }
  123. }
  124.  
  125. void updateOutputs()
  126. {
  127.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  128.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  129.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  130. }
  131.  
  132. void handleKeypadInput(char key)
  133. {
  134.     // Assuming '1234' is the correct PIN code
  135.     static String input;
  136.     if (key == '*') { // Reset input
  137.         input = "";
  138.     } else if (key == '#') { // Check PIN
  139.         if (input == "1234") { // Check if input matches the PIN
  140.             if (doorLocked) {
  141.                 servoControl.unlock(); // Unlock the door
  142.                 doorLocked = false;
  143.             } else {
  144.                 servoControl.lock(); // Lock the door
  145.                 doorLocked = true;
  146.             }
  147.             webServerControl.setLockState(doorLocked); // Update web server state
  148.         }
  149.     } else { // Append digit to input
  150.         input += key;
  151.     }
  152. }
  153.  
  154. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement