Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Keypad Lock**
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2024-10-05 07:01:20
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The servo motor installed in the door will lock or */
- /* unlock based on the correct PIN code received by */
- /* the button. If the door is unlocked and the PIN is */
- /* correct, it will lock. If the door is locked and */
- /* the PIN is correct, it will unlock. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* The webserver provides a web page about lock or */
- /* unlock state of the door. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Deneyap_Servo.h> // https://github.com/deneyapkart/deneyap-servo-arduino-library
- #include <Keypad.h> // https://github.com/Chris--A/Keypad
- #include <WiFi.h> // Include WiFi library for web server
- #include <WebServer.h> // Include WebServer library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- void handleRoot(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
- const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
- const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
- const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
- const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
- const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
- /***** KEYPAD CONFIGURATION *****/
- const uint8_t KEYPAD3X4_ROWS = 4; // four rows
- const uint8_t KEYPAD3X4_COLS = 3; // three columns
- char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'},
- {'*','0','#'},
- };
- // Create keypad object
- Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
- // Create servo object
- Servo myServo;
- // Web server on port 80
- WebServer server(80);
- // State variables
- bool doorLocked = true; // Door starts locked
- const char* ssid = "your_SSID"; // Replace with your network SSID
- const char* password = "your_PASSWORD"; // Replace with your network password
- void setup(void) {
- // Initialize serial communication
- Serial.begin(115200);
- // Set up WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- // Start the server
- server.on("/", handleRoot);
- server.begin();
- Serial.println("HTTP server started");
- // Attach the servo to the PWM pin
- myServo.attach(servo_Servomotor_PWMSignal_PIN_D3);
- myServo.write(doorLocked ? 0 : 180); // Set initial position based on lock state
- // Initialize keypad pins
- pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
- pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
- pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
- pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
- pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
- pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
- pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
- }
- void loop(void) {
- // Handle client requests
- server.handleClient();
- // Check for keypad input
- char key = keypad.getKey();
- if (key) {
- // Check if the entered key is the correct PIN (e.g., "1234")
- if (key == '#') { // Assuming '#' is used to submit the PIN
- // Toggle the door state
- doorLocked = !doorLocked;
- myServo.write(doorLocked ? 0 : 180); // Move servo to lock/unlock position
- updateOutputs(); // Refresh output data
- }
- }
- }
- void updateOutputs() {
- // Update the web server with the current state
- server.send(200, "text/html", "<h1>Door is " + String(doorLocked ? "Locked" : "Unlocked") + "</h1>");
- }
- void handleRoot() {
- // Handle the root URL
- server.send(200, "text/html", "<h1>Welcome to the Door Lock System</h1><p>Door is " + String(doorLocked ? "Locked" : "Unlocked") + "</p>");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement