0

I got this weather station and I am trying to get the data of the station to a website with this ESP32 as an school project. But the weather station isn't original as it is on the hyperlink, it got modified by an former student. So the cable coming from the anemometer and the wind direction indicator got cut of, so the four cable inside (GND, 3.3V, wind speed data, wind direction data) could fit in the ESP32. First I try to get the anemometer running. I got the Signal from the anemometer to the serial monitor with this code, which works fine:

int windSpeed = A0;
int outputValue = 0;


void setup() {
    Serial.begin(9600);
}

void loop() {
    outputValue = analogRead(windSpeed); 
    Serial.println(outputValue);
    if(outputValue > 0) {
        Serial.println("high");
    }
}

So the next thing I wanted to do was to was calculating the wind speed. I found this code in the internet and adjusted it so it would fit to my ESP32.

const int RecordTime = 3; //Define Measuring Time (Seconds)
const int SensorPin = A0;  //Define Interrupt Pin (2 or 3 @ Arduino Uno)

int InterruptCounter;
float WindSpeed;

void setup()
{
  Serial.begin(9600);
}

void loop() {
  meassure();
  Serial.print("Wind Speed: ");
  Serial.print(WindSpeed);       //Speed in km/h
  Serial.print(" km/h - ");
  Serial.print(WindSpeed / 3.6); //Speed in m/s
  Serial.println(" m/s");
}

void meassure() {
  InterruptCounter = 0;
  attachInterrupt(digitalPinToInterrupt(SensorPin), countup, RISING);
  delay(1000 * RecordTime);
  detachInterrupt(digitalPinToInterrupt(SensorPin));
  WindSpeed = (float)InterruptCounter / (float)RecordTime * 2.4;
}

void countup() {
  InterruptCounter++;
}

I can upload this code to the ESP32 and it prints the massages in the serial monitor, but it doesn`t get the interrupt. Screenshot of the seriell monitor while I spun the anemometer.

What´s wrong?

Edit: Thank you, now I get the wind speed in the seriell monitor, but unfortunately it doesn't sends many signals. But RISING in attachInterrupt(digitalPinToInterrupt(SensorPin), countup, RISING); should´ve solve this should´nt it?

Edit no. 2:

@PMF This is written in the manual (The manual is german but I will continue to write in english so it may help other people): Das Windrad ist mit einem Magnetschalter gekoppelt, der bei einer Umdrehung zwei Impulse abgibt. Das Datenblatt besagt, dass die Impulsfrequenz in Hz mit dem Faktor 0,33 multipliziert werden muss, um die Windgeschwindigkeit in m/s zu erhalten. Die Rotationsfrequenz ergibt sich aus der Zeitdifferenz zwischen zwei Impulsen in Sekunden, multipliziert mit dem Faktor 2 (der Sensor liefert bei einer Umdrehung zwei Impulse). Die Rotationsfrequenz ist dann der Kehrwert der Zeitdifferenz. Wenn dieser Wert mit dem Faktor 0,66 multipliziert wird, ist das Ergebnis die Windgeschwindigkeit in m/s. Die Windgeschwindigkeit in km/h lässt sich berechnen, indem die Windgeschwindigkeit in m/s mit dem Faktor 3,6 multipliziert wird. Mit anderen Worten: Eine Umdrehung des Windrads pro Sekunde ist gleichbedeutend mit der Windgeschwindigkeit 2,4 km/h. (1 rotation = 2.4 kph)

So if I am right the code should also be right, but the interrupt counter seems to just go up while there is an interrupt, even if I put the anemometer on the table in a certain position it just counts up as you can see in the following screenshot of the seriell monitor while the anemometer is just laying on the table:

enter image description here

tino
  • 13
  • 4

1 Answers1

0

"A0" is defined as 0 on the ESP32 (for most variants at least). I would not use it at all, because the ESP can use analog in on most pins and uses no pin mapping. Just use the GPIO pin number directly. The interrupt handling anyway works on digital inputs, not on analog inputs, so any reference to an analog pin would at least be confusing.

So change to SensorPin = 36; and make sure you do pinMode(SensorPin, INPUT); in setup().

PMF
  • 14,535
  • 3
  • 23
  • 49
  • Hi, so now I get the Signal, but still not as it should work. I wrote more information in the original question. – tino Apr 16 '22 at 17:38
  • It's rather stormy at your place... Obviously these values are way to big. The problem is now most likely in the line `WindSpeed = (float)InterruptCounter / (float)RecordTime * 2.4;`. You need to know how many pulses per second define a certain wind speed and apply the correct factor. That is hopefully in the manual, otherwise you need a way to calibrate your sensor. – PMF Apr 17 '22 at 06:49