The MagPi - July 2018

(Steven Felgate) #1

raspberrypi.org/magpi May 2018 75


BUILD A WEATHER STATION Feature


raspberrypi.org/magpi July 2018 75


CONNECTION
Anemometers normally have two wires.
Connect one to a GND pin, the other to
GPIO 05. If using the RJ11 connectors,
the anemometer uses the middle two
wires of the cable: normally pins 3 and 4
on RJ11 breakout boards.

TEST WITH CODE
Open IDLE, create a new Python file,
and save it as wind_speed.py in the
/home/pi/weather-station directory.
Add the code from the wind_speed.py
listing. Save and run your code. Test
it by manually turning the arms of
the anemometer.

STEP 3


WIND SPEED
The anemometer produces two signals
per spin, so you can count the number
of full rotations of the sensor by
halving the number of detected inputs.
This can then be used to calculate the
wind speed:

speed = distance / time

To calculate speed, you need to know
the distance travelled in a certain
amount of time. Measuring time is
fairly straightforward, and you can
count the number of signals over
the course of a fixed time period,
for example 5 seconds. The distance
travelled by one of the cups will be equal
to the number of rotations multiplied
by the distance around the edge of the
circle (circumference):

speed = (rotations × circumference)
/ time

The circumference can be calculated as
long as you know either the radius of
the circle. You can discover the radius
made by the anemometer by measuring
the distance from the centre to the edge
of one of the cups. Once you know the
radius, you can find the circumference

with the formula 2 × π × radius. Don’t
forget that a whole rotation generates
two signals, so you’ll need to halve the
number of signals detected:

speed = ( (signals/2) × (2 × π × radius) )
/ time

The radius for the recommended
anemometers used by the original
Oracle Weather Station is 9.0 cm, and
that is the figure which is used in the
code example. Don’t forget to change
this value if your anemometer has
different dimensions!
To implement this formula in Python,
you can use the math library. For
example, if you measured 17 signals
from your anemometer in 5 seconds,
your wind speed could be calculated like
the code in wind_speed.py.

STEP 4


WIND DIRECTION
If you look inside the recommended
wind vane, you’ll see eight reed switches
arranged like the spokes of a wheel.
These can be used to measure the wind
direction. In order to do so, you’ll need
to be able to measure the resistance
produced by the sensor and convert that
into an angle value. There are several
steps in this process, and you need an
analogue-to-digital converter (ADC).
A popular and versatile ADC is the
MCP3008 (magpi.cc/BlHlKm)
Measuring the wind direction is a
bonus project and you can read how to
set up the rotating magnets and ADC
here: magpi.cc/nZNLga.

STEP 1


STEP 2


from gpiozero import Button
import time
import math

wind_count = 0 # Counts how
many half-rotations
radius_cm = 9.0 # Radius of your anemometer
wind_interval = 5 # How often (secs) to report speed

# Every half-rotation, add 1 to count
def spin():
global wind_count
wind_count = wind_count + 1
# print("spin" + str(wind_count))

# Calculate the wind speed
def calculate_speed(time_sec):
global wind_count
circumference_cm = ( 2 * math.pi) * radius_cm
rotations = wind_count / 2.0

# Calculate distance travelled by a cup in cm
dist_cm = circumference_cm * rotations

speed = dist_cm / time_sec

return speed

wind_speed_sensor = Button( 5 )
wind_speed_sensor.when_pressed = spin

# Loop to measure wind speed and report at 5-second intervals
while True:
wind_count = 0
time.sleep(wind_interval)
print( calculate_speed(wind_interval), "cm/h")

01.
02.
03.
04.
05.

06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.

wind_speed.py CODE


Language: Python
Name: wind_speed.py
magpi.cc/ABckLC
Free download pdf