Advertisement
pleasedontcode

**Servo Control** rev_103

Oct 6th, 2024
114
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: **Servo Control**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-06 18:01:13
  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 <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  32. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  33. #include <WiFi.h> // Include WiFi library for web server
  34. #include "ServoControl.h" // Include the ServoControl class
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs();
  40. void handleKeypadInput(char key);
  41. void startWebServer();
  42. void handleClient();
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  46. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  47. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  48. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  49. const uint8_t pinButton_PushButton_PIN_D2 = 2;
  50.  
  51. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  52. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  53. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  54. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  55.  
  56. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  58. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  59. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  60.  
  61. /***** DEFINITION OF KEYPAD VARIABLES *****/
  62. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  63. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  64. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  65.     {'1', '2', '3'},
  66.     {'4', '5', '6'},
  67.     {'7', '8', '9'},
  68.     {'*', '0', '#'},
  69. };
  70.  
  71. // Define the correct PIN code
  72. const String correctPin = "1234";
  73. String enteredPin = "";
  74.  
  75. // Servo control object
  76. ServoControl doorControl(10); // Initialize ServoControl with pin 10
  77.  
  78. // WiFi credentials
  79. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  80. const char* password = "your_PASSWORD"; // Replace with your WiFi Password
  81.  
  82. WiFiServer server(80); // Create a server that listens on port 80
  83.  
  84. void setup(void)
  85. {
  86.     // Initialize keypad pins
  87.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  88.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  89.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  90.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  91.     pinMode(pinButton_PushButton_PIN_D2, INPUT_PULLUP);
  92.  
  93.     // Initialize output pins
  94.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  95.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  96.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  97.  
  98.     // Connect to WiFi
  99.     WiFi.begin(ssid, password);
  100.     while (WiFi.status() != WL_CONNECTED) {
  101.         delay(1000);
  102.     }
  103.     startWebServer();
  104. }
  105.  
  106. void loop(void)
  107. {
  108.     // Handle keypad input
  109.     char key = getKey(); // Assuming getKey() is a function that reads the keypad
  110.     if (key) {
  111.         handleKeypadInput(key);
  112.     }
  113.  
  114.     // Handle web server client
  115.     handleClient();
  116. }
  117.  
  118. void updateOutputs()
  119. {
  120.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  121.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  122.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  123. }
  124.  
  125. void handleKeypadInput(char key)
  126. {
  127.     if (key == '#') {
  128.         // Check if the entered PIN is correct
  129.         if (enteredPin == correctPin) {
  130.             // Toggle lock state
  131.             doorControl.toggleLock(); // Use the ServoControl class to toggle lock
  132.         }
  133.         enteredPin = ""; // Reset entered PIN
  134.     } else {
  135.         enteredPin += key; // Append key to entered PIN
  136.     }
  137. }
  138.  
  139. void startWebServer()
  140. {
  141.     server.begin(); // Start the server
  142. }
  143.  
  144. void handleClient()
  145. {
  146.     WiFiClient client = server.available(); // Check for incoming clients
  147.     if (client) {
  148.         String currentLine = ""; // String to hold incoming data
  149.         while (client.connected()) {
  150.             if (client.available()) {
  151.                 char c = client.read(); // Read a byte
  152.                 currentLine += c; // Add it to the current line
  153.                 if (c == '\n') { // If the line is complete
  154.                     // Check if the line is a request for the lock state
  155.                     if (currentLine.startsWith("GET /state")) {
  156.                         // Send the response
  157.                         client.println("HTTP/1.1 200 OK");
  158.                         client.println("Content-type:text/html");
  159.                         client.println();
  160.                         client.print("Door is ");
  161.                         client.print(doorControl.getLockState() ? "Locked" : "Unlocked"); // Check lock state
  162.                         client.println();
  163.                         break;
  164.                     }
  165.                     currentLine = ""; // Reset the current line
  166.                 }
  167.             }
  168.         }
  169.         delay(1); // Give the web browser time to receive the data
  170.         client.stop(); // Close the connection
  171.     }
  172. }
  173.  
  174. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement