Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: **Lock Control**
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-02-27 14:25:03
- ********* 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 <EasyButton.h> //https://github.com/evert-arias/EasyButton
- #include <Deneyap_Servo.h> //https://github.com/deneyapkart/deneyap-servo-arduino-library
- #include <SoftwareSerial.h> // Software serial library
- #include <Wire.h> // I2C library
- #include <Adafruit_DS3502.h> // Library for DS3502 digital potentiometer
- #include <WiFi.h> // WiFi library for ESP32
- #include <WebServer.h> // Web server library for ESP32
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void handleRoot(void);
- void handleLockUnlock(void);
- void updateOutputs();
- void sendDataOverHC12();
- void receiveDataOverHC12();
- void readCurrentFeedback();
- void checkButtonPress();
- void toggleLockUnlock();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t codeButton_PushButton_PIN_D9 = 9;
- // HC12 connections
- #define HC12_TX 6 // Changed to use pin 6 instead of D6 for compatibility
- #define HC12_RX 4 // Changed to use pin 4 instead of D4 for compatibility
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- EspSoftwareSerial::UART HC12(HC12_TX, HC12_RX);
- Adafruit_DS3502 ds3502 = Adafruit_DS3502(); // DS3502 instance
- // Current feedback sensor pin
- #define CURRENT_FEEDBACK_SENSOR_PIN A0 // A0 pin where the voltage is read
- const int numSamples = 10; // Number of samples to average
- float runningSum = 0; // Variable to store the running sum of readings
- int sampleCount = 0; // Counter to track the number of samples taken
- #define HC12_DATA_REPETITION_TIME 1000
- int current_setpoint = 0;
- int current_feedback = -1;
- // WiFi credentials
- const char* ssid = "your_SSID"; // Replace with your SSID
- const char* password = "your_PASSWORD"; // Replace with your password
- // Web server on port 80
- WebServer server(80);
- // Lock state
- bool isLocked = true; // Initial state is locked
- const String correctPIN = "1234"; // Example PIN code
- void setup(void)
- {
- pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
- pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
- Serial.begin(9600); // Serial port to computer
- HC12.begin(9600); // Serial port to HC12
- ds3502.begin();
- // Initialize WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- // Define web server routes
- server.on("/", handleRoot);
- server.on("/lockunlock", handleLockUnlock);
- server.begin();
- }
- void loop(void)
- {
- updateOutputs(); // Refresh output data
- receiveDataOverHC12();
- sendDataOverHC12();
- readCurrentFeedback();
- server.handleClient(); // Handle web server requests
- checkButtonPress(); // Check for button press
- }
- void updateOutputs()
- {
- analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
- }
- void sendDataOverHC12() {
- static unsigned long lastTimeSendData = millis();
- if (millis() - lastTimeSendData > HC12_DATA_REPETITION_TIME) {
- HC12.println(String(current_feedback));
- lastTimeSendData = millis();
- Serial.println("sendData");
- }
- }
- void receiveDataOverHC12() {
- if (HC12.available()) {
- String receivedData = HC12.readStringUntil('\n');
- Serial.println(receivedData);
- int current_setpoint_temp = receivedData.toInt();
- if (current_setpoint_temp != current_setpoint) {
- current_setpoint = current_setpoint_temp;
- int digitalPotValue = map(current_setpoint, 0, 500, 0, 127);
- Serial.print("New current setpoint: ");
- Serial.println(current_setpoint);
- Serial.print("New Digital Pot value: ");
- Serial.println(digitalPotValue);
- ds3502.setWiper((uint8_t)digitalPotValue);
- }
- }
- }
- void readCurrentFeedback() {
- static float filteredValue = analogRead(CURRENT_FEEDBACK_SENSOR_PIN); // Initialize with the first reading
- int sensorValue = analogRead(CURRENT_FEEDBACK_SENSOR_PIN);
- filteredValue = (0.2 * sensorValue) + (0.8 * filteredValue);
- current_feedback = map((int)filteredValue, 0, 1023, 0, 500);
- }
- void checkButtonPress() {
- static EasyButton codeButton(codeButton_PushButton_PIN_D9);
- codeButton.begin();
- codeButton.onPressed([]() {
- toggleLockUnlock();
- });
- codeButton.read();
- }
- void toggleLockUnlock() {
- // Toggle lock state
- if (isLocked) {
- // Unlock the door
- Serial.println("Unlocking the door...");
- servo_Servomotor_PWMSignal_PIN_D2_rawData = 180; // Example value to unlock
- isLocked = false;
- } else {
- // Lock the door
- Serial.println("Locking the door...");
- servo_Servomotor_PWMSignal_PIN_D2_rawData = 0; // Example value to lock
- isLocked = true;
- }
- }
- void handleRoot() {
- String message = "Door is currently " + String(isLocked ? "Locked" : "Unlocked");
- server.send(200, "text/html", message);
- }
- void handleLockUnlock() {
- String pin = server.arg("pin");
- if (pin == correctPIN) {
- toggleLockUnlock();
- server.send(200, "text/html", "PIN accepted. Door state changed.");
- } else {
- server.send(403, "text/html", "Incorrect PIN.");
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement