0

I am trying to set up a weather station, and I should just have the anemometer left. This code works fine (as far as I know).

// Pin definitions
const int reedSwitchPin = 2;

// Variables
volatile unsigned long lastRotationTime = 0;
volatile float windSpeed = 0.0;
const float windSpeedFactor = 2.4; // Factor to convert rotation time to wind speed

void setup() {
  pinMode(reedSwitchPin, INPUT_PULLUP);
  // attachInterrupt(digitalPinToInterrupt(reedSwitchPin), handleRotation, FALLING);
  Serial.begin(9600);
  Serial.println("hello world!");
}

void loop() {
  // Test reed switch functionality
  int reedState = digitalRead(reedSwitchPin);
  if (reedState == HIGH) {
    Serial.println("Reed switch triggered!");
  }

  // Calculate wind speed based on the time between rotations
  unsigned long elapsedTime = micros() - lastRotationTime;
  if (elapsedTime > 0) {
   windSpeed = windSpeedFactor / (float)elapsedTime;
  }

  Serial.print("Wind Speed: ");
  Serial.print(windSpeed);
  Serial.println(" m/s");

  delay(1000); // Delay for 1 second before measuring again
}

void handleRotation() {
  // Record the time of the last rotation
  lastRotationTime = micros();

}

But when I uncomment the following line: attachInterrupt(digitalPinToInterrupt(reedSwitchPin), handleRotation, FALLING);

it prints garbage characters on the serial monitor. Here is the full code:

    // Pin definitions
const int reedSwitchPin = 2;

// Variables
volatile unsigned long lastRotationTime = 0;
volatile float windSpeed = 0.0;
const float windSpeedFactor = 2.4; // Factor to convert rotation time to wind speed

void setup() {
  pinMode(reedSwitchPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(reedSwitchPin), handleRotation, FALLING);
  Serial.begin(9600);
  Serial.println("hello world!");
}

void loop() {
  // Test reed switch functionality
  int reedState = digitalRead(reedSwitchPin);
  if (reedState == HIGH) {
    Serial.println("Reed switch triggered!");
  }

  // Calculate wind speed based on the time between rotations
  unsigned long elapsedTime = micros() - lastRotationTime;
  if (elapsedTime > 0) {
   windSpeed = windSpeedFactor / (float)elapsedTime;
  }

  Serial.print("Wind Speed: ");
  Serial.print(windSpeed);
  Serial.println(" m/s");

  delay(1000); // Delay for 1 second before measuring again
}

void handleRotation() {
  // Record the time of the last rotation
  lastRotationTime = micros();
}
6
  • By garbage, do you mean that even if the anemometer is stopped, you get initially a high speed reading in the serial monitor which, continually reduces each second ?
    – 6v6gt
    Commented Jul 13, 2023 at 9:58
  • by garbage, I mean random characters that make no sense Commented Jul 13, 2023 at 22:54
  • A picture is worth a thousand words. Add one to your question. If the esp8266 is continually crashing then it may write to the console at a baud rate other than the one you selected. Try changing this to 115200 or 74880 in Serial.begin() and the console to see readable output.
    – 6v6gt
    Commented Jul 14, 2023 at 3:06
  • What happens if you remove anything else but the anemometer (hardware and source code)? What does the serial window say when you change the baudrate as suggested? Please edit your question and add this important information. Commented Jul 14, 2023 at 5:59
  • previously, I tried every baud rate which got me a few things, but not what I wanted, also, I don't know what you mean to remove the anemometer, and also is this issue between the serial moniter or hardware, because if it is serial moniter, I can skip this part and try to put it directly onto the website. last thing, I don't really know what you want the picture to be of Commented Jul 14, 2023 at 10:38

1 Answer 1

-1

Pin 2 that is gpio2 (D4) is not suitable for this esp8266 application because this pin must be high at boot time and your anemometer could be in either state. Try say pin 4 that is gpio4 (D2) instead or search for "best pins to use - esp8266"

A crash during boot time can result in characters being written to the serial monitor at a baud rate of 115200 or 74880.

7
  • Whoever voted down, please add a comment, why. Commented Jul 14, 2023 at 5:58
  • I wasn't the one that voted down but just saying that I thing I was using D2 not D4, at least hardware wise. should I try using D4 because D2 was the one I was using before? Commented Jul 14, 2023 at 10:33
  • @Iliketurtles You appear to have difficulty with the two different pin numbering system in use on the nodeMcu. Search for a nodeMcu pinout diagram to see the translation between the GPIO and "D" pins. In the case here const int reedSwitchPin = 2; the 2 is a GPIO pin. On the nodeMcu board this is labeled D4. Try the suggestion I gave in my answer .
    – 6v6gt
    Commented Jul 14, 2023 at 12:29
  • oh, so i should say in the code to use const int reedSwitchPin = 4; Commented Jul 16, 2023 at 0:00
  • @Iliketurtles Yes. const int reedSwitchPin = 4; is correct. So you then connect the anemometer to the pin marked D2 on your nodeMcu board. If you have anything else connected to other pins on your nodeMcu, say a led, then temporarily disconnect these. If you still have problems then select the baud rate which gives the most understandable output on the serial console and add this output to your original question.
    – 6v6gt
    Commented Jul 16, 2023 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.