CCCPaste

Smart Lock rev_118

/********* 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: Smart Lock
	- Source Code compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-10-07 22:26:46

********* 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 *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/deneyapkart/deneyap-servo-arduino-library
#include 	//https://github.com/Chris--A/Keypad
#include   // Include WiFi library for web server
#include  // Include WebServer library
#include "ServoControl.h" // Include the ServoControl header
#include "WebServerControl.h" // Include the WebServerControl header
#include "KeypadHandler.h" // Include the KeypadHandler header

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(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;
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;

// Define raw data variables for digital outputs
int keypad_Keypad3x4_C1_PIN_D8_rawData = 0; // Define based on your logic
int keypad_Keypad3x4_C2_PIN_D9_rawData = 0; // Define based on your logic
int keypad_Keypad3x4_C3_PIN_D10_rawData = 0; // Define based on your logic

const char* ssid = "your_SSID";  // Replace with your network SSID
const char* password = "your_PASSWORD";  // Replace with your network password

/****** DEFINITION OF OUTPUT VARIABLES *****/
bool doorLocked = true; // Initial state of the door
ServoControl servoControl(9); // Assuming the servo is connected to pin 9
WebServerControl webServerControl; // Create web server control instance

// 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 KeypadHandler object
byte rowPins[4] = {keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7};
byte colPins[3] = {keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10};
KeypadHandler keypadHandler(Keys_Keypad3x4, rowPins, colPins);

void setup(void)
{
	// put your setup code here, to run once:
	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(pinButton_PushButton_PIN_D2,	INPUT_PULLUP);

	pinMode(keypad_Keypad3x4_C1_PIN_D8,	 OUTPUT);
	pinMode(keypad_Keypad3x4_C2_PIN_D9,	 OUTPUT);
	pinMode(keypad_Keypad3x4_C3_PIN_D10,	 OUTPUT);

	// Initialize WiFi
	WiFi.begin(ssid, password);
	while (WiFi.status() != WL_CONNECTED) {
		delay(1000);
	}
	webServerControl.begin(); // Start the web server
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	updateOutputs(); // Refresh output data
	webServerControl.handleClient(); // Handle web server requests

	// Check for keypad input here and unlock/lock the door based on the PIN
	char key = keypadHandler.getKey(); // Use the KeypadHandler to get the key
	if (key) {
		handleKeypadInput(key); // Function to handle keypad input
	}
}

void updateOutputs()
{
	digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
	digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
	digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
}

void handleKeypadInput(char key)
{
	// Assuming '1234' is the correct PIN code
	static String input;
	if (key == '*') { // Reset input
		input = "";
	} else if (key == '#') { // Check PIN
		if (input == "1234") { // Check if input matches the PIN
			if (doorLocked) {
				servoControl.unlock(); // Unlock the door
				doorLocked = false;
			} else {
				servoControl.lock(); // Lock the door
				doorLocked = true;
			}
			webServerControl.setLockState(doorLocked); // Update web server state
		}
	} else { // Append digit to input
		input += key;
	}
}

/* END CODE */