Advertisement
pleasedontcode

**Lock System** rev_88

Oct 6th, 2024
132
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-06 10:19: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.  
  31. /********* User code review feedback **********
  32. #### Feedback 1 ####
  33. - use keypad to get pin.
  34. ********* User code review feedback **********/
  35.  
  36. /****** DEFINITION OF LIBRARIES *****/
  37. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  38. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  39. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  40. #include <LcdKeypad.h>  //https://github.com/dniklaus/arduino-display-lcdkeypad
  41. #include <XYZrobotServo.h>  //https://github.com/pololu/xyzrobot-servo-arduino
  42. #include <WiFi.h> // Include WiFi library for web server
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47. void updateOutputs();
  48. void handleClient(); // Function to handle web client requests
  49. bool checkForCorrectPIN(); // Function to check for correct PIN
  50. void toggleDoorLock(); // Function to toggle the door lock state
  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 pinButton_PushButton_PIN_D2       = 2;
  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 OUTPUT RAW VARIABLES *****/
  65. bool    keypad_Keypad3x4_C1_PIN_D8_rawData      = 0;
  66. bool    keypad_Keypad3x4_C2_PIN_D9_rawData      = 0;
  67. bool    keypad_Keypad3x4_C3_PIN_D10_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.  
  74. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  75. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  76. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  77.     {'1','2','3'},
  78.     {'4','5','6'},
  79.     {'7','8','9'},
  80.     {'*','0','#'},
  81. };
  82.  
  83. // Create Servo object
  84. Servo servoMotor; // Updated from Deneyap_Servo to Servo
  85.  
  86. // Create Keypad object
  87. Keypad keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  88.  
  89. // WiFi credentials
  90. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  91. const char* password = "your_PASSWORD"; // Replace with your WiFi Password
  92.  
  93. WiFiServer server(80); // Create a server that listens on port 80
  94. bool doorLocked = true; // Initial state of the door
  95.  
  96. void setup(void)
  97. {
  98.     // put your setup code here, to run once:
  99.  
  100.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  101.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  102.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  103.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  104.     pinMode(pinButton_PushButton_PIN_D2,    INPUT_PULLUP);
  105.  
  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.     // Initialize the servo motor
  111.     servoMotor.attach(3); // Attach the servo to pin 3
  112.  
  113.     // Start WiFi
  114.     WiFi.begin(ssid, password);
  115.     while (WiFi.status() != WL_CONNECTED) {
  116.         delay(1000);
  117.     }
  118.     server.begin(); // Start the server
  119. }
  120.  
  121. void loop(void)
  122. {
  123.     // put your main code here, to run repeatedly:
  124.     updateOutputs(); // Refresh output data
  125.     handleClient(); // Handle incoming web client requests
  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.     // Check for correct PIN input
  135.     if (checkForCorrectPIN()) {
  136.         toggleDoorLock(); // Toggle the door lock state
  137.     }
  138. }
  139.  
  140. bool checkForCorrectPIN() {
  141.     const char* correctPIN = "1234"; // Example correct PIN
  142.     String enteredPIN = ""; // Variable to store entered PIN
  143.     char key;
  144.  
  145.     // Read from keypad and store in enteredPIN
  146.     while (enteredPIN.length() < 4) { // Assuming a 4-digit PIN
  147.         key = keypad.getKey();
  148.         if (key) {
  149.             if (key == '*') {
  150.                 enteredPIN = ""; // Reset if '*' is pressed
  151.             } else if (key == '#') {
  152.                 break; // End input on '#'
  153.             } else {
  154.                 enteredPIN += key; // Append key to enteredPIN
  155.             }
  156.         }
  157.     }
  158.  
  159.     return (enteredPIN.equals(correctPIN)); // Check if entered PIN matches
  160. }
  161.  
  162. void toggleDoorLock() {
  163.     doorLocked = !doorLocked; // Toggle the lock state
  164.     if (doorLocked) {
  165.         servoMotor.write(0); // Lock the door
  166.     } else {
  167.         servoMotor.write(90); // Unlock the door
  168.     }
  169. }
  170.  
  171. void handleClient() {
  172.     WiFiClient client = server.available(); // Check if a client has connected
  173.     if (client) {
  174.         String currentLine = ""; // Make a String to hold incoming data
  175.         while (client.connected()) {
  176.             if (client.available()) {
  177.                 char c = client.read(); // Read a byte
  178.                 if (c == '\n') {
  179.                     // If the current line is blank, you got two newline characters in a row.
  180.                     if (currentLine.length() == 0) {
  181.                         // Send HTTP headers
  182.                         client.println("HTTP/1.1 200 OK");
  183.                         client.println("Content-type:text/html");
  184.                         client.println();
  185.                         // Send the HTML page
  186.                         client.println("<!DOCTYPE HTML>");
  187.                         client.println("<html>");
  188.                         client.println("<h1>Door Lock State</h1>");
  189.                         client.println(doorLocked ? "<p>The door is locked.</p>" : "<p>The door is unlocked.</p>");
  190.                         client.println("</html>");
  191.                         client.println();
  192.                         break;
  193.                     } else {
  194.                         currentLine = ""; // Clear the current line
  195.                     }
  196.                 } else if (c != '\r') {
  197.                     currentLine += c; // Add to the current line
  198.                 }
  199.             }
  200.         }
  201.         client.stop(); // Close the connection
  202.     }
  203. }
  204.  
  205. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement