CCCPaste

Smart Lock rev_90

/********* 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 NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-10-06 10:33:23

********* 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 	//https://github.com/deneyapkart/deneyap-servo-arduino-library
#include 	//https://github.com/Chris--A/Keypad
#include 	//https://github.com/evert-arias/EasyButton
#include 	//https://github.com/dniklaus/arduino-display-lcdkeypad
#include 	//https://github.com/pololu/xyzrobot-servo-arduino
#include  // Include WiFi library for web server
#include  // Include WebServer library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
void handleRoot(); // Function to handle root URL
void lockUnlockDoor(); // Function to lock/unlock the door

/***** 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 OUTPUT RAW VARIABLES *****/
bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;

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 instances for servo and web server
Deneyap_Servo servoMotor;
WebServer server(80); // Create a web server on port 80

// Define the correct PIN code
const String correctPIN = "1234"; // Example PIN code
String inputPIN = ""; // Variable to store user input PIN

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);

	// Attach the servo to a pin
	servoMotor.attach(11); // Example pin for servo control
	servoMotor.write(0); // Start with the door locked

	// Initialize WiFi
	WiFi.begin("yourSSID", "yourPASSWORD"); // Replace with your WiFi credentials
	while (WiFi.status() != WL_CONNECTED) {
		delay(1000);
	}
	server.on("/", handleRoot); // Handle root URL
	server.begin(); // Start the server
}

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

	// Check keypad input
	char key = getKeypadInput();
	if (key != '\0') {
		inputPIN += key; // Append pressed key to inputPIN
		if (inputPIN.length() == 4) { // Check if 4 digits are entered
			lockUnlockDoor(); // Check and lock/unlock the door
			inputPIN = ""; // Reset inputPIN after checking
		}
	}
}

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);
}

char getKeypadInput() {
	// Function to read the keypad and return the pressed key
	for (int row = 0; row < KEYPAD3X4_ROWS; row++) {
		for (int col = 0; col < KEYPAD3X4_COLS; col++) {
			if (digitalRead(keypad_Keypad3x4_R1_PIN_D4 + row) == LOW) {
				return Keys_Keypad3x4[row][col]; // Return the pressed key
			}
		}
	}
	return '\0'; // No key pressed
}

void handleRoot() {
	// Function to handle root URL and send lock/unlock state
	String state = (servoMotor.read() == 0) ? "Locked" : "Unlocked";
	server.send(200, "text/plain", "Door is currently: " + state);
}

void lockUnlockDoor() {
	// Function to lock/unlock the door based on the input PIN
	if (inputPIN == correctPIN) {
		int currentAngle = servoMotor.read();
		if (currentAngle == 0) {
			servoMotor.write(90); // Unlock the door
		} else {
			servoMotor.write(0); // Lock the door
		}
	}
}

/* END CODE */