The MagPi - July 2018

(Steven Felgate) #1

raspberrypi.org/magpi May 2018 77


BUILD A WEATHER STATION Feature


raspberrypi.org/magpi 77


TIP THE BUCKET


The product data sheet (magpi.cc/ltofJB)
tells us that 0.2794 mm of rain will tip
the bucket. You can multiply this by the
number of tips to calculate the amount of
rainfall. If you’re using a different type
of rain gauge, then you should consult
the relevant data sheet or determine the
volume of water required experimentally.


STEP 2


INSIDE THE BUCKET
The top of the back wall does come off if
you want to see inside; just pull on the
flat end gently and it should release.
Inside there’s a small circuit board
that you can remove to examine. In the
middle of it you will see the reed switch.
Replace the circuit board and back wall lid
before continuing.
When one of the buckets tips, the
magnet passes the reed switch, causing
it to close momentarily. So, just like with
the anemometer, if you connect the rain
gauge to a GPIO pin on the Raspberry Pi,
you can treat it like a button and count the
number of ‘presses’ to calculate rainfall.


STEP 3


WIRE IT UP
To test the rain gauge, you’ll need to
either remove the RJ11 connector and
strip the wires, or make use of an RJ11
breakout board.
The Oracle Weather Station rain
gauge is connected to GPIO 06,
so for consistency, use the same
pin for your device. (See the
main Figure 1 circuit diagram on
page 72.)


STEP 4


WRITE THE CODE


Using the code you wrote for the
anemometer as a starting point,
write a program called rainfall.py
(saved in the /home/pi/weather-
station directory) to detect when


STEP 1


One of the best ways to store
your weather data is in a
database. Databases can store
very large numbers of records
efficiently and make it easier
to sort, search, and analyse
your data.
There are many different
pieces of database software,
and MariaDB is a good, versatile
general-purpose product. You
should have already installed
it – if not, head back to the
‘Software download’ section on
page 71 and install it.
Now follow the instructions at
magpi.cc/ltofJB to learn how to
set up and run your database.

July 2018

from gpiozero import Button

rain_sensor = Button( 6 )
BUCKET_SIZE = 0.2794
count = 0

def bucket_tipped():
global count
count = count + 1
print (count * BUCKET_SIZE)

def reset_rainfall():
global count
count = 0

rain_sensor.when_pressed =
bucket_tipped

01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.

rainfall.py


CODE
Language: Python
Name: rainfall.py
magpi.cc/uNgWwe

the rain gauge bucket has tipped. It should
print out a running count of how many
bucket tips have occurred.
Now that you can count bucket tips,
you need to convert this into a height of
water that equals the amount of rain that
has fallen.
Finally, we use a function named
reset_rainfall to reset the count of
bucket tips so that it starts at 0 again.
You will need this function for your fully
operational weather station.
Free download pdf