Advertisement
pleasedontcode

Smart Lock rev_90

Oct 6th, 2024
78
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-06 10:33:23
  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. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  32. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  33. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  34. #include <LcdKeypad.h>  //https://github.com/dniklaus/arduino-display-lcdkeypad
  35. #include <XYZrobotServo.h>  //https://github.com/pololu/xyzrobot-servo-arduino
  36. #include <WiFi.h> // Include WiFi library for web server
  37. #include <WebServer.h> // Include WebServer library
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42. void updateOutputs();
  43. void handleRoot(); // Function to handle root URL
  44. void lockUnlockDoor(); // Function to lock/unlock the door
  45.  
  46. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  47. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  48. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  49. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  50. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  51. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  52.  
  53. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  54. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  55. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  56. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  57.  
  58. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  59. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  60. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  61. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  62.  
  63. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  64. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  65. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  66. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  67.  
  68. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  69. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  70. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  71.     {'1','2','3'},
  72.     {'4','5','6'},
  73.     {'7','8','9'},
  74.     {'*','0','#'},
  75. };
  76.  
  77. // Create instances for servo and web server
  78. Deneyap_Servo servoMotor;
  79. WebServer server(80); // Create a web server on port 80
  80.  
  81. // Define the correct PIN code
  82. const String correctPIN = "1234"; // Example PIN code
  83. String inputPIN = ""; // Variable to store user input PIN
  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.     // Attach the servo to a pin
  99.     servoMotor.attach(11); // Example pin for servo control
  100.     servoMotor.write(0); // Start with the door locked
  101.  
  102.     // Initialize WiFi
  103.     WiFi.begin("yourSSID", "yourPASSWORD"); // Replace with your WiFi credentials
  104.     while (WiFi.status() != WL_CONNECTED) {
  105.         delay(1000);
  106.     }
  107.     server.on("/", handleRoot); // Handle root URL
  108.     server.begin(); // Start the server
  109. }
  110.  
  111. void loop(void)
  112. {
  113.     // put your main code here, to run repeatedly:
  114.     updateOutputs(); // Refresh output data
  115.     server.handleClient(); // Handle client requests
  116.  
  117.     // Check keypad input
  118.     char key = getKeypadInput();
  119.     if (key != '\0') {
  120.         inputPIN += key; // Append pressed key to inputPIN
  121.         if (inputPIN.length() == 4) { // Check if 4 digits are entered
  122.             lockUnlockDoor(); // Check and lock/unlock the door
  123.             inputPIN = ""; // Reset inputPIN after checking
  124.         }
  125.     }
  126. }
  127.  
  128. void updateOutputs()
  129. {
  130.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  131.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  132.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  133. }
  134.  
  135. char getKeypadInput() {
  136.     // Function to read the keypad and return the pressed key
  137.     for (int row = 0; row < KEYPAD3X4_ROWS; row++) {
  138.         for (int col = 0; col < KEYPAD3X4_COLS; col++) {
  139.             if (digitalRead(keypad_Keypad3x4_R1_PIN_D4 + row) == LOW) {
  140.                 return Keys_Keypad3x4[row][col]; // Return the pressed key
  141.             }
  142.         }
  143.     }
  144.     return '\0'; // No key pressed
  145. }
  146.  
  147. void handleRoot() {
  148.     // Function to handle root URL and send lock/unlock state
  149.     String state = (servoMotor.read() == 0) ? "Locked" : "Unlocked";
  150.     server.send(200, "text/plain", "Door is currently: " + state);
  151. }
  152.  
  153. void lockUnlockDoor() {
  154.     // Function to lock/unlock the door based on the input PIN
  155.     if (inputPIN == correctPIN) {
  156.         int currentAngle = servoMotor.read();
  157.         if (currentAngle == 0) {
  158.             servoMotor.write(90); // Unlock the door
  159.         } else {
  160.             servoMotor.write(0); // Lock the door
  161.         }
  162.     }
  163. }
  164.  
  165. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement