Advertisement
pleasedontcode

Smart Lock rev_93

Oct 6th, 2024
113
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:56:42
  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. char getKey(); // Function to get key from keypad
  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 OUTPUT RAW VARIABLES *****/
  58. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  59. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  60. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  61.  
  62. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  63. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  64. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  65. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  66.  
  67. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  68. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  69. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  70.     {'1','2','3'},
  71.     {'4','5','6'},
  72.     {'7','8','9'},
  73.     {'*','0','#'},
  74. };
  75.  
  76. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  77. // Instantiate the servo object
  78. Deneyap_Servo myServo;
  79.  
  80. // Define the correct PIN code
  81. const String correctPIN = "1234"; // Example PIN code
  82. String enteredPIN = ""; // To store the entered PIN
  83. bool doorLocked = true; // Initial state of the door
  84.  
  85. // WiFi credentials
  86. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  87. const char* password = "your_PASSWORD"; // Replace with your WiFi Password
  88.  
  89. // Web server on port 80
  90. WiFiServer server(80);
  91.  
  92. void setup(void)
  93. {
  94.     // put your setup code here, to run once:
  95.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  96.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  97.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  98.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  99.     pinMode(pinButton_PushButton_PIN_D2, INPUT_PULLUP);
  100.  
  101.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  102.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  103.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  104.  
  105.     // Attach the servo to a pin
  106.     myServo.attach(11); // Assuming servo is connected to pin 11
  107.  
  108.     // Connect to WiFi
  109.     WiFi.begin(ssid, password);
  110.     while (WiFi.status() != WL_CONNECTED) {
  111.         delay(1000);
  112.     }
  113.     server.begin(); // Start the server
  114. }
  115.  
  116. void loop(void)
  117. {
  118.     // put your main code here, to run repeatedly:
  119.     updateOutputs(); // Refresh output data
  120.     handleClient(); // Handle web client requests
  121.  
  122.     // Check keypad input
  123.     char key = getKey(); // Function to get key from keypad
  124.     if (key) {
  125.         enteredPIN += key; // Append the pressed key to enteredPIN
  126.         if (key == '#') { // Check if '#' is pressed to submit the PIN
  127.             if (enteredPIN.substring(0, enteredPIN.length() - 1) == correctPIN) {
  128.                 // Toggle lock state
  129.                 doorLocked = !doorLocked;
  130.                 myServo.write(doorLocked ? 0 : 90); // Lock or unlock the servo
  131.             }
  132.             enteredPIN = ""; // Reset entered PIN
  133.         }
  134.     }
  135. }
  136.  
  137. void updateOutputs()
  138. {
  139.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  140.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  141.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  142. }
  143.  
  144. void handleClient() {
  145.     WiFiClient client = server.available(); // Check for incoming clients
  146.     if (client) {
  147.         String currentLine = ""; // String to hold incoming data
  148.         while (client.connected()) {
  149.             if (client.available()) {
  150.                 char c = client.read(); // Read a byte
  151.                 if (c == '\n') {
  152.                     // If the current line is blank, you got a GET request
  153.                     if (currentLine.length() == 0) {
  154.                         // Send HTTP headers
  155.                         client.println("HTTP/1.1 200 OK");
  156.                         client.println("Content-type:text/html");
  157.                         client.println();
  158.                         // Send the HTML page
  159.                         client.println("<html><body>");
  160.                         client.println("<h1>Door Lock Status</h1>");
  161.                         client.println("<p>The door is currently " + String(doorLocked ? "locked" : "unlocked") + ".</p>");
  162.                         client.println("</body></html>");
  163.                         client.println();
  164.                         break;
  165.                     } else {
  166.                         currentLine = ""; // Clear the current line
  167.                     }
  168.                 } else if (c != '\r') {
  169.                     currentLine += c; // Add to the current line
  170.                 }
  171.             }
  172.         }
  173.         client.stop(); // Close the connection
  174.     }
  175. }
  176.  
  177. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement