Advertisement
pleasedontcode

**WiFi Control** rev_05

Jan 26th, 2025
55
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 Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-26 12:30:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 生成 WiFi热点    wifi服务端 密码 8个1    wifi热点接入时 亮12脚 */
  21.     /* 13脚闪烁      SG舵机io5 每秒5度到达360度后反转        wifi热点断开时 */
  22.     /* 灭12脚 13脚亮    SG舵机io5每秒转1度 360度后反转 */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <ESP_AT_WiFiManager.h>  // https://github.com/khoih-prog/ESP_AT_WiFiManager
  29. #include <Deneyap_Servo.h>       // https://github.com/deneyapkart/deneyap-servo-arduino-library
  30. #include <Arduino.h>
  31. #include <WiFi.h>
  32. #include <Servo.h>               // Ensure this line has no spelling errors
  33.  
  34. // WiFi credentials
  35. const char* ssid = "your_SSID";      // Update with your WiFi SSID
  36. const char* password = "your_PASSWORD"; // Update with your WiFi password
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41. void updateOutputs(void);
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t LED_PIN_D4 = 4;         // LED pin
  45. const int d2Pin = 2;                   // D2 pin
  46.  
  47. /***** DEFINITION OF PWM OUTPUT PINS *****/
  48. const uint8_t Servo_PWM_PIN_D13 = 13; // Servo PWM pin
  49. const int servoPin = 5;                // Change to your servo pin
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. bool LED_PIN_D4_rawData = 0;          // Raw data for LED
  53. uint8_t Servo_PWM_PIN_D13_rawData = 0; // Raw data for servo
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. float LED_PIN_D4_phyData = 0.0;       // Physical data for LED
  57. float Servo_PWM_PIN_D13_phyData = 0.0; // Physical data for servo
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. // Servo motor setup
  61. Servo myservo;
  62.  
  63. /****** FUNCTION DECLARATIONS *****/
  64. int myFunction(int, int);
  65.  
  66. void setup(void)
  67. {
  68.     Serial.begin(115200);
  69.     pinMode(d2Pin, OUTPUT);
  70.     digitalWrite(d2Pin, LOW); // Initialize D2 pin to LOW
  71.  
  72.     pinMode(LED_PIN_D4, OUTPUT);
  73.     pinMode(Servo_PWM_PIN_D13, OUTPUT);
  74.  
  75.     // Initialize servo
  76.     myservo.attach(servoPin);
  77.  
  78.     // Connect to WiFi
  79.     WiFi.begin(ssid, password);
  80.     while (WiFi.status() != WL_CONNECTED) {
  81.         delay(1000);
  82.         Serial.println("Connecting to WiFi...");
  83.     }
  84.     Serial.println("Connected to WiFi");
  85.  
  86.     // Turn on LED when WiFi is connected
  87.     digitalWrite(LED_PIN_D4, HIGH);
  88.     // Rotate servo to 30 degrees and set D2 pin HIGH
  89.     myservo.write(30);
  90.     digitalWrite(d2Pin, HIGH);
  91. }
  92.  
  93. void loop(void)
  94. {
  95.     // put your main code here, to run repeatedly:
  96.     updateOutputs(); // Refresh output data
  97.     int result = myFunction(2, 3);
  98.  
  99.     // Control servo and LED based on WiFi connection status
  100.     if (WiFi.status() == WL_CONNECTED) {
  101.         // WiFi is connected
  102.         digitalWrite(LED_PIN_D4, HIGH); // Turn on LED
  103.         for (int angle = 30; angle <= 360; angle += 5) {
  104.             myservo.write(angle);
  105.             delay(200); // Adjust delay for speed
  106.         }
  107.         for (int angle = 360; angle >= 30; angle -= 5) {
  108.             myservo.write(angle);
  109.             delay(200); // Adjust delay for speed
  110.         }
  111.     } else {
  112.         // WiFi is disconnected
  113.         digitalWrite(LED_PIN_D4, LOW); // Turn off LED
  114.         for (int angle = 360; angle >= 0; angle -= 1) {
  115.             myservo.write(angle);
  116.             delay(100); // Adjust delay for speed
  117.         }
  118.     }
  119. }
  120.  
  121. void updateOutputs()
  122. {
  123.     digitalWrite(LED_PIN_D4, LED_PIN_D4_rawData);
  124.     analogWrite(Servo_PWM_PIN_D13, Servo_PWM_PIN_D13_rawData);
  125. }
  126.  
  127. /***** FUNCTION DEFINITIONS *****/
  128. int myFunction(int x, int y) {
  129.     return x + y;
  130. }
  131.  
  132. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement