How to use this Arduino code for Raspberry Pi via Node-Red?

Hello everyone, I am a new Node-Red user using a Raspberry Pi :smiley:

Previously, I had purchased an anemometer sensor to measure air speed, here is the product:

I bought one that supports arduino, and got the following code from the seller:

// Pin definitions

# define windPin 2 // Receive the data from sensor

// Constants definitions
const float pi = 3.14159265; // pi number
int period = 10000; // Measurement period (miliseconds)
int delaytime = 10000; // Time between samples (miliseconds)
int radio = 90; // Distance from center windmill to outer cup (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

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

void loop()
Serial.print(": Start measurementā€¦");
Serial.println(" finished.");
Serial.print("Counter: ");
Serial.print("; RPM: ");
Serial.print("; Wind speed: ");
Serial.print(" [m/s]");

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


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()

The code is a code for reading air speed from an anemometer sensor whose products I have listed in the previous link, but the code is intended for Arduino. As for the microcontroller that I use is a Raspberry Pi with integrated sensor function settings through Node-Red, and my problem for now is how to set up the anemometer sensor. Moreover, there is no special palette that can be installed through Node-Red to be used as a node that can be connected to the Raspberry Pi.

Please can you help me condition the code to work on a Raspberry Pi via Node-Red? :smile:

Thank you :wink:

I would investigate using mqtt in your code. That way you could setup a mqtt broker on the Pi and use an mqtt-in node to receive the messages the Arduino code sends out.

  1. To install it on the Pi, you can see: Install Mosquitto Broker Raspberry Pi | Random Nerd Tutorials
  2. If you haven't used MQTT then see this tutorial MQTT Essentials - All Core Concepts Explained
  3. do a google search for info on using mqtt in the Arduino code

Are you trying to use a Pi instead of the Arduino (which may not be possible, or at least not easy) or are you trying to use an arduino and communicate with node-red on the pi?

Hello zenofmud and colin, thanks for the answer

I here intend to really use the Raspberry Pi, and don't intend to use Arduino. Previously, I had installed other sensors such as the DHT22 and DS18B20 on my Raspberry Pi, whose readings were regulated via Node-Red and the results were safe, moreover, both types of sensors had a palette that could be installed. However, here the problem is that the Anemometer sensor that I bought does not have a palette like the previous two types of sensors and the seller only provided a code for use via Arduino. So, what I really want to ask is how do I use the Anemometer sensor properly using a Raspberry Pi? and How do I change the code that has been given so that it can be used via Node-Red? By changing the language from C++ to Javascript? and to use it, is the resulting code that has been converted into Javascript inserted into the node function?

Thank you :wink:

Looking at the code it appears that it is getting pulses on a digital input pin and counting them over a period to determine the speed of rotation. You will have to work out what sort of output the sensor provides and how to connect that to one of the pi GPIO inputs. Then count the pulses that come in. Whether the pi will cope with it depends on the speed that the pulses are coming in at. The Pi OS is not real time so there is the possibility of occasionally losing pulses if they come in too fast. Really this is a situation where you would be better to use something like an arduino or an ESP32 for connecting the sensor.

You need to check if the anemometer can be powered by a 3.5V pin or if it needs 5V.
If it's 5V, is it safe to connect the output to a GPIO pin?

Then wire it up, setup the data pin in Node-red and see what a debug node shows.

Most likely it is just a reed relay which closes each time the anemometer turns.

Thank you very much for the answer

Colin, thank you for your explanation and suggestion, I will take your suggestion into consideration

Then for jbudd, based on the information from the seller for the sensor is able to run with a voltage of 3.3V, so this is safe to be connected to the GPIO pin

I presume that the anemometer has to be exposed to the full fury of winter tempests.
And I guess that 3.5V signals can only reliably work over a fairly short wire, thus your Raspberry Pi has to be outdoors too and hopefully protected from the weather.

There is no way I would put a Raspberry anywhere the damp can get at it.
Instead I would ensure there was a wifi signal at the anemometer location and use a cheap ESP32 to collect data and transmit it over MQTT to the Pi safe and snug indoors.

If wifi was impossible, LoraWan.

1 Like

What device is the anemometer?

Ok, thanks a lot jbudd for the explanation, I'll consider it.

For Colin, based on information from the seller, the anemometer that I use uses the A3144 sensor.