/********* 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: **Servo Control**
- Source Code compiled for: Arduino Nano ESP32
- Source Code created on: 2024-10-07 22:00: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 *****/
/* START CODE */
/****** DEFINITION OF LIBRARIES *****/
#include // Use the XYZrobotServo library instead of the standard Servo library
#include // Keypad library
#include // WiFi library for web server
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
void handleClient();
char getKey(); // Added function prototype for getKey()
/***** 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','#'},
};
// Define the correct PIN code
const String correctPin = "1234"; // Example PIN code
String inputPin = ""; // To store user input
// Servo object
XYZrobotServo myServo(Serial, 1); // Instantiate with Serial and ID 1
// WiFi credentials
const char* ssid = "your_SSID"; // Replace with your WiFi SSID
const char* password = "your_PASSWORD"; // Replace with your WiFi password
WiFiServer server(80); // Create a server that listens on port 80
void setup(void)
{
// Initialize servo
myServo.setPosition(0, 0); // Start with the door locked
// 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(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);
// Start WiFi connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
server.begin(); // Start the server
}
void loop(void)
{
updateOutputs(); // Refresh output data
handleClient(); // Handle incoming client requests
// Check if a key is pressed
char key = getKey();
if (key) {
if (key == '#') { // If '#' is pressed, check the PIN
if (inputPin == correctPin) {
uint16_t position = myServo.readPosition();
if (position == 0) {
myServo.setPosition(90, 0); // Unlock the door
} else {
myServo.setPosition(0, 0); // Lock the door
}
}
inputPin = ""; // Reset input
} else {
inputPin += key; // Add key to 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 handleClient() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
String currentState = (myServo.readPosition() == 0) ? "Locked" : "Unlocked";
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("");
client.println("Door is " + currentState + "
");
client.println("");
client.stop(); // Close the connection
}
}
char getKey() {
// Implement keypad reading logic here
// This function should return the pressed key character
Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4),
new byte[KEYPAD3X4_ROWS]{keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7},
new byte[KEYPAD3X4_COLS]{keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10},
KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
char key = keypad.getKey();
return key; // Return the pressed key character
}
/* END CODE */