Adding sensors to a RPi weather station

In the previous posts I set up a Raspberry Pi weather station using a USB radio reciever to pickup the data direct from the sensors. Unfortunately the pressure sensor is located in the base station rather than the outdoor sensors so I cant read it. As a work around I used a BME280 temperature/pressure sensor from Pimoroni.  Although Weewx is capable of running a service to poll the sensor, I found it simplest to grab a reading from the sensor into sdr.py as the recieved packets are processed. The patch is in /usr/share/weewx/user/sdr.py. First you will need to connect up the sensor and install the drivers. I found it useful to install drivers for both Python 2 and 3. You will also need to enable the i2c inteface for the pi. See the instructions on this page. You will need to run both sudo pip install ... and sudo pip3 install.... to get both versions.

Mods to sdr.py

Near the start of sdr.py, where the imports are defined, add code to import the smbus and bme280 libraries:

...
import time
## RWS - BME280 sensor with Pimoroni library
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
...

After the section where various constants are added, include code to initialise the sensor:

...
DRIVER_NAME = 'SDR'

DRIVER_VERSION = '0.77'

# Initialise the BME280 in 'forced' mode
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
bme280.setup(mode="forced")
...

Finally, locate the class that handles processing the packets for your sensor (in my case the Bresser6in1Packet.parse_json), add a line to append the sensor reading to the packet:

 @staticmethod
def parse_json(obj):
pkt = dict()
pkt['dateTime'] = Packet.parse_time(obj.get('time'))
pkt['usUnits'] = weewx.METRICWX
pkt['station_id'] = obj.get('id')
pkt['battery'] = obj.get('battery_ok')
pkt['pressure'] = bme280.get_pressure()
......

You can of course add indoor temperature and humidity if you wish.