Wind speed sensor, how to run this program on nodemcu or wemos D1

// Definisi konstanta
#define windPin 14 // Receive the data from sensor
const float pi = 3.14159265; // pi number
int period = 10000; // Periode pengukuran (milidetik)
int delaytime = 10000; // Waktu antar sampel (milidetik)
int radio = 90; // Jarak dari pusat kincir angin ke cangkir luar (mm)
int jml_celah = 18; // jumlah celah sensor

// Variable definitions
unsigned int Sample = 0; // Sample number
unsigned int counter = 0; // B/W counter for sensor
unsigned int RPM = 0; // Revolutions per minute
float speedwind = 0; // Wind speed (m/s)

void setup()
{
// Set the pins
pinMode(14, INPUT);
digitalWrite(14, HIGH);

// sets the serial port to 9600
Serial.begin(9600);

// Splash screen
Serial.println("ANEMOMETER");
Serial.println("**********");
Serial.println("Based on depoinovasi anemometer sensor");
Serial.print("Sampling period: ");
Serial.print(period/1000);
Serial.print(" seconds every ");
Serial.print(delaytime/1000);
Serial.println(" seconds.");
Serial.println("** You could modify those values on code **");
Serial.println();
}

void loop()
{
Sample++;
Serial.print(Sample);
Serial.print(": Start measurement…");
windvelocity();
Serial.println(" finished.");
Serial.print("Counter: ");
Serial.print(counter);
Serial.print("; RPM: ");
RPMcalc();
Serial.print(RPM);
Serial.print("; Wind speed: ");
WindSpeed();
Serial.print(speedwind);
Serial.print(" [m/s]");
Serial.println();
delay(5000);
}

// Measure wind speed
void windvelocity()
{
speedwind = 0;
counter = 0;
attachInterrupt(0, addcount, CHANGE);
unsigned long millis();
long startTime = millis();
while(millis() < startTime + period) {}

detachInterrupt(1);
}

void RPMcalc()
{
RPM=((counter/jml_celah)*60)/(period/1000); // Calculate revolutions per minute (RPM)
}

void WindSpeed()
{
speedwind = ((2 * pi * radio * RPM)/60) / 1000; // Calculate wind speed on m/s
}

void addcount()
{
counter++;
}

not working when using wemos D1

and it works normally when using arduino
can friends help me to edit the program so that it can work in wemos D1

What error is that ?

Please edit your post and add the code tags. Without them, your post is breaking forum rules. Read the forum guide to find out how. The forum guide is linked to in a sticky post at the top of each forum section.

// Set the pins
pinMode(2, INPUT);

Which pin of the NodeMCU/Wemos is the anemometer connected to? Confusingly, there are two ways of referring to a pin on these boards, and "2" is not the same pin as "D2"!

attachInterrupt(0, addcount, CHANGE);

On Nano/Uno, interrupt 0 is actually pin 2. Another confusing way to refer to a pin! On NodeMCU/Wemos, you should use the actual pin number, for example "D2".

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

This also looks strange...

It enables the internal pull-up resistor.

Thanks! I didn´t know it can be done this way.

NodeMCU is ok with the D-numbering or the GPIO numbering.
WeMos only works with the GPIO numbering (even though it has the D-number labelling on the board).

PE --

and it works normally when using arduino
can friends help me to edit the program so that it can work in wemos D1

I think that you have to change
attachInterrupt(0, addcount, CHANGE);

to

attachInterrupt(digitalPinToInterrupt(0), addcount, CHANGE);

and

void addcount()
{
  counter++;
}

to

ICACHE_RAM_ATTR void addcount()
{
  counter++;
}

I changed it to like this, and this is the result, still the same

// Pin definitions
#define windPin 2 // Receive the data from sensor

// Definisi konstanta
const float pi = 3.14159265; // pi number
int period = 10000; // Periode pengukuran (milidetik)
int delaytime = 10000; // Waktu antar sampel (milidetik)
int radio = 90; // Jarak dari pusat kincir angin ke cangkir luar (mm)
int jml_celah = 18; // jumlah celah sensor

// Variable definitions
unsigned int Sample = 0; // Sample number
unsigned int counter = 0; // B/W counter for sensor
unsigned int RPM = 0; // Revolutions per minute
float speedwind = 0; // Wind speed (m/s)

void setup()
{
// Set the pins
pinMode(2, INPUT);
digitalWrite(2, HIGH);

// sets the serial port to 9600
Serial.begin(9600);

// Splash screen
Serial.println("ANEMOMETER");
Serial.println("**********");
Serial.println("Based on depoinovasi anemometer sensor");
Serial.print("Sampling period: ");
Serial.print(period/1000);
Serial.print(" seconds every ");
Serial.print(delaytime/1000);
Serial.println(" seconds.");
Serial.println("** You could modify those values on code **");
Serial.println();
}

void loop()
{
Sample++;
Serial.print(Sample);
Serial.print(": Start measurement…");
windvelocity();
Serial.println(" finished.");
Serial.print("Counter: ");
Serial.print(counter);
Serial.print("; RPM: ");
RPMcalc();
Serial.print(RPM);
Serial.print("; Wind speed: ");
WindSpeed();
Serial.print(speedwind);
Serial.print(" [m/s]");
Serial.println();
delay(5000);
}

// Measure wind speed
void windvelocity()
{
speedwind = 0;
counter = 0;
attachInterrupt(digitalPinToInterrupt(0), addcount, CHANGE);
unsigned long millis();
long startTime = millis();
while(millis() < startTime + period) {}

detachInterrupt(1);
}

void RPMcalc()
{
RPM=((counter/jml_celah)*60)/(period/1000); // Calculate revolutions per minute (RPM)
}

void WindSpeed()
{
speedwind = ((2 * pi * radio * RPM)/60) / 1000; // Calculate wind speed on m/s
}

ICACHE_RAM_ATTR void addcount()
{
counter++;
}

Nonsense- I have more than a dozen Wemos projects here and the D# for the Wemos pins work just fine.

1 Like

I've tried it's still the same

this is the result

@muhammadyudi
8266 stuff is NOT as carefree as good ol' arduino.

soft wdt reset
software watchdog timer
the loop is NOT looping around fast enough.

@SteveMann
just noting my admittedly less than exhaustive experience with onboard LED near the antenna (that wemos & nodemcu have in common).
Any insight/s on this anemometer matter?

@SteveMann

I looked into this more.
When I select the 'WemosD1R1' board and Upload to a Wemos: the D-numbers do not work.
When I select the 'Nodemcu-12E' board and Upload to a Wemos: the D-numbers work.

Do you have the ESP8266 in your boards manager?

@muhammadyudi
Code Tags (look up code tags, using code tags, . . .)
I found a couple of places to poke at maybe --

#define windPin 2 // Receive the data from sensor

// Definisi konstanta
const float pi = 3.14159265; // pi number
int period = 10000; // Periode pengukuran (milidetik)
int delaytime = 10000; // Waktu antar sampel (milidetik)
int radio = 90; // Jarak dari pusat kincir angin ke cangkir luar (mm)
int jml_celah = 18; // jumlah celah sensor

// Variable definitions
unsigned int Sample = 0; // Sample number
unsigned int counter = 0; // B/W counter for sensor
unsigned int RPM = 0; // Revolutions per minute
float speedwind = 0; // Wind speed (m/s)

void setup()
{
  // Set the pins
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);

  // sets the serial port to 9600
  Serial.begin(9600);

  // Splash screen
  Serial.println("ANEMOMETER");
  Serial.println("**********");
  Serial.println("Based on depoinovasi anemometer sensor");
  Serial.print("Sampling period: ");
  Serial.print(period/1000);
  Serial.print(" seconds every ");
  Serial.print(delaytime/1000);
  Serial.println(" seconds.");
  Serial.println("** You could modify those values on code **");
  Serial.println();
}

void loop()
{
  Sample++;
  Serial.print(Sample);
  Serial.print(": Start measurement…");
  windvelocity();
  Serial.println(" finished.");
  Serial.print("Counter: ");
  Serial.print(counter);
  Serial.print("; RPM: ");
  
  RPMcalc();
  
  Serial.print(RPM);
  Serial.print("; Wind speed: ");
  
  WindSpeed();
  Serial.print(speedwind);
  Serial.print(" [m/s]");
  Serial.println();
  delay(5000);
}

// Measure wind speed
void windvelocity()
{
  yield();   // new
  speedwind = 0;
  counter = 0;
  attachInterrupt(digitalPinToInterrupt(0), addcount, CHANGE);
  unsigned long millis();
  unsigned long startTime = millis();  // ← it's a change
  while(millis() < startTime + period) 
  {
    yield();
    // empty  why??
  }

  detachInterrupt(1);
}

void RPMcalc()
{
  RPM = ((counter/jml_celah)*60)/(period/1000); // Calc revs per min (RPM)
}

void WindSpeed()
{
  speedwind = ((2 * pi * radio * RPM)/60) / 1000; // Calc wind spd on m/s
}

ICACHE_RAM_ATTR void addcount()
{
  counter++;
}
1 Like

The closest to that is "Generic ESP8266 Module", I guess.
The others are 'board specific'.