Advertisement
pleasedontcode

"WiFi Lock" rev_06

May 20th, 2024
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "WiFi Lock"
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-05-20 22:54:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The servo motor installed in the door will lock or */
  21.     /* unlock based on the correct PIN code received by */
  22.     /* the button. If the door is unlocked and the PIN is */
  23.     /* correct, it will lock. If the door is locked and */
  24.     /* the PIN is correct, it will unlock. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* The webserver provides a web page about lock or */
  27.     /* unlock state of the door. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  32. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  33. #include <WiFi.h>
  34. #include <WebServer.h>
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs(void);
  40. void onButtonPressed(void);
  41. void handleRoot(void);
  42. void handleNotFound(void);
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t codeButton_PushButton_PIN_D9 = 9;
  46.  
  47. /***** DEFINITION OF PWM OUTPUT PINS *****/
  48. const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. EasyButton button(codeButton_PushButton_PIN_D9);  // Initialize EasyButton instance
  60. Servo myservo;  // Initialize Servo instance
  61. WebServer server(80);  // Initialize web server on port 80
  62.  
  63. /****** DEFINITION OF VARIABLES *****/
  64. bool isLocked = true;  // Variable to store the lock state
  65. const char* ssid = "your_SSID";  // Replace with your network SSID
  66. const char* password = "your_PASSWORD";  // Replace with your network password
  67.  
  68. void setup(void)
  69. {
  70.   // put your setup code here, to run once:
  71.   Serial.begin(115200);  // Initialize Serial for debugging purposes
  72.   Serial.println();
  73.   Serial.println(">>> EasyButton and Servo example <<<");
  74.  
  75.   pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
  76.   pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  77.  
  78.   button.begin();  // Initialize the button
  79.   button.onPressed(onButtonPressed);  // Add the callback function to be called when the button is pressed
  80.  
  81.   myservo.attach(servo_Servomotor_PWMSignal_PIN_D2);  // Attach the servo to pin D2
  82.   myservo.write(0);  // Initialize the servo to locked position
  83.  
  84.   // Connect to Wi-Fi
  85.   WiFi.begin(ssid, password);
  86.   while (WiFi.status() != WL_CONNECTED) {
  87.     delay(1000);
  88.     Serial.println("Connecting to WiFi...");
  89.   }
  90.   Serial.println("Connected to WiFi");
  91.  
  92.   // Start the web server
  93.   server.on("/", handleRoot);
  94.   server.onNotFound(handleNotFound);
  95.   server.begin();
  96.   Serial.println("Web server started");
  97. }
  98.  
  99. void loop(void)
  100. {
  101.   // put your main code here, to run repeatedly:
  102.   button.read();  // Continuously read the status of the button
  103.   updateOutputs();  // Refresh output data
  104.   server.handleClient();  // Handle web server client requests
  105. }
  106.  
  107. void updateOutputs()
  108. {
  109.   analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  110. }
  111.  
  112. void onButtonPressed()
  113. {
  114.   Serial.println("Button pressed");
  115.   // Toggle the lock state
  116.   isLocked = !isLocked;
  117.   int pos = isLocked ? 0 : 180;  // 0 degrees for locked, 180 degrees for unlocked
  118.   myservo.write(pos);  // Move the servo to the new position
  119.   Serial.println(isLocked ? "Door locked" : "Door unlocked");
  120. }
  121.  
  122. void handleRoot()
  123. {
  124.   String message = isLocked ? "Door is locked" : "Door is unlocked";
  125.   server.send(200, "text/plain", message);
  126. }
  127.  
  128. void handleNotFound()
  129. {
  130.   server.send(404, "text/plain", "404: Not found");
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement