How to build a Raspberry Pi weather station

The mini marvel that is the Raspberry Pi computer has been a barnstorming success since the original was launched back in 2012, selling more than 19 million in total to date. The tiny but wallet-friendly PCs can handle all manner of tasks, including a vast array of projects - in this case, a weather station.

Building a weather station is just one of the awesome projects within the grasp of a Raspberry Pi owner, and as with almost every Pi project, there are a variety of different approaches available, depending on how professional you'd like the finished product to be, how much you plan to spend, and of course what specific aspects of weather you'd like to measure.

There are two different 'routes' here - indoors and outdoors. That might sound a bit counter-intuitive, but essentially outdoor weather stations can measure environmental factors like wind speed and rainfall, while an indoor station will be more geared to measuring pressure changes, temperature and perhaps air quality.

There are plenty of ready-to-assemble outdoor kits out there - such as this from the Pi Hut which takes the pain out of constructing your own-brew anemometer. However, on the downside you will need to source a waterproof case and hack together the supporting scripts. If you fancy going full-bore MacGyver then it's worth learning from this impressive station, involving Christmas baubles and electrical glands.

Another outdoor weather station kit you might encounter is the Raspberry Pi Foundation's collaboration with Oracle, which resulted in a Weather Station Kit that has been shipped to schools across the globe. This is based on a Raspberry Pi HAT (Hardware Attached on Top) board, which links into the Pi's GPIO header board by mounting directly on top.

Although the Oracle Weather Station Kit is not available publicly, there are other options that deliver a similar HAT-experience, most notably the Raspberry Pi Sense HAT which, once plugged atop your Pi, gives you an 'Astro Pi', complete with sensors that can monitor orientation (yaw, pitch & roll) via an accelerometer, 3D gyroscope and magnetometer, pressure, humidity and temperature, as well as fronting an LED panel that can display pretty much anything you want.

The Sense HAT was originally designed to be sent into space on the ISS to carry out a range of experiments, so should be rugged enough for your back garden. This is a great starting point for an indoor/sheltered outdoor weather station that can be brewed up with little in the way of hardware difficulty - not a Christmas bauble in sight.

The basic equipment you'll need is a Raspberry Pi 2 or 3, a Pi power supply, a Micro SD card (or SD for older Pi's), a Sense HAT and some species of Pi case to hold it all together. If you plan to measure outdoor temperatures and humidity - perhaps from a windowsill or covered porch - then a weatherproof case would be a good idea at a minimum. It's also worth considering getting a Pi 3 if you're buying specially for this project, as the Pi 3 has inbuilt Wifi, which will save running Cat5 Ethernet cabling to your porch.

This particular project not only measures temperature, humidity and pressure using the Astro Pi Sense HAT, but also uploads the data to Weather Underground - a platform which allows users to network their connected thermostats and weather sensors together to create a detailed, community-driven repository of real-time weather data.

Step 1

Install the latest version of the Raspbian OS on your Pi via the NOOBS installer. You can find a guide on how to do so here. Once your Pi is set up, connect your Pi and Raspberry Pi Sense HAT together and mount the device in your chosen case.

After that, you should make sure that your Pi is up to date with all the relevant patches and drivers. You can check for updates with the following terminal commands:

sudo apt-get updatesudo apt-get upgrade

Step 2

Next, you're going to want to grab the libraries to power the Sense HAT. Again, you can install these with some simple terminal commands:

sudo apt-get install sense-hatsudo reboot

Step 3

Once the Sense HAT libraries are installed and the reboot is complete, make a folder for this project to live in:

mkdir folder_nameEg: mkdir pi_weather_station

Finally, you're ready to copy the project files from this Github repo into that folder. You'll also need to setup a station on Weather Underground, which will generate a station ID and access key which you'll need for the next step.

Step 4

Open the project's config.py file in your editor and fill in the STATION_ID and STATION_KEY fields with your values from your Weather Underground Weather Station, like so:

class Config:# Weather UndergroundSTATION_ID = ""STATION_KEY = ""

The main application file, weather_station.py has two settings that control how the program operates. Open the file in your editor and find the following line:

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

The MEASUREMENT_INTERVAL variable controls how often the application reads temperature measurements from the Sense HAT. To change how often the application checks temperature values, change the value on the right of the equals sign on the second line.

If you're testing the application and don't want the weather data uploaded to Weather Underground until you're ready, change the value for WEATHER_UPLOAD to False when testing the code and/or hardware, and set it to True to enable upload of weather data to Weather Underground.

Step 5

You can test the data collection application by navigating to the project folder via the terminal and typing:

python ./weather_station.py

The terminal window should display this:######################################### Pi Weather Station ## By John M. Wargo (www.johnwargo.com) #########################################

Initializing Weather Underground configurationSuccessfully read Weather Underground configuration valuesStation ID: YOUR_IDInitializing the Sense HAT clientInitialization complete!

By default the application will start collecting data and uploading it to the Weather Underground every 10 minutes, unless you've already tweaked that setting. Now, enjoy the weather!

(Thanks to John M. Wargo)