CCCPaste

**Lock Control** rev_63

/********* 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 14:36:48

********* 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 	// Servo control library
#include 	// Keypad library for PIN entry
#include 	// EasyButton library for button handling
#include  // WiFi library for web server

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void handleButtonPress();
void updateWebServer();
void unlockDoor();
void lockDoor();
bool checkPIN(const String& pin);

/***** 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;
const uint8_t pinButton_PushButton_PIN_D2 = 2;

/***** 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 KEYPAD VARIABLES *****/
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(Keys_Keypad3x4, keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);

/***** DEFINITION OF SERVO VARIABLES *****/
Servo doorServo; // Servo object for the door lock
bool doorLocked = true; // Initial state of the door

/***** DEFINITION OF BUTTON VARIABLES *****/
EasyButton button(pinButton_PushButton_PIN_D2); // Button object

/***** DEFINITION OF WIFI VARIABLES *****/
const char* ssid = "your_SSID"; // Replace with your network SSID
const char* password = "your_PASSWORD"; // Replace with your network password
WiFiServer server(80); // Create a web server on port 80

// Define the correct PIN code
const String correctPIN = "1234"; // Example PIN code

void setup(void)
{
    // Initialize Serial for debugging
    Serial.begin(115200);

    // Initialize the servo
    doorServo.attach(9); // Attach the servo to pin 9
    lockDoor(); // Start with the door locked

    // Setup keypad pins
    pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
    pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
    pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);

    // Setup button
    button.begin();
    button.onPressed(handleButtonPress); // Set button press handler

    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");
    server.begin(); // Start the web server
}

void loop(void)
{
    // Read keypad input
    char key = keypad.getKey();
    static String enteredPIN = ""; // Store the entered PIN

    if (key) {
        // Handle key press for PIN entry
        if (key == '#') {
            // Check if the entered PIN is correct
            if (checkPIN(enteredPIN)) {
                // Toggle door state
                if (doorLocked) {
                    unlockDoor();
                } else {
                    lockDoor();
                }
            }
            enteredPIN = ""; // Clear the entered PIN
        } else {
            // Append the pressed key to the entered PIN
            enteredPIN += key;
        }
    }

    // Read button state
    button.read();

    // Handle web server requests
    WiFiClient client = server.available();
    if (client) {
        // Process the client request
        updateWebServer();
    }
}

void handleButtonPress() {
    // Toggle door state on button press
    if (doorLocked) {
        unlockDoor();
    } else {
        lockDoor();
    }
}

void unlockDoor() {
    doorServo.write(90); // Move servo to unlock position
    doorLocked = false; // Update door state
    Serial.println("Door unlocked");
}

void lockDoor() {
    doorServo.write(0); // Move servo to lock position
    doorLocked = true; // Update door state
    Serial.println("Door locked");
}

bool checkPIN(const String& pin) {
    return pin == correctPIN; // Check if entered PIN matches the correct PIN
}

void updateWebServer() {
    // Handle web server requests and send the lock/unlock state
    WiFiClient client = server.available();
    if (client) {
        String currentLine = "";
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                if (c == '\n') {
                    // Send response
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    client.println("");
                    client.println("");
                    client.print("Door is currently: ");
                    client.print(doorLocked ? "Locked" : "Unlocked");
                    client.println("");
                    break;
                } else {
                    currentLine += c;
                }
            }
        }
        client.stop();
    }
}

/* END CODE */