Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Wind Speed Sensor PR-3000-FSJT-N01
- // --------------------- Define Pins ------------------------
- // [Sensor Pin] ---> [RS485 to TTL Pin]
- // Brown ---> 10-30V DC+ (Power Supply)
- // Black ---> GND DC- (Power Supply)
- // Green ---> A+
- // Blue ---> B-
- //
- // [ESP32 Pin] ---> [RS485 to TTL Pin]
- // VIN ---> Vcc
- // TX2 ---> TXD
- // RX2 ---> RXD
- // GND ---> GND
- //------------------------------------------------------------
- int Set_Max_Wind_Speed_To_Turn_OFF = 1;//19;
- int Delay_Time_OFF = 10;//30; // Time in Seconds
- #define relayPin 19
- #define RXD2 16 // ESP32 Pin [RX2]
- #define TXD2 17 // ESP32 Pin [TX2]
- byte ByteArray[250];
- int ByteData[20];
- float winds;
- int flag = 0;
- void setup() {
- Serial.begin(9600);
- Serial2.begin(4800, SERIAL_8N1, RXD2, TXD2);
- pinMode(relayPin, OUTPUT);
- digitalWrite(relayPin, 1);
- }
- ////////////////////////////////////////////////////////
- unsigned long previousMillis = 0; // Variable to store the previous time
- void readWindSpeesSensor() {
- delay(200);
- byte msgfs[] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B };
- int i;
- int len = 8;
- for (i = 0; i < len; i++) {
- Serial2.write(msgfs[i]);
- }
- len = 0;
- int a = 0;
- while (Serial2.available()) {
- ByteArray[a] = Serial2.read();
- a++;
- }
- int b = 0;
- String registros;
- for (b = 0; b < a; b++) {
- registros = String(ByteArray[b], HEX);
- }
- ByteData[0] = ByteArray[3] * 256 + ByteArray[4];
- winds = ByteData[0] * 0.1;
- Serial.print("Wind Speed = ");
- Serial.print(winds);
- Serial.println(" m/s");
- //delay(50);//200
- }
- /*
- void loop() {
- readWindSpeesSensor();
- unsigned long currentMillis = millis(); // Get the current time
- if (flag == 1) {
- // Check if it's time to turn off the relay
- if (currentMillis - previousMillis >= (Delay_Time_OFF * 1000)) {
- Serial.println("Relay is turned OFF");
- digitalWrite(relayPin, LOW); // Turn off the relay
- //flag = 0; // Reset the flag
- }
- }
- if (winds > Set_Max_Wind_Speed_To_Turn_OFF) {
- flag = 1; // Set the flag
- previousMillis = currentMillis; // Save the current time
- } else {
- digitalWrite(relayPin, HIGH); // Turn on the relay
- }
- }
- }
- */
- ////////////////////////////////////////////////////////
- void loop() {
- readWindSpeesSensor();
- unsigned long currentMillis = millis(); // Get the current time
- if (flag == 1) {
- if (currentMillis - previousMillis >= (Delay_Time_OFF * 1000)) {
- Serial.println("Relay is turned back ON");
- flag = 0;
- digitalWrite(relayPin, 1);
- }
- }
- if (winds > Set_Max_Wind_Speed_To_Turn_OFF) {
- flag = 1;
- previousMillis = currentMillis; // Save the current time
- digitalWrite(relayPin, 0);
- Serial.println("Relay is turned OFF");
- }
- }
- ////////////////////////////////////////////////////////
- /*
- void loop() {
- readWindSpeesSensor();
- if (flag == 1) {
- digitalWrite(relayPin, 0);
- delay(Delay_Time_OFF*1000);
- }
- if (winds > Set_Max_Wind_Speed_To_Turn_OFF) {
- flag = 1;
- }
- else {
- flag = 0;
- digitalWrite(relayPin, 1);
- }
- }
- */
- ///////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement