Advertisement
pleasedontcode

**Keypad Lock** rev_46

Oct 5th, 2024
105
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: **Keypad Lock**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-05 07:01:20
  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 <WiFi.h>          // Include WiFi library for web server
  34. #include <WebServer.h>      // Include WebServer library
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs(void);
  40. void handleRoot(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  44. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  45. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  46. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  50. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  51. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  52.  
  53. /***** DEFINITION OF PWM OUTPUT PINS *****/
  54. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  55.  
  56. /***** KEYPAD CONFIGURATION *****/
  57. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  58. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  59. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  60.     {'1','2','3'},
  61.     {'4','5','6'},
  62.     {'7','8','9'},
  63.     {'*','0','#'},
  64. };
  65.  
  66. // Create keypad object
  67. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  68.  
  69. // Create servo object
  70. Servo myServo;
  71.  
  72. // Web server on port 80
  73. WebServer server(80);
  74.  
  75. // State variables
  76. bool doorLocked = true; // Door starts locked
  77. const char* ssid = "your_SSID"; // Replace with your network SSID
  78. const char* password = "your_PASSWORD"; // Replace with your network password
  79.  
  80. void setup(void) {
  81.     // Initialize serial communication
  82.     Serial.begin(115200);
  83.  
  84.     // Set up WiFi
  85.     WiFi.begin(ssid, password);
  86.     while (WiFi.status() != WL_CONNECTED) {
  87.         delay(1000);
  88.         Serial.println("Connecting to WiFi...");
  89.     }
  90.     Serial.println("Connected to WiFi");
  91.  
  92.     // Start the server
  93.     server.on("/", handleRoot);
  94.     server.begin();
  95.     Serial.println("HTTP server started");
  96.  
  97.     // Attach the servo to the PWM pin
  98.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D3);
  99.     myServo.write(doorLocked ? 0 : 180); // Set initial position based on lock state
  100.  
  101.     // Initialize keypad pins
  102.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  103.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  104.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  105.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  106.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  107.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  108.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  109. }
  110.  
  111. void loop(void) {
  112.     // Handle client requests
  113.     server.handleClient();
  114.  
  115.     // Check for keypad input
  116.     char key = keypad.getKey();
  117.     if (key) {
  118.         // Check if the entered key is the correct PIN (e.g., "1234")
  119.         if (key == '#') { // Assuming '#' is used to submit the PIN
  120.             // Toggle the door state
  121.             doorLocked = !doorLocked;
  122.             myServo.write(doorLocked ? 0 : 180); // Move servo to lock/unlock position
  123.             updateOutputs(); // Refresh output data
  124.         }
  125.     }
  126. }
  127.  
  128. void updateOutputs() {
  129.     // Update the web server with the current state
  130.     server.send(200, "text/html", "<h1>Door is " + String(doorLocked ? "Locked" : "Unlocked") + "</h1>");
  131. }
  132.  
  133. void handleRoot() {
  134.     // Handle the root URL
  135.     server.send(200, "text/html", "<h1>Welcome to the Door Lock System</h1><p>Door is " + String(doorLocked ? "Locked" : "Unlocked") + "</p>");
  136. }
  137.  
  138. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement