Advertisement
pleasedontcode

**Lock System** rev_52

Oct 5th, 2024
87
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: **Lock System**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-05 07:46:57
  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. - add a button which enable the insertion of PIN code.
  34. #### Feedback 2 ####
  35. - i need a physical button to enable pin enter into keypad
  36. #### Feedback 3 ####
  37. - remove pinentryactive variable and use directly the state of but
  38. ton read from pin.
  39. ********* User code review feedback **********/
  40.  
  41. /****** DEFINITION OF LIBRARIES *****/
  42. #include <Deneyap_Servo.h>    // Library for controlling servo motors
  43. #include <Keypad.h>           // Library for keypad input
  44. #include <WiFi.h>            // Library for WiFi functionality
  45. #include <WebServer.h>        // Library for web server functionality
  46.  
  47. /****** FUNCTION PROTOTYPES *****/
  48. void setup(void);
  49. void loop(void);
  50. void handleRoot();          // Function to handle root URL
  51.  
  52. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  53. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  54. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  55. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  56. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  57. const uint8_t buttonPin = 2; // Physical button pin for enabling PIN entry
  58.  
  59. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  60. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  61. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  62. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  63.  
  64. /***** DEFINITION OF PWM OUTPUT PINS *****/
  65. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  66.  
  67. /***** DEFINITION OF KEYPAD *****/
  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. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  78. Servo myServo; // Servo object
  79.  
  80. /***** DEFINITION OF VARIABLES *****/
  81. const char* ssid = "your_SSID"; // Your WiFi SSID
  82. const char* password = "your_PASSWORD"; // Your WiFi Password
  83. WebServer server(80); // Create a web server on port 80
  84.  
  85. bool doorLocked = true; // Initial state of the door
  86. const String correctPIN = "1234"; // Correct PIN code
  87. String inputPIN = ""; // User input PIN
  88.  
  89. void setup(void) {
  90.     Serial.begin(115200); // Start serial communication
  91.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attach the servo to the PWM pin
  92.     myServo.write(0); // Initially lock the door
  93.  
  94.     // Set up keypad pins
  95.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  96.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  97.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  98.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  99.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  100.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  101.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  102.     pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
  103.  
  104.     // Connect to WiFi
  105.     WiFi.begin(ssid, password);
  106.     while (WiFi.status() != WL_CONNECTED) {
  107.         delay(1000);
  108.         Serial.println("Connecting to WiFi...");
  109.     }
  110.     Serial.println("Connected to WiFi");
  111.  
  112.     // Set up web server
  113.     server.on("/", handleRoot); // Handle root URL
  114.     server.begin(); // Start the server
  115. }
  116.  
  117. void loop(void) {
  118.     server.handleClient(); // Handle client requests
  119.     char key = keypad.getKey(); // Get the pressed key
  120.  
  121.     // Check if the button is pressed to activate PIN entry
  122.     if (digitalRead(buttonPin) == LOW) {
  123.         inputPIN = ""; // Reset input
  124.         Serial.println("PIN entry activated. Please enter your PIN.");
  125.         delay(1000); // Debounce delay
  126.     }
  127.  
  128.     if (key) {
  129.         // Check if the button is pressed to determine if PIN entry is active
  130.         if (digitalRead(buttonPin) == LOW) {
  131.             if (key == '#') { // If '#' is pressed, check the PIN
  132.                 if (inputPIN == correctPIN) {
  133.                     doorLocked = !doorLocked; // Toggle lock state
  134.                     myServo.write(doorLocked ? 0 : 90); // Lock or unlock the door
  135.                     Serial.println("PIN accepted. Door state changed.");
  136.                 } else {
  137.                     Serial.println("Incorrect PIN. Try again.");
  138.                 }
  139.                 inputPIN = ""; // Reset input
  140.             } else {
  141.                 inputPIN += key; // Append key to input PIN
  142.             }
  143.         }
  144.     }
  145. }
  146.  
  147. void handleRoot() {
  148.     String message = "Door is currently " + String(doorLocked ? "Locked" : "Unlocked");
  149.     server.send(200, "text/html", message); // Send response to the client
  150. }
  151.  
  152. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement