Advertisement
pleasedontcode

**Door Lock** rev_48

Oct 5th, 2024
61
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: **Door Lock**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-05 07:07:35
  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 that enable introduction of pin into keypad.
  34. #### Feedback 2 ####
  35. - complete getKeypadKey logic.
  36. ********* User code review feedback **********/
  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.  
  43. /****** FUNCTION PROTOTYPES *****/
  44. void setup(void);
  45. void loop(void);
  46. void updateOutputs(void);
  47. char getKeypadKey(void);
  48.  
  49. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  50. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  51. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  52. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  53. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  54.  
  55. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  56. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  57. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  58. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  59.  
  60. /***** DEFINITION OF PWM OUTPUT PINS *****/
  61. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  62.  
  63. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  64. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  65. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  66. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  67. uint8_t servo_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  68.  
  69. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  70. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  71. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  72. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  73. float servo_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
  74.  
  75. // Define keypad layout
  76. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  77. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  78. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  79.     {'1', '2', '3'},
  80.     {'4', '5', '6'},
  81.     {'7', '8', '9'},
  82.     {'*', '0', '#'},
  83. };
  84.  
  85. // Define servo object
  86. Servo myServo;
  87.  
  88. // Define WiFi credentials
  89. const char* ssid = "your_SSID"; // Replace with your network SSID
  90. const char* password = "your_PASSWORD"; // Replace with your network password
  91.  
  92. // Define web server on port 80
  93. WiFiServer server(80);
  94.  
  95. // Variable to store the current lock state
  96. bool isLocked = true; // Start with the door locked
  97.  
  98. void setup(void) {
  99.     // Initialize serial communication
  100.     Serial.begin(115200);
  101.  
  102.     // Setup keypad pins
  103.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  104.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  105.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  106.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  107.  
  108.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  109.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  110.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  111.     pinMode(servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  112.  
  113.     // Attach the servo to the PWM pin
  114.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D3);
  115.  
  116.     // Connect to Wi-Fi
  117.     WiFi.begin(ssid, password);
  118.     while (WiFi.status() != WL_CONNECTED) {
  119.         delay(1000);
  120.         Serial.println("Connecting to WiFi...");
  121.     }
  122.     Serial.println("Connected to WiFi");
  123.     server.begin();
  124. }
  125.  
  126. void loop(void) {
  127.     // Refresh output data
  128.     updateOutputs();
  129.  
  130.     // Check for keypad input
  131.     char key = getKeypadKey();
  132.     if (key == '1') {
  133.         // Example PIN code to unlock
  134.         if (isLocked) {
  135.             isLocked = false;
  136.             myServo.write(90); // Unlock position
  137.             Serial.println("Door Unlocked");
  138.         } else {
  139.             isLocked = true;
  140.             myServo.write(0); // Lock position
  141.             Serial.println("Door Locked");
  142.         }
  143.     }
  144.  
  145.     // Handle web server requests
  146.     WiFiClient client = server.available();
  147.     if (client) {
  148.         String currentLine = "";
  149.         while (client.connected()) {
  150.             if (client.available()) {
  151.                 char c = client.read();
  152.                 if (c == '\n') {
  153.                     // Send HTTP response
  154.                     client.println("HTTP/1.1 200 OK");
  155.                     client.println("Content-type:text/html");
  156.                     client.println();
  157.                     client.println("<!DOCTYPE HTML>");
  158.                     client.println("<html>");
  159.                     client.println("<h1>Door Lock Status</h1>");
  160.                     client.println(isLocked ? "<p>The door is locked.</p>" : "<p>The door is unlocked.</p>");
  161.                     client.println("</html>");
  162.                     break;
  163.                 } else {
  164.                     currentLine += c;
  165.                 }
  166.             }
  167.         }
  168.         client.stop();
  169.     }
  170. }
  171.  
  172. void updateOutputs() {
  173.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  174.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  175.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  176.     analogWrite(servo_Servomotor_PWMSignal_PIN_D3, servo_Servomotor_PWMSignal_PIN_D3_rawData);
  177. }
  178.  
  179. char getKeypadKey() {
  180.     // Logic to read the keypad and return the pressed key
  181.     for (uint8_t col = 0; col < KEYPAD3X4_COLS; col++) {
  182.         digitalWrite(keypad_Keypad3x4_C1_PIN_D8 + col, LOW); // Set the current column to LOW
  183.         for (uint8_t row = 0; row < KEYPAD3X4_ROWS; row++) {
  184.             if (digitalRead(keypad_Keypad3x4_R1_PIN_D4 + row) == LOW) {
  185.                 // Wait for the key to be released
  186.                 while (digitalRead(keypad_Keypad3x4_R1_PIN_D4 + row) == LOW);
  187.                 digitalWrite(keypad_Keypad3x4_C1_PIN_D8 + col, HIGH); // Set the column back to HIGH
  188.                 return Keys_Keypad3x4[row][col]; // Return the pressed key
  189.             }
  190.         }
  191.         digitalWrite(keypad_Keypad3x4_C1_PIN_D8 + col, HIGH); // Set the column back to HIGH
  192.     }
  193.     return '\0'; // No key pressed
  194. }
  195.  
  196. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement