rasppiweatheron-1jbr

Read articles from the magazine right here on Make:. Don’t have a subscription yet? Get one today.

For years I’ve wanted to set up a weather station in my backyard, but I’ve balked at the cost of those expensive kits. When the folks at Astro Pi released the Sense HAT sensor board for the Raspberry Pi computer, I knew I had an easy way to build my own Pi-based weather station using off-the-shelf parts.

The Sense HAT was designed to be sent into space, and schoolchildren in England were engaged to create (and code, of course) experiments for it that would be executed by an astronaut on the International Space Station. The HAT stacks right on top of the Raspberry Pi computer and has the following hardware capabilities:

  • Temperature, humidity, and pressure sensors
  • Accelerator, gyroscope, and magnetometer
  • 8×8 full-color RGB LED display
  • 5-button joystick

For this project, I use the Sense HAT to measure temperature, humidity, and barometric pressure. Once I started collecting the data, I needed to do something with it, so I coded the application to upload the measurements to Weather Underground, creating my own online weather station. Weather Underground (WU) lets you set up your own station and upload your data for others to use; your data becomes part of the aggregate weather data, and you (and your neighbors) can view your station’s data separately as well.

Since the HAT also has an LED display, I decided to use it to display information about the weather data. You could use it to display numbers (1 or 2 digits at a time), but I decided to display a red arrow pointing up when the temperature increased over the last reading, a blue arrow pointing down when the temperature decreased, and blue and red bars (like a strange equal sign) when the temperature stays the same between measurements.
The project’s really easy to complete. Just assemble the hardware (which takes about 5 minutes), install some Python libraries, download and configure the project’s code, and you’re all set. Overall, the whole project should take you no more than an hour. The complete source code is available on Github.

You’ll also want to mount the project in a container that protects it from the elements while at the same time enabling the HAT’s sensors to accurately measure current conditions. I’m going to use mine on my covered back porch, but I also improvised a rain shield for exposed locations.

Project Steps

WEATHER UNDERGROUND WEATHER STATION SETUP

Weather Underground (WU) is a public weather service, now owned by the Weather Channel, that lets you upload local weather data into the WU database for public consumption. Setting up a WU weather station is free and easy to do. Point your browser of choice to the Weather Underground Personal Weather Station Network (PWSN) home page and click the Join button in the upper-right corner.

pi-weather-station-01

On the page that appears, you’ll be prompted to enter your email address, a user name, and password to create a new account. Then go back to the PWSN home page and click the My PWS button at the upper right. On the page that appears, populate the form to create your new personal weather station.

pi-weather-station-02

Once you complete the setup, WU will generate a station ID and an access key you’ll need to access the service. Be sure to capture those values, you’ll need them to configure the project application later.

ASSEMBLE THE HARDWARE

Assembly is really easy. Just mount the Sense HAT on top of the Raspberry Pi, put it all in the case, and power it up.

The C4 Labs Zebra Case comes with 2 heat sinks, one for the top of the Pi and one for the bottom. Mount these before assembling the case; they’ll help dissipate heat from the processor and should help reduce the effect of the Pi’s processor on readings (see Calculating Ambient Temperature further down).

pi-weather-station-03

The Zebra Case is built in layers, so you have to stack multiple parts around the Pi to assemble it. Build the case, following the included instructions, but leave the final piece off as shown in Figure C.

The Sense HAT Upgrade replaces the top layer of the case with several parts, including a set of longer screws. In Figure C, the original case top is sitting to the left of the Pi, with the extra Upgrade parts behind the device.

Mount the Sense HAT on the Raspberry Pi. The Sense HAT will sit flush against the top of the colored part of the case as shown in the photo below.

pi-weather-station-04

Note: The Raspberry Pi Foundation recommends that you install standoffs between the Pi and the HAT for stability, but you can’t use these standoffs with the C4Labs Zebra case; the HAT won’t fit correctly if you do.

Next comes a clear piece that fits flush around the outline of the Sense HAT as shown below.

pi-weather-station-05

Finally, add the remaining 2 pieces: one covers the Sense HAT around the joystick area, and the top plate covers the rest, while still exposing the Sense HAT’s sensors. You can see the assembled case below.

pi-weather-station-06

Before you tighten the case using the longer screws provided with the Sense HAT Upgrade, be sure to look around all sides to make sure everything fits snugly. If you see any big gaps or bent parts, then you may not have the case assembled correctly.

