Advertisement
pleasedontcode

**Lock Control** rev_50

Oct 5th, 2024
85
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 07:42:22
  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. ********* User code review feedback **********/
  35.  
  36. /****** DEFINITION OF LIBRARIES *****/
  37. #include <Deneyap_Servo.h>    // Library for controlling servo motors
  38. #include <Keypad.h>           // Library for keypad input
  39. #include <WiFi.h>            // Library for WiFi functionality
  40. #include <WebServer.h>        // Library for web server functionality
  41.  
  42. /****** FUNCTION PROTOTYPES *****/
  43. void setup(void);
  44. void loop(void);
  45. void handleRoot();          // Function to handle root URL
  46.  
  47. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  48. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  49. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  50. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  51. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  52.  
  53. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  54. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  55. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  56. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  57.  
  58. /***** DEFINITION OF PWM OUTPUT PINS *****/
  59. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  60.  
  61. /***** DEFINITION OF KEYPAD *****/
  62. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  63. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  64. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  65.     {'1', '2', '3'},
  66.     {'4', '5', '6'},
  67.     {'7', '8', '9'},
  68.     {'*', '0', '#'},
  69. };
  70.  
  71. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  72. Servo myServo; // Servo object
  73.  
  74. /***** DEFINITION OF VARIABLES *****/
  75. const char* ssid = "your_SSID"; // Your WiFi SSID
  76. const char* password = "your_PASSWORD"; // Your WiFi Password
  77. WebServer server(80); // Create a web server on port 80
  78.  
  79. bool doorLocked = true; // Initial state of the door
  80. const String correctPIN = "1234"; // Correct PIN code
  81. String inputPIN = ""; // User input PIN
  82. bool pinEntryActive = false; // Flag to indicate if PIN entry is active
  83.  
  84. void setup(void) {
  85.     Serial.begin(115200); // Start serial communication
  86.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attach the servo to the PWM pin
  87.     myServo.write(0); // Initially lock the door
  88.  
  89.     // Set up keypad pins
  90.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  91.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  92.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  93.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  94.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  95.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  96.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  97.  
  98.     // Connect to WiFi
  99.     WiFi.begin(ssid, password);
  100.     while (WiFi.status() != WL_CONNECTED) {
  101.         delay(1000);
  102.         Serial.println("Connecting to WiFi...");
  103.     }
  104.     Serial.println("Connected to WiFi");
  105.  
  106.     // Set up web server
  107.     server.on("/", handleRoot); // Handle root URL
  108.     server.begin(); // Start the server
  109. }
  110.  
  111. void loop(void) {
  112.     server.handleClient(); // Handle client requests
  113.     char key = keypad.getKey(); // Get the pressed key
  114.  
  115.     if (key) {
  116.         if (key == '*') { // If '*' is pressed, activate PIN entry
  117.             pinEntryActive = true; // Set flag to true to start PIN entry
  118.             inputPIN = ""; // Reset input
  119.             Serial.println("PIN entry activated. Please enter your PIN.");
  120.         } else if (pinEntryActive) {
  121.             if (key == '#') { // If '#' is pressed, check the PIN
  122.                 if (inputPIN == correctPIN) {
  123.                     doorLocked = !doorLocked; // Toggle lock state
  124.                     myServo.write(doorLocked ? 0 : 90); // Lock or unlock the door
  125.                     Serial.println("PIN accepted. Door state changed.");
  126.                 } else {
  127.                     Serial.println("Incorrect PIN. Try again.");
  128.                 }
  129.                 pinEntryActive = false; // Deactivate PIN entry
  130.                 inputPIN = ""; // Reset input
  131.             } else {
  132.                 inputPIN += key; // Append key to input PIN
  133.             }
  134.         }
  135.     }
  136. }
  137.  
  138. void handleRoot() {
  139.     String message = "Door is currently " + String(doorLocked ? "Locked" : "Unlocked");
  140.     server.send(200, "text/html", message); // Send response to the client
  141. }
  142.  
  143. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement