Advertisement
pleasedontcode

Smart Lock rev_86

Oct 6th, 2024
87
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: Smart Lock
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-06 10:04:16
  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 <EasyButton.h> //https://github.com/evert-arias/EasyButton
  34. #include <LcdKeypad.h>  //https://github.com/dniklaus/arduino-display-lcdkeypad
  35. #include <XYZrobotServo.h>  //https://github.com/pololu/xyzrobot-servo-arduino
  36. #include <WiFi.h> // Include WiFi library for web server
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41. void updateOutputs();
  42. void handleClient(); // Function to handle web client requests
  43. bool checkForCorrectPIN(); // Function to check for correct PIN
  44. void toggleDoorLock(); // Function to toggle the door lock state
  45.  
  46. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  47. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  48. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  49. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  50. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  51. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  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 OUTPUT RAW VARIABLES *****/
  59. bool    keypad_Keypad3x4_C1_PIN_D8_rawData      = 0;
  60. bool    keypad_Keypad3x4_C2_PIN_D9_rawData      = 0;
  61. bool    keypad_Keypad3x4_C3_PIN_D10_rawData     = 0;
  62.  
  63. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  64. float   keypad_Keypad3x4_C1_PIN_D8_phyData      = 0.0;
  65. float   keypad_Keypad3x4_C2_PIN_D9_phyData      = 0.0;
  66. float   keypad_Keypad3x4_C3_PIN_D10_phyData     = 0.0;
  67.  
  68. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  69. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  70. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  71.     {'1','2','3'},
  72.     {'4','5','6'},
  73.     {'7','8','9'},
  74.     {'*','0','#'},
  75. };
  76.  
  77. // Create Servo object
  78. Deneyap_Servo servoMotor;
  79.  
  80. // WiFi credentials
  81. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  82. const char* password = "your_PASSWORD"; // Replace with your WiFi Password
  83.  
  84. WiFiServer server(80); // Create a server that listens on port 80
  85. bool doorLocked = true; // Initial state of the door
  86.  
  87. void setup(void)
  88. {
  89.     // put your setup code here, to run once:
  90.  
  91.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  92.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  93.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  94.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  95.     pinMode(pinButton_PushButton_PIN_D2,    INPUT_PULLUP);
  96.  
  97.     pinMode(keypad_Keypad3x4_C1_PIN_D8,  OUTPUT);
  98.     pinMode(keypad_Keypad3x4_C2_PIN_D9,  OUTPUT);
  99.     pinMode(keypad_Keypad3x4_C3_PIN_D10,     OUTPUT);
  100.  
  101.     // Initialize the servo motor
  102.     servoMotor.attach(3); // Attach the servo to pin 3
  103.  
  104.     // Start WiFi
  105.     WiFi.begin(ssid, password);
  106.     while (WiFi.status() != WL_CONNECTED) {
  107.         delay(1000);
  108.     }
  109.     server.begin(); // Start the server
  110. }
  111.  
  112. void loop(void)
  113. {
  114.     // put your main code here, to run repeatedly:
  115.     updateOutputs(); // Refresh output data
  116.     handleClient(); // Handle incoming web client requests
  117. }
  118.  
  119. void updateOutputs()
  120. {
  121.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  122.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  123.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  124.  
  125.     // Check for correct PIN input
  126.     if (checkForCorrectPIN()) {
  127.         toggleDoorLock(); // Toggle the door lock state
  128.     }
  129. }
  130.  
  131. bool checkForCorrectPIN() {
  132.     // Implement your logic to check for the correct PIN code
  133.     // Return true if the correct PIN is entered, otherwise false
  134.     // Example: Compare entered PIN with a predefined PIN
  135.     const char* correctPIN = "1234"; // Example correct PIN
  136.     String enteredPIN = ""; // Variable to store entered PIN
  137.  
  138.     // Logic to read from keypad and store in enteredPIN
  139.     // ...
  140.  
  141.     return (enteredPIN.equals(correctPIN)); // Check if entered PIN matches
  142. }
  143.  
  144. void toggleDoorLock() {
  145.     doorLocked = !doorLocked; // Toggle the lock state
  146.     if (doorLocked) {
  147.         servoMotor.write(0); // Lock the door
  148.     } else {
  149.         servoMotor.write(90); // Unlock the door
  150.     }
  151. }
  152.  
  153. void handleClient() {
  154.     WiFiClient client = server.available(); // Check if a client has connected
  155.     if (client) {
  156.         String currentLine = ""; // Make a String to hold incoming data
  157.         while (client.connected()) {
  158.             if (client.available()) {
  159.                 char c = client.read(); // Read a byte
  160.                 if (c == '\n') {
  161.                     // If the current line is blank, you got two newline characters in a row.
  162.                     if (currentLine.length() == 0) {
  163.                         // Send HTTP headers
  164.                         client.println("HTTP/1.1 200 OK");
  165.                         client.println("Content-type:text/html");
  166.                         client.println();
  167.                         // Send the HTML page
  168.                         client.println("<!DOCTYPE HTML>");
  169.                         client.println("<html>");
  170.                         client.println("<h1>Door Lock State</h1>");
  171.                         client.println(doorLocked ? "<p>The door is locked.</p>" : "<p>The door is unlocked.</p>");
  172.                         client.println("</html>");
  173.                         client.println();
  174.                         break;
  175.                     } else {
  176.                         currentLine = ""; // Clear the current line
  177.                     }
  178.                 } else if (c != '\r') {
  179.                     currentLine += c; // Add to the current line
  180.                 }
  181.             }
  182.         }
  183.         client.stop(); // Close the connection
  184.     }
  185. }
  186.  
  187. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement