CCCPaste

"WiFi Lock" rev_06

/********* 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: "WiFi Lock"
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-05-20 22:54:18

********* 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/evert-arias/EasyButton
#include   //https://github.com/deneyapkart/deneyap-servo-arduino-library
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void onButtonPressed(void);
void handleRoot(void);
void handleNotFound(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t codeButton_PushButton_PIN_D9 = 9;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(codeButton_PushButton_PIN_D9);  // Initialize EasyButton instance
Servo myservo;  // Initialize Servo instance
WebServer server(80);  // Initialize web server on port 80

/****** DEFINITION OF VARIABLES *****/
bool isLocked = true;  // Variable to store the lock state
const char* ssid = "your_SSID";  // Replace with your network SSID
const char* password = "your_PASSWORD";  // Replace with your network password

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(115200);  // Initialize Serial for debugging purposes
  Serial.println();
  Serial.println(">>> EasyButton and Servo example <<<");

  pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
  pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);

  button.begin();  // Initialize the button
  button.onPressed(onButtonPressed);  // Add the callback function to be called when the button is pressed

  myservo.attach(servo_Servomotor_PWMSignal_PIN_D2);  // Attach the servo to pin D2
  myservo.write(0);  // Initialize the servo to locked position

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Start the web server
  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("Web server started");
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  button.read();  // Continuously read the status of the button
  updateOutputs();  // Refresh output data
  server.handleClient();  // Handle web server client requests
}

void updateOutputs()
{
  analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
}

void onButtonPressed()
{
  Serial.println("Button pressed");
  // Toggle the lock state
  isLocked = !isLocked;
  int pos = isLocked ? 0 : 180;  // 0 degrees for locked, 180 degrees for unlocked
  myservo.write(pos);  // Move the servo to the new position
  Serial.println(isLocked ? "Door locked" : "Door unlocked");
}

void handleRoot()
{
  String message = isLocked ? "Door is locked" : "Door is unlocked";
  server.send(200, "text/plain", message);
}

void handleNotFound()
{
  server.send(404, "text/plain", "404: Not found");
}

/* END CODE */