DesignSpark Electrical Logolinkedin
Menu Search
Ask a Question

The Raspberry Pi Weather Station – In Ten Minutes Flat

The weather is a hot topic again in the UK, where we are experiencing something of a heatwave. This is generally referred to as ‘summer’ elsewhere in the world but heat in summer is something of an outlandish concept to the people of this island!

Parts List

Qty Product Part number
1 × Raspberry Pi 3 Model B+ 1373331
1 × Raspberry Pi Bridge 1743694
1 × XinaBox MD01 for use with Blank Placeholder 1743711
1 × AVX BTB Series, Male Edge Connector For use with Xinabox XChips, SMT, 10 Way, 2 Row, 2mm Pitch, 2.5A 1744977
1 × XinaBox SW01, Advanced Weather Sensor Module for BME280 1743744
See more products

The weather is a hot topic again in the UK, where we are experiencing something of a heatwave. This is generally referred to as ‘summer’ elsewhere in the world but heat in summer is something of an outlandish concept to the people of this sceptred isle. Much like that other aberrant period, winter, we are woefully unprepared for the kind of weather that is generally considered standard for the season.

But fear not, friends. Whether you live on ‘this precious stone set in the silver sea’ or elsewhere in the world, it has never been easier to keep track of how inclemently the weather is behaving. You can do it with just your Raspberry Pi, a couple of low-cost Xinabox parts and ten minutes of your time.

If you are new to XinaBox, you will find that these little, square modules are great for clipping together super-fast prototypes for all sorts of circuit functionality.

The SW01 module sports a Bosch BME280 digital sensor that will give us accurate readings for temperature, humidity and air pressure. XinaBox modules use I2C to interface with each other and the BME280 is an I2C device, so we can use existing Python BME280 libraries to drive the sensor from our Raspberry Pi. For this experiment, I will be using Richard Hull’s Python module, though there is also an Adafruit module you could use.

The BR01 Rasperry Pi bridge fits onto the GPIO pins furthest away from the USB sockets on your Pi:

We are using the MD01 spacer (or in my case, the MD03) to make sure our sensor is not reading the temperature directly above the Broadcom processor. As regular Pi users will know, the processor can get quite warm, even when it isn’t doing all that much:

I used a quick shell script to get the processor temperatures and even doing nothing, the processor is at 45 degrees C. Moving the sensor away from this heat source, using the spacer, should (mostly) mitigate any distorted readings.

Prerequisite Setup

The first thing we need to do is make sure the I2C kernel driver is enabled on our Raspberry Pi. We can do this by running dmesg or lsmod from the command line in a terminal:

If we find, unlike in the picture above, that we have no listed I2C kernel modules then we will need to enable them.

In your terminal type:

$ sudo raspi-config

 

Then use the down arrow key to move down to ‘Advanced Options’ and hit <Enter>:

We then need to move down to ‘Interfacing Options’ and <Enter>:

Then select P5 I2C:

And we get:

Select <Yes> which should produce:

Hit the <OK> which takes us back to the main menu. Use the right arrow key to get to <Finish>:

If we reboot and check dmesg or lsmod again, we should see the kernel modules are now loaded.

The next thing we need to do is install some tools and an essential pre-requisite Python library:

With the I2C tools installed, we can check that our weather sensor is visible on the bus:

There it is at address 0x76. Great.

Now we can install our Python module:

$ sudo pip install RPi.bme280

If you want to use Python 3, then you will need to use pip3:

$ sudo pip3 install RPi.bme280

Programming

At this point, we are ready to begin writing our Python Program. We will start with a really rough n’ ready program, just to make sure our hardware is behaving as expected. Open the text editor or use Geany to enter a test script:

import smbus2
import bme280

bus = smbus2.SMBus(1)

bme280.load_calibration_params(bus, 0x76)

print(bme280.sample(bus, 0x76))

We can save this as test.py.

Open a terminal and run the script from the command line:

Good. Our hardware has produced a sensible output, so we know it’s all working as it should. Now we can go on and do something useful, like logging our readings over time into a file. For this we are going to need a slightly more complicated Python script:

import time
import smbus2
import bme280

bus = smbus2.SMBus(1)
address = 0x76

calibration_params = bme280.load_calibration_params(bus, address)
layout = '{0:5d}:  {1},  {2:0.3f} deg C,  {3:0.2f} hPa,  {4:0.2f} %'
counter = 1

while True:
    data = bme280.sample(bus, address, calibration_params)
    with open("sample.log","a+") as f:
        f.write(layout.format(counter, data.timestamp, data.temperature, data.pressure, data.humidity) + "\n")
    f.close()
    counter += 1
    time.sleep(5)

When we save and run this, not much happens in the terminal. Instead, our output is appended to a file on each loop:

I have used only a 5-second interval between each loop in order to get some quick data output, but in a real-world logging situation, you would probably want to have an interval of something more like 5 to 15 minutes between log entries. The log file ends up looking something like this:

Final Thoughts

The XinaBox x-chips make it incredibly fast to try out any ideas you may have. You don’t even need to know much about electronics – you just need to know the functionality you want. Then you can clip these functions together in seconds and get straight to programming. This is ideal for super-quick prototyping or makeshift measurement instruments that you can take apart and re-use for other purposes when you’re done.

There are over 80 x-chips covering a host of different processor, sensor and communications functions. More are being added all the time, so I’m excited by the prospect of being able to build and deploy almost any kind of IoT or autonomous embedded system that I can imagine, pretty much at the drop of a hat. You can’t help feeling the future looks bright!

Mark completed his Electronic Engineering degree in 1991 and went on to work in real-time digital signal processing applications engineering, later moving into technical marketing.

Comments

August 7, 2018 07:47

A nice addition to this project would be to export the data to a spreadsheet so that temperature could be viewed over time.

0 Votes

August 7, 2018 14:42

@Les_B I have tried to keep this project as simple as possible, but you could write your data directly to Excel (.xlsx) files if you wish, using the openpyxl or xlsxwriter Python packages.

July 25, 2018 08:03

Nice intro project, Mark! Look forward to more with the XinaBox.
Also, from a fellow Fallout fan - cool 'T-51b armor' in the Terminal :)

July 23, 2018 08:14

@davecole, I think the last column of the output is the relative humidity. The code for it in the source code appears to be cut off by the edge of the screen capture window.

While this is a nice example of quickly getting a Raspberry Pi doing some monitoring of its environment, there are quite a few additional considerations for turning it into a station that could read outdoor weather (apart from sensing additional environmental variables).
For example, besides being separated from the influence of the processor's heating, the temperature sensor needs to be protected from direct sunlight, but still have sufficient exposure to the air (not a sealed box). Usually a louvered enclosure is used to do this.
You need to decide where the processor will be - indoors or out? If outdoors, you need check what operating conditions your processor is rated for. Will it handle the range of heat and cold, and possibly very high humidity?
If you keep the processor inside, you need to determine how you will get the signals from the sensors to the processor. Direct cabling? Some interfaces are only designed for short distances. Analog lines will pick up noise. And longer wiring increases the exposure to electrical surges and differences in ground potential between the sensors and processor. (Including nearby lightening strikes, for weather stations.)
These can be challenges, but good opportunities to learn more about real-world engineering.

(I know - I designed a network of weather stations.)

July 25, 2018 15:45

@BradLevy Thanks for laying out the design considerations for a full-blown weather station. We will be looking at how to upload the data produced from such a station to the cloud, to share with the world, in an upcoming article.

July 25, 2018 08:03

Thanks @BradLevy - really useful extra information

July 23, 2018 08:14

Great Project... A weather station is obviously more than just temperature, I'd be interested to also see humidity and maybe rainfall. Will this product allow you to expand to monitor these as well? Can you do a follow up of how you would increase the monitoring and send this data to the IoT cloud?

July 25, 2018 15:44

@davecole Thanks. The Bosch BME280 sensor used here will also measure humidity and pressure, so it is like a digital version of your old wall barometer. There are a large number of other sensors available in the x-chip range for light, VOCs etc, though if you want to measure things like rainfall and wind speed, you will have to go off-piste as far as ready-made x-chips go.