Advertisement
pleasedontcode

WiFi Doorlock rev_112

Oct 7th, 2024
52
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: WiFi Doorlock
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-07 21:43:37
  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. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Deneyap_Servo.h>  // Servo control library
  34. #include <Keypad.h> // Keypad library
  35. #include <WiFi.h>   // WiFi library for web server
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void updateOutputs();
  41. void handleClient();
  42. char getKey(); // Added function prototype for getKey()
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  46. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  47. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  48. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  49. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  50.  
  51. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  52. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  53. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  54. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  55.  
  56. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  58. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  59. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  60.  
  61. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  62. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  63. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  64. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  65.  
  66. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  67. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  68. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  69.     {'1','2','3'},
  70.     {'4','5','6'},
  71.     {'7','8','9'},
  72.     {'*','0','#'},
  73. };
  74.  
  75. // Define the correct PIN code
  76. const String correctPin = "1234"; // Example PIN code
  77. String inputPin = ""; // To store user input
  78.  
  79. // Servo object
  80. Deneyap_Servo myServo;
  81.  
  82. // WiFi credentials
  83. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  84. const char* password = "your_PASSWORD"; // Replace with your WiFi password
  85.  
  86. WiFiServer server(80); // Create a server that listens on port 80
  87.  
  88. void setup(void)
  89. {
  90.     // Initialize servo
  91.     myServo.attach(9); // Attach servo to pin 9
  92.     myServo.write(0); // Start with the door locked
  93.  
  94.     // Initialize keypad pins
  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.     // Start WiFi connection
  106.     WiFi.begin(ssid, password);
  107.     while (WiFi.status() != WL_CONNECTED) {
  108.         delay(1000);
  109.     }
  110.     server.begin(); // Start the server
  111. }
  112.  
  113. void loop(void)
  114. {
  115.     updateOutputs(); // Refresh output data
  116.     handleClient(); // Handle incoming client requests
  117.  
  118.     // Check if a key is pressed
  119.     char key = getKey();
  120.     if (key) {
  121.         if (key == '#') { // If '#' is pressed, check the PIN
  122.             if (inputPin == correctPin) {
  123.                 if (myServo.read() == 0) {
  124.                     myServo.write(90); // Unlock the door
  125.                 } else {
  126.                     myServo.write(0); // Lock the door
  127.                 }
  128.             }
  129.             inputPin = ""; // Reset input
  130.         } else {
  131.             inputPin += key; // Add key to input
  132.         }
  133.     }
  134. }
  135.  
  136. void updateOutputs()
  137. {
  138.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  139.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  140.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  141. }
  142.  
  143. void handleClient() {
  144.     WiFiClient client = server.available(); // Listen for incoming clients
  145.     if (client) {
  146.         String currentState = (myServo.read() == 0) ? "Locked" : "Unlocked";
  147.         client.println("HTTP/1.1 200 OK");
  148.         client.println("Content-type:text/html");
  149.         client.println();
  150.         client.println("<html><body>");
  151.         client.println("<h1>Door is " + currentState + "</h1>");
  152.         client.println("</body></html>");
  153.         client.stop(); // Close the connection
  154.     }
  155. }
  156.  
  157. char getKey() {
  158.     // Implement keypad reading logic here
  159.     // This function should return the pressed key character
  160.     // For simplicity, you can use a library function if available
  161.     return '\0'; // Placeholder return
  162. }
  163.  
  164. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement