Advertisement
safwan092

Untitled

Apr 14th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1.  
  2. // Wind Speed Sensor PR-3000-FSJT-N01
  3.  
  4. // --------------------- Define Pins ------------------------
  5.  
  6. // [Sensor Pin] ---> [RS485 to TTL Pin]
  7. // Brown ---> 10-30V DC+ (Power Supply)
  8. // Black ---> GND DC- (Power Supply)
  9. // Green ---> A+
  10. // Blue ---> B-
  11. //
  12. // [ESP32 Pin] ---> [RS485 to TTL Pin]
  13. // VIN ---> Vcc
  14. // TX2 ---> TXD
  15. // RX2 ---> RXD
  16. // GND ---> GND
  17.  
  18. //------------------------------------------------------------
  19. int Set_Max_Wind_Speed_To_Turn_OFF = 5;//19;
  20. int Delay_Time_OFF = 300;//30; // Time in Seconds
  21. #define relayPin 19
  22. #define RXD2 16 // ESP32 Pin [RX2]
  23. #define TXD2 17 // ESP32 Pin [TX2]
  24. byte ByteArray[250];
  25. int ByteData[20];
  26. float winds;
  27. int flag = 0;
  28.  
  29. void setup() {
  30. Serial.begin(9600);
  31. Serial2.begin(4800, SERIAL_8N1, RXD2, TXD2);
  32. pinMode(relayPin, OUTPUT);
  33. digitalWrite(relayPin, 1);
  34. }
  35. ////////////////////////////////////////////////////////
  36. unsigned long previousMillis = 0; // Variable to store the previous time
  37.  
  38.  
  39. void readWindSpeesSensor() {
  40. delay(200);
  41. byte msgfs[] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B };
  42. int i;
  43. int len = 8;
  44. for (i = 0; i < len; i++) {
  45. Serial2.write(msgfs[i]);
  46. }
  47. len = 0;
  48. int a = 0;
  49. while (Serial2.available()) {
  50. ByteArray[a] = Serial2.read();
  51. a++;
  52. }
  53. int b = 0;
  54. String registros;
  55. for (b = 0; b < a; b++) {
  56. registros = String(ByteArray[b], HEX);
  57. }
  58. ByteData[0] = ByteArray[3] * 256 + ByteArray[4];
  59.  
  60. winds = ByteData[0] * 0.1;
  61.  
  62. Serial.print("Wind Speed = ");
  63. Serial.print(winds);
  64. Serial.println(" m/s");
  65. //delay(50);//200
  66. }
  67.  
  68. /*
  69. void loop() {
  70. readWindSpeesSensor();
  71.  
  72. unsigned long currentMillis = millis(); // Get the current time
  73.  
  74. if (flag == 1) {
  75. // Check if it's time to turn off the relay
  76. if (currentMillis - previousMillis >= (Delay_Time_OFF * 1000)) {
  77. Serial.println("Relay is turned OFF");
  78. digitalWrite(relayPin, LOW); // Turn off the relay
  79. //flag = 0; // Reset the flag
  80. }
  81. }
  82. if (winds > Set_Max_Wind_Speed_To_Turn_OFF) {
  83. flag = 1; // Set the flag
  84. previousMillis = currentMillis; // Save the current time
  85. } else {
  86. digitalWrite(relayPin, HIGH); // Turn on the relay
  87. }
  88. }
  89. }
  90. */
  91. ////////////////////////////////////////////////////////
  92.  
  93. void loop() {
  94. readWindSpeesSensor();
  95. unsigned long currentMillis = millis(); // Get the current time
  96. if (flag == 1) {
  97. if (currentMillis - previousMillis >= (Delay_Time_OFF * 1000)) {
  98. Serial.println("Relay is turned back ON");
  99. flag = 0;
  100. digitalWrite(relayPin, 1);
  101. }
  102. }
  103. if (winds > Set_Max_Wind_Speed_To_Turn_OFF) {
  104. flag = 1;
  105. previousMillis = currentMillis; // Save the current time
  106. digitalWrite(relayPin, 0);
  107. Serial.println("Relay is turned OFF");
  108. }
  109.  
  110. }
  111. ////////////////////////////////////////////////////////
  112. /*
  113. void loop() {
  114. readWindSpeesSensor();
  115. if (flag == 1) {
  116. digitalWrite(relayPin, 0);
  117. delay(Delay_Time_OFF*1000);
  118. }
  119.  
  120. if (winds > Set_Max_Wind_Speed_To_Turn_OFF) {
  121. flag = 1;
  122. }
  123. else {
  124. flag = 0;
  125. digitalWrite(relayPin, 1);
  126. }
  127.  
  128.  
  129. }
  130. */
  131. ///////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement