Advertisement
pleasedontcode

WiFi Lock rev_99

Oct 6th, 2024
55
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 Lock
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-06 15:43:45
  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 <XYZrobotServo.h>  //https://github.com/pololu/xyzrobot-servo-arduino
  35. #include <LcdKeypad.h>  //https://github.com/dniklaus/arduino-display-lcdkeypad
  36. #include <WiFi.h> // Include WiFi library for web server
  37. #include <WebServer.h> // Include WebServer library
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42. void updateOutputs();
  43. void handleRoot(); // Function to handle root URL
  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. // Servo object for locking/unlocking the door
  77. Deneyap_Servo doorServo;
  78.  
  79. // Web server object
  80. WebServer server(80);
  81.  
  82. // PIN code for unlocking the door
  83. const String correctPIN = "1234"; // Set your desired PIN code
  84. String enteredPIN = ""; // Store entered PIN
  85.  
  86. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  87.  
  88. void setup(void)
  89. {
  90.     // put your setup code here, to run once:
  91.  
  92.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  93.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  94.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  95.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  96.     pinMode(pinButton_PushButton_PIN_D2,    INPUT_PULLUP);
  97.  
  98.     pinMode(keypad_Keypad3x4_C1_PIN_D8,  OUTPUT);
  99.     pinMode(keypad_Keypad3x4_C2_PIN_D9,  OUTPUT);
  100.     pinMode(keypad_Keypad3x4_C3_PIN_D10,     OUTPUT);
  101.  
  102.     // Initialize the servo
  103.     doorServo.attach(11); // Attach the servo to pin 11 (change as needed)
  104.     doorServo.write(0); // Start with the door locked
  105.  
  106.     // Initialize WiFi
  107.     WiFi.begin("yourSSID", "yourPASSWORD"); // Replace with your WiFi credentials
  108.     while (WiFi.status() != WL_CONNECTED) {
  109.         delay(1000);
  110.     }
  111.     server.on("/", handleRoot); // Handle root URL
  112.     server.begin(); // Start the server
  113. }
  114.  
  115. void loop(void)
  116. {
  117.     // put your main code here, to run repeatedly:
  118.     updateOutputs(); // Refresh output data
  119.     server.handleClient(); // Handle client requests
  120.  
  121.     // Check for key presses
  122.     char key = keypad.getKey();
  123.     if (key) {
  124.         if (key == '#') { // If '#' is pressed, check the entered PIN
  125.             if (enteredPIN == correctPIN) {
  126.                 // Toggle the lock state
  127.                 if (doorServo.read() == 0) {
  128.                     doorServo.write(90); // Unlock the door
  129.                 } else {
  130.                     doorServo.write(0); // Lock the door
  131.                 }
  132.             }
  133.             enteredPIN = ""; // Reset entered PIN after checking
  134.         } else {
  135.             enteredPIN += key; // Append the pressed key to the entered PIN
  136.         }
  137.     }
  138. }
  139.  
  140. void updateOutputs()
  141. {
  142.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  143.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  144.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  145. }
  146.  
  147. void handleRoot() {
  148.     String state = (doorServo.read() == 0) ? "Locked" : "Unlocked"; // Check lock state
  149.     String message = "<html><body><h1>Door is " + state + "</h1></body></html>";
  150.     server.send(200, "text/html", message); // Send the HTML response
  151. }
  152.  
  153. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement