Advertisement
pleasedontcode

**Lock Control** rev_63

Oct 5th, 2024
65
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 Control**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-05 14:36:48
  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>  // Servo control library
  32. #include <Keypad.h> // Keypad library for PIN entry
  33. #include <EasyButton.h> // EasyButton library for button handling
  34. #include <WiFi.h> // WiFi library for web server
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void handleButtonPress();
  40. void updateWebServer();
  41. void unlockDoor();
  42. void lockDoor();
  43. bool checkPIN(const String& pin);
  44.  
  45. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  46. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  47. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  48. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  49. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  50. const uint8_t pinButton_PushButton_PIN_D2 = 2;
  51.  
  52. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  53. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  54. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  55. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  56.  
  57. /***** DEFINITION OF KEYPAD VARIABLES *****/
  58. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  59. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  60. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  61.     {'1','2','3'},
  62.     {'4','5','6'},
  63.     {'7','8','9'},
  64.     {'*','0','#'},
  65. };
  66. Keypad keypad(Keys_Keypad3x4, keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  67.  
  68. /***** DEFINITION OF SERVO VARIABLES *****/
  69. Servo doorServo; // Servo object for the door lock
  70. bool doorLocked = true; // Initial state of the door
  71.  
  72. /***** DEFINITION OF BUTTON VARIABLES *****/
  73. EasyButton button(pinButton_PushButton_PIN_D2); // Button object
  74.  
  75. /***** DEFINITION OF WIFI VARIABLES *****/
  76. const char* ssid = "your_SSID"; // Replace with your network SSID
  77. const char* password = "your_PASSWORD"; // Replace with your network password
  78. WiFiServer server(80); // Create a web server on port 80
  79.  
  80. // Define the correct PIN code
  81. const String correctPIN = "1234"; // Example PIN code
  82.  
  83. void setup(void)
  84. {
  85.     // Initialize Serial for debugging
  86.     Serial.begin(115200);
  87.  
  88.     // Initialize the servo
  89.     doorServo.attach(9); // Attach the servo to pin 9
  90.     lockDoor(); // Start with the door locked
  91.  
  92.     // Setup keypad pins
  93.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  94.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  95.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  96.  
  97.     // Setup button
  98.     button.begin();
  99.     button.onPressed(handleButtonPress); // Set button press handler
  100.  
  101.     // Connect to WiFi
  102.     WiFi.begin(ssid, password);
  103.     while (WiFi.status() != WL_CONNECTED) {
  104.         delay(1000);
  105.         Serial.println("Connecting to WiFi...");
  106.     }
  107.     Serial.println("Connected to WiFi");
  108.     server.begin(); // Start the web server
  109. }
  110.  
  111. void loop(void)
  112. {
  113.     // Read keypad input
  114.     char key = keypad.getKey();
  115.     static String enteredPIN = ""; // Store the entered PIN
  116.  
  117.     if (key) {
  118.         // Handle key press for PIN entry
  119.         if (key == '#') {
  120.             // Check if the entered PIN is correct
  121.             if (checkPIN(enteredPIN)) {
  122.                 // Toggle door state
  123.                 if (doorLocked) {
  124.                     unlockDoor();
  125.                 } else {
  126.                     lockDoor();
  127.                 }
  128.             }
  129.             enteredPIN = ""; // Clear the entered PIN
  130.         } else {
  131.             // Append the pressed key to the entered PIN
  132.             enteredPIN += key;
  133.         }
  134.     }
  135.  
  136.     // Read button state
  137.     button.read();
  138.  
  139.     // Handle web server requests
  140.     WiFiClient client = server.available();
  141.     if (client) {
  142.         // Process the client request
  143.         updateWebServer();
  144.     }
  145. }
  146.  
  147. void handleButtonPress() {
  148.     // Toggle door state on button press
  149.     if (doorLocked) {
  150.         unlockDoor();
  151.     } else {
  152.         lockDoor();
  153.     }
  154. }
  155.  
  156. void unlockDoor() {
  157.     doorServo.write(90); // Move servo to unlock position
  158.     doorLocked = false; // Update door state
  159.     Serial.println("Door unlocked");
  160. }
  161.  
  162. void lockDoor() {
  163.     doorServo.write(0); // Move servo to lock position
  164.     doorLocked = true; // Update door state
  165.     Serial.println("Door locked");
  166. }
  167.  
  168. bool checkPIN(const String& pin) {
  169.     return pin == correctPIN; // Check if entered PIN matches the correct PIN
  170. }
  171.  
  172. void updateWebServer() {
  173.     // Handle web server requests and send the lock/unlock state
  174.     WiFiClient client = server.available();
  175.     if (client) {
  176.         String currentLine = "";
  177.         while (client.connected()) {
  178.             if (client.available()) {
  179.                 char c = client.read();
  180.                 if (c == '\n') {
  181.                     // Send response
  182.                     client.println("HTTP/1.1 200 OK");
  183.                     client.println("Content-Type: text/html");
  184.                     client.println("Connection: close");
  185.                     client.println();
  186.                     client.println("<!DOCTYPE HTML>");
  187.                     client.println("<html>");
  188.                     client.print("Door is currently: ");
  189.                     client.print(doorLocked ? "Locked" : "Unlocked");
  190.                     client.println("</html>");
  191.                     break;
  192.                 } else {
  193.                     currentLine += c;
  194.                 }
  195.             }
  196.         }
  197.         client.stop();
  198.     }
  199. }
  200.  
  201. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement