Notes

A weather station on a Pi Zero

The thermometer on my balcony disagrees with the forecast by three degrees in either direction depending on its mood. I wanted a log I could look at later, so I built one from a Raspberry Pi Zero 2 W that had been sitting in a drawer and a BME280 breakout board — temperature, humidity and pressure on I²C, four wires.

Reading the sensor

Enable I²C in raspi-config, connect the board and check that it shows up:

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
...
70: -- -- -- -- -- -- 76 --

Most boards answer at 0x76, some at 0x77. Raspberry Pi OS refuses to install Python packages system-wide these days, so the script lives in its own virtual environment with smbus2 and RPi.bme280:

import sqlite3
import time

import bme280
import smbus2

PORT, ADDRESS = 1, 0x76

bus = smbus2.SMBus(PORT)
params = bme280.load_calibration_params(bus, ADDRESS)
s = bme280.sample(bus, ADDRESS, params)

db = sqlite3.connect("/home/pi/weather/weather.db")
db.execute("CREATE TABLE IF NOT EXISTS readings (ts INTEGER, t REAL, h REAL, p REAL)")
db.execute("INSERT INTO readings VALUES (?, ?, ?, ?)",
           (int(time.time()), s.temperature, s.humidity, s.pressure))
db.commit()

Every five minutes

I used a systemd timer rather than cron, mostly to have the logs in one place:

# /etc/systemd/system/weather.service
[Unit]
Description=Read BME280 once

[Service]
Type=oneshot
User=pi
ExecStart=/home/pi/weather/venv/bin/python /home/pi/weather/read.py

# /etc/systemd/system/weather.timer
[Unit]
Description=Read BME280 every five minutes

[Timer]
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target

Then sudo systemctl enable --now weather.timer, and systemctl list-timers shows when it runs next.

The first week

-10° -5° 0° 5° Mon Tue Wed Thu Fri Sat Sun
Balcony temperature, 9–15 February. Five-minute readings, hourly averages.

The first readings were about two degrees too warm. The sensor sat right above the Pi, which is warm even when idle. Moving it to the end of a 20 cm cable, outside the case, fixed most of it. Next is a small radiation shield made from stacked plant-pot saucers, which is apparently what everyone does.

← All notes