SET UP THE RASPBERRY PI

The Raspberry PI needs an OS to boot, so grab your microSD card, then follow the instructions here  to download and install the OS on the SD card. Once that’s completed, insert the SD card into the Pi. Turn on your monitor, then plug the Pi’s power supply into a power outlet and the Micro-USB port on the Pi.

Now you’ll configure some system-wide settings on the Raspberry Pi. Open the Raspberry menu in the upper left corner of the Pi screen, then select Preferences→Raspberry Pi Configuration (see the photo below).

pi-weather-station-07

By default, the Raspbian image only utilizes a part of the SD card’s storage (3GB); if the card you used for your Raspbian installation is larger than 3GB, you’ll need to expand the file system to use the whole disk, to make sure you’ll have room for the additional software used in this project.

In the Raspberry Pi Configuration utility shown in the photo below, click the Expand Filesystem button to enable the Pi to use the whole SD card for storage. Making this change will require rebooting your Pi, so don’t be surprised.

pi-weather-station-08

If you want, use this opportunity to change the host name for the Pi device, so you can easily find it on the network later. You can see I’ve named mine pi_weather. If you’re a U.K.-based reader, you’re done; click the OK button and let the Pi reboot.

If you’re outside the U.K., switch to the Localisation tab and make sure the settings are properly configured for your locale, time zone, and keyboard. Click OK and reboot.

Next you’ll need to update the Pi’s core software. Open a terminal window and execute the command:

sudo apt-get update

This command updates the Pi’s indexes of available software packages. Next, execute:

sudo apt-get upgrade

This command fetches and installs the latest and greatest versions of the Raspbian OS and other software packages installed on the Pi. It will take quite a while.

INSTALL PROJECT SOFTWARE

The Sense HAT uses its own Python libraries. To install them, go to the terminal window and execute the command:

sudo apt-get install sense-hat

Next, create a directory for the project files:

cd ~
mkdir pi_weather_station
cd pi_weather_station

Then copy the project’s Python source code to the new directory with this command:

wget https://github.com/johnwargo/pi_weather_station/archive/master.zip

and extract the files with this command:

unzip -j master.zip

CONFIGURE PROJECT SOFTWARE

In order to upload data to the Weather Underground service, our Python app needs access to the station ID and station access key you created earlier in the setup process. Open the project’s config.py file in your text editor of choice then populate the STATION_ID and STATION_KEY fields with the appropriate values from your Weather Underground Personal Weather Station:

class Config:
# Weather Underground
STATION_ID = “YOUR_STATION_ID”
STATION_KEY = “YOUR_STATION_KEY”

The project’s main Python app, weather_station.py, has two configuration settings that control how it works. To change these values, open the file in your text editor and look for these lines near the top:

# specifies how often to upload values from the Sense HAT (in minutes)
UPLOAD_INTERVAL = 10 # minutes

The app reads the temperature sensor on the Sense HAT every 10 seconds, for use in calculating ambient temperature. But we don’t want to upload data to Weather Underground that frequently. So, the UPLOAD_INTERVAL variable controls how often the app sends measurements to WU. To change this interval, just change the value to the right of the equal sign.

If you’re testing the app and don’t want your data uploaded to WU until you’re ready, change the value for WEATHER_UPLOAD to False (in Python, case matters, so it has to be False, not false):

# Set to False when testing the code and/or hardware
# Set to True to enable upload of weather data to Weather Underground
WEATHER_UPLOAD = False

TEST THE PROJECT’S PYTHON APP

To run your weather station Python application, open a terminal window, navigate to the folder where you copied the project files and execute the following:

python ./weather_station.py

The terminal window should quickly sprout the following output:

##################################
# Pi Weather Station #
# By John M. Wargo (www.johnwargo.com) #
##################################

Initializing Weather Underground configuration
Successfully read Weather Underground configuration values
Station ID: YOUR_STATION_ID
Initializing the Sense HAT client
Initialization complete!

If you see something like that, you’re golden. If not, figure out what any error messages mean, fix things, then try again. At this point, the application will start collecting data every 10 seconds and uploading it to the Weather Underground every 10 minutes (unless you changed the app’s configuration to change the upload interval).

START THE APP AUTOMATICALLY

Finally, you must configure the Raspberry Pi so it executes the Python app on startup. In a terminal window, navigate to the folder where you extracted the project files. Then make the project’s Bash script file executable by executing the following command:

chmod +x start-station.sh

Next, open the Pi user’s session autostart file using the following command:

sudo nano ~/.config/lxsession/LXDE-pi/autostart

Add the following line to the end (bottom) of the file:

@lxterminal -e /home/pi/pi_weather_station/start-station.sh

To save your changes, press Ctrl-O then press the Enter key. Next, press Ctrl-X to exit the nano application. Reboot the Raspberry Pi. When it restarts, the weather station’s Python process should execute in a terminal window.

pi-weather-station-09

USE IT

You’re now running a personal weather station! You can share your Weather Underground page with friends and neighbors, or just enjoy knowing that you’re contributing to one of the world’s biggest weather databases powered by citizen science.

CALCULATING AMBIENT TEMPERATURE

One of the frustrating parts of building this project was that I ran into a problem with faulty temperature readings. It turns out the Sense HAT has a design flaw, in that the humidity and pressure sensors (each of which can be used to measure temperature) are not thermally isolated from the Pi’s CPU, and therefore can’t measure ambient temperature accurately. Ugh! Anyway, the Pi community has figured out ways to read the CPU temperature, then use that value to guesstimate ambient temperature to within 1°C — pretty good. I implemented one of these solutions in the code so you can grab near-accurate temperature readings. This is especially important if you’re using a Raspberry Pi 3, which generates more heat than older Pi boards.

You can also connect a standalone temp/humidity sensor to the Pi’s GPIO pins, and edit the code to read that sensor too! I’ve built a version using an off-the-shelf sensor (Adafruit #385) and the Adafruit libraries. My revised code for this version lives on Github.

MOUNTING YOUR WEATHER STATION OUTDOORS

The Zebra case is not waterproof, so you’ll want to install it in a sheltered spot like a porch, or make an enclosure that protects it while letting the sensors breathe.

For exposed locations, I improvised a simple rain shield using a plastic food container and a Chinese takeout lid. It’s open at the bottom, with vents at the top, so the temperature should equalize easily with the surroundings (Newton’s law of cooling applies). I didn’t want to drill holes in the sides, as that would let water in, so I mounted the Pi on a piece of ¼” plywood and velcroed it to the inside of the container. To see how I made it, read on — and I’d love to see your ideas too!

mount-020

At first I wasn’t sure how to design an enclosure that still exposed the temperature and humidity sensors, but then I found this helpful forum post that discussed issues with mounting hardware outdoors. I knew that if I provided enough ventilation while at the same time protecting the Pi from direct contact with water, I’d be OK. So, I gathered the following parts and got to work:
• Plastic Storage Container
• Lid from a fast food container
• 2″×6″ piece of ¼ plywood
• 2″×3″ piece of ¾ plywood
• External grade Velcro®
• Mounting screws
• Spacers

I didn’t want to drill any holes in the container as that would let water in. So, I mounted the Pi on a piece of ¼” plywood then used velcro to attach it to the inside of the container. Cut the plywood so it’s just a little wider than the Pi and it’s long enough to reach the lip of your plastic container. Position the Pi close to one end of the board then trace the mounting holes on the Pi on the plywood.

mount-02

You’ll need longer screws in order to mount the Pi to the board.

mount-03

Add spacers to the board. You’ll need a specific size to fit the separation between the Pi and the Sense HAT. The ones on the bottom of the Pi can be as long as you want

mount-04

Mount the Sense HAT on the Pi.

mount-05

Attach the Pi to the plywood.

mount-06

mount-07

Next, cut a small piece of ¾” plywood to act as a standoff between the ¼” plywood and the inside wall of the container. Mount the board on the back of the ¼” plywood as shown in Figure 8 and Figure 9. In my implementation, I used ¾” pan head screws. Waterproof glue could work as well.

mount-08

mount-09

Next, cut a piece of Velcro to fit the plywood extension. For my implementation, I used the harder side of the Velcro pair.

mount-10

mount-11

Cut the other half of the Velcro, in my case, the softer side, remove the backing and mount it inside the container positioned to it holds the Pi up toward the top of the container.

mount-12

mount-13

mount-14

mount-15

Next, cut some holes in the top of the container as close to the actual top of the container as possible . You want the holes to be above the lower edge of the lid when attached.

mount-18

Then, attach the food container lid on top of the container, making sure to cover the holes .

Finally, mount the Pi inside the container, power it up and you’re ready to go.