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: **Lock Control**
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2024-10-05 07:42:22
- ********* 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 *****/
- /********* User code review feedback **********
- #### Feedback 1 ####
- - add a button which enable the insertion of PIN code.
- ********* User code review feedback **********/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Deneyap_Servo.h> // Library for controlling servo motors
- #include <Keypad.h> // Library for keypad input
- #include <WiFi.h> // Library for WiFi functionality
- #include <WebServer.h> // Library for web server functionality
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void handleRoot(); // Function to handle root URL
- /***** 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;
- /***** DEFINITION OF KEYPAD *****/
- 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', '#'},
- };
- Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
- Servo myServo; // Servo object
- /***** DEFINITION OF VARIABLES *****/
- const char* ssid = "your_SSID"; // Your WiFi SSID
- const char* password = "your_PASSWORD"; // Your WiFi Password
- WebServer server(80); // Create a web server on port 80
- bool doorLocked = true; // Initial state of the door
- const String correctPIN = "1234"; // Correct PIN code
- String inputPIN = ""; // User input PIN
- bool pinEntryActive = false; // Flag to indicate if PIN entry is active
- void setup(void) {
- Serial.begin(115200); // Start serial communication
- myServo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attach the servo to the PWM pin
- myServo.write(0); // Initially lock the door
- // Set up 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);
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- // Set up web server
- server.on("/", handleRoot); // Handle root URL
- server.begin(); // Start the server
- }
- void loop(void) {
- server.handleClient(); // Handle client requests
- char key = keypad.getKey(); // Get the pressed key
- if (key) {
- if (key == '*') { // If '*' is pressed, activate PIN entry
- pinEntryActive = true; // Set flag to true to start PIN entry
- inputPIN = ""; // Reset input
- Serial.println("PIN entry activated. Please enter your PIN.");
- } else if (pinEntryActive) {
- if (key == '#') { // If '#' is pressed, check the PIN
- if (inputPIN == correctPIN) {
- doorLocked = !doorLocked; // Toggle lock state
- myServo.write(doorLocked ? 0 : 90); // Lock or unlock the door
- Serial.println("PIN accepted. Door state changed.");
- } else {
- Serial.println("Incorrect PIN. Try again.");
- }
- pinEntryActive = false; // Deactivate PIN entry
- inputPIN = ""; // Reset input
- } else {
- inputPIN += key; // Append key to input PIN
- }
- }
- }
- }
- void handleRoot() {
- String message = "Door is currently " + String(doorLocked ? "Locked" : "Unlocked");
- server.send(200, "text/html", message); // Send response to the client
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement