Skip to main content

The Raspberry Pi Weather Station – In Ten Minutes Flat | DesignSpark

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

v

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:

system1_0f6061b5e9f6bd1eb46203d7db59588ff54f467d.jpg

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:

temp13_8fa763a4002e60b1ef7e4a9600340be6973db125.jpg

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:

kernel_drivers_8906b052714d3b50c6de04d5c62fcb6c9ed4b259.jpg

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>:

config_011_ffb1cd40220280279278febcaa81c0b914407b85.jpgWe then need to move down to ‘Interfacing Options’ and <Enter>:

config_02_fb18ad1751f315c6b9b56d68090086bba1a506f3.jpg

Then select P5 I2C:

config_03_24829fdef7c72f871bd933196e5d1013f13ebbf3.jpg

And we get:

config_042_4a7ef9959685c60f0108a5a41fd7dfb2c5685769.jpg

Select <Yes> which should produce:

config_05_01e82369885714b3184ce3b36ac80b09a5f61054.jpg

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

config_06_25ca7349f8f19321b6df8150f0c364fd580e5ce4.jpg

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:

tools_01_a978b9c3e450c03efd2dbcab9b9f26d5dbfc0b76.jpg

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

tools_02_167adac355ff7869e1c7982256c22a7e33e81aae.jpg

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:

test11_0465e19b355dd0ca3ff1e7435dc911df3ef8c843.jpg

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:

no_output_c4ae610d597a04b741b3810142241678b6a131c7.jpg

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:

logfile_dc055ce0156cbf2dcdf707131ee86a79f10dc0ad.jpg

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 worked in real-time digital signal processing applications engineering for a number of years, before moving into technical marketing.
DesignSpark Electrical Logolinkedin