Advertisement
pleasedontcode

**Lock Control** rev_11

Feb 27th, 2025
210
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: **Lock Control**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-02-27 14:25:03
  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. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  34. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  35. #include <SoftwareSerial.h> // Software serial library
  36. #include <Wire.h>          // I2C library
  37. #include <Adafruit_DS3502.h> // Library for DS3502 digital potentiometer
  38. #include <WiFi.h>          // WiFi library for ESP32
  39. #include <WebServer.h>     // Web server library for ESP32
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44. void handleRoot(void);
  45. void handleLockUnlock(void);
  46. void updateOutputs();
  47. void sendDataOverHC12();
  48. void receiveDataOverHC12();
  49. void readCurrentFeedback();
  50. void checkButtonPress();
  51. void toggleLockUnlock();
  52.  
  53. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  54. const uint8_t codeButton_PushButton_PIN_D9 = 9;
  55.  
  56. // HC12 connections
  57. #define HC12_TX   6 // Changed to use pin 6 instead of D6 for compatibility
  58. #define HC12_RX   4 // Changed to use pin 4 instead of D4 for compatibility
  59.  
  60. /***** DEFINITION OF PWM OUTPUT PINS *****/
  61. const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
  62.  
  63. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  64. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
  65.  
  66. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  67. float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  68.  
  69. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  70. EspSoftwareSerial::UART HC12(HC12_TX, HC12_RX);
  71. Adafruit_DS3502 ds3502 = Adafruit_DS3502(); // DS3502 instance
  72.  
  73. // Current feedback sensor pin
  74. #define CURRENT_FEEDBACK_SENSOR_PIN A0  // A0 pin where the voltage is read
  75. const int numSamples = 10;   // Number of samples to average
  76. float runningSum = 0;        // Variable to store the running sum of readings
  77. int sampleCount = 0;         // Counter to track the number of samples taken
  78.  
  79. #define HC12_DATA_REPETITION_TIME 1000
  80.  
  81. int current_setpoint = 0;
  82. int current_feedback = -1;
  83.  
  84. // WiFi credentials
  85. const char* ssid = "your_SSID"; // Replace with your SSID
  86. const char* password = "your_PASSWORD"; // Replace with your password
  87.  
  88. // Web server on port 80
  89. WebServer server(80);
  90.  
  91. // Lock state
  92. bool isLocked = true; // Initial state is locked
  93. const String correctPIN = "1234"; // Example PIN code
  94.  
  95. void setup(void)
  96. {
  97.     pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
  98.     pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  99.  
  100.     Serial.begin(9600);             // Serial port to computer
  101.     HC12.begin(9600);               // Serial port to HC12
  102.     ds3502.begin();
  103.  
  104.     // Initialize WiFi
  105.     WiFi.begin(ssid, password);
  106.     while (WiFi.status() != WL_CONNECTED) {
  107.         delay(1000);
  108.         Serial.println("Connecting to WiFi...");
  109.     }
  110.     Serial.println("Connected to WiFi");
  111.  
  112.     // Define web server routes
  113.     server.on("/", handleRoot);
  114.     server.on("/lockunlock", handleLockUnlock);
  115.     server.begin();
  116. }
  117.  
  118. void loop(void)
  119. {
  120.     updateOutputs(); // Refresh output data
  121.     receiveDataOverHC12();
  122.     sendDataOverHC12();
  123.     readCurrentFeedback();
  124.     server.handleClient(); // Handle web server requests
  125.     checkButtonPress(); // Check for button press
  126. }
  127.  
  128. void updateOutputs()
  129. {
  130.     analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  131. }
  132.  
  133. void sendDataOverHC12() {
  134.     static unsigned long lastTimeSendData = millis();
  135.  
  136.     if (millis() - lastTimeSendData > HC12_DATA_REPETITION_TIME) {
  137.         HC12.println(String(current_feedback));
  138.         lastTimeSendData = millis();
  139.         Serial.println("sendData");
  140.     }
  141. }
  142.  
  143. void receiveDataOverHC12() {
  144.     if (HC12.available()) {
  145.         String receivedData = HC12.readStringUntil('\n');
  146.         Serial.println(receivedData);
  147.         int current_setpoint_temp = receivedData.toInt();
  148.  
  149.         if (current_setpoint_temp != current_setpoint) {
  150.             current_setpoint = current_setpoint_temp;
  151.             int digitalPotValue = map(current_setpoint, 0, 500, 0, 127);
  152.             Serial.print("New current setpoint: ");
  153.             Serial.println(current_setpoint);
  154.             Serial.print("New Digital Pot value: ");
  155.             Serial.println(digitalPotValue);
  156.             ds3502.setWiper((uint8_t)digitalPotValue);
  157.         }
  158.     }
  159. }
  160.  
  161. void readCurrentFeedback() {
  162.     static float filteredValue = analogRead(CURRENT_FEEDBACK_SENSOR_PIN); // Initialize with the first reading
  163.     int sensorValue = analogRead(CURRENT_FEEDBACK_SENSOR_PIN);
  164.     filteredValue = (0.2 * sensorValue) + (0.8 * filteredValue);
  165.     current_feedback = map((int)filteredValue, 0, 1023, 0, 500);
  166. }
  167.  
  168. void checkButtonPress() {
  169.     static EasyButton codeButton(codeButton_PushButton_PIN_D9);
  170.     codeButton.begin();
  171.     codeButton.onPressed([]() {
  172.         toggleLockUnlock();
  173.     });
  174.     codeButton.read();
  175. }
  176.  
  177. void toggleLockUnlock() {
  178.     // Toggle lock state
  179.     if (isLocked) {
  180.         // Unlock the door
  181.         Serial.println("Unlocking the door...");
  182.         servo_Servomotor_PWMSignal_PIN_D2_rawData = 180; // Example value to unlock
  183.         isLocked = false;
  184.     } else {
  185.         // Lock the door
  186.         Serial.println("Locking the door...");
  187.         servo_Servomotor_PWMSignal_PIN_D2_rawData = 0; // Example value to lock
  188.         isLocked = true;
  189.     }
  190. }
  191.  
  192. void handleRoot() {
  193.     String message = "Door is currently " + String(isLocked ? "Locked" : "Unlocked");
  194.     server.send(200, "text/html", message);
  195. }
  196.  
  197. void handleLockUnlock() {
  198.     String pin = server.arg("pin");
  199.     if (pin == correctPIN) {
  200.         toggleLockUnlock();
  201.         server.send(200, "text/html", "PIN accepted. Door state changed.");
  202.     } else {
  203.         server.send(403, "text/html", "Incorrect PIN.");
  204.     }
  205. }
  206.  
  207. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement