How to Build a Raspberry Pi Weather Station

Building a Raspberry Pi Weather Station is a popular and fun project. It entails some wiring/soldering, python programming, scraping the Adafruit website for software and drivers, and finally some engineering to get a Raspberry to live outside in your backyard with access to your network. Depending on how fancy you want to get, this weather station can log to your MySQL database, display on a website, or just output raw ASCII to a screen.

Once you’ve got your probes wired up, you’ll need to install some software. Start with i2c-tools. It has a great hardware test called i2cdetect.

sudo apt-get install -y i2c-tools
sudo i2cdetect -y 1

Before I get ahead of myself I like to check to see that my probes are wired up properly and responding on the I2C bus, so I run i2cdetect noninteractively (-y) and just scan the first page (because it’s a required argument and Raspberries only have one I2C page.)

root@weather:/home/pi# i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- 5a -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Yay! Soldering was good! I can see all three probes on my I2C bus!

Next, install the tools to get the ADAfruit drivers and other tools installed. Just do a search on Adafruit for the model number of the probes that you’re using and follow the instructions for that probe. I think it’s worth the extra effort to find the newer python3 drivers and software. python2 is going away quickly, so I like to future-proof with strictly python3 everything:

apt-get install -y build-essential python-pip python-dev python-smbus git
git clone http://github.com/adafruit/Adafruit_Python_GPIO.git
cd Adafruit_Python_GPIO
sudo python setup.py install
sudo pip3 install adafruit-circuitpython-ccs811
sudo pip3 install adafruit-circuitpython-si7021
sudo pip3 install adafruit-circuitpython-mpl115a2

Here’s my python script so far:

!/usr/bin/python3
import sys
import numpy as np
import os
import time
import board
import busio
import adafruit_si7021
import adafruit_mpl3115a2
import adafruit_ccs811

#a couple of formulas for Relative Humidity calculations
def dewpoint(C,rh):
   Td = (237.7 * gamma(C,rh)) / (17.271 - gamma(C,rh))
   return Td

def gamma(C,rh):
   g = (17.271 * C / (237.7 + C)) + np.log(rh/100.0)
   return g
   
#instanciate the probes reusing the i2c definition
i2c = busio.I2C(board.SCL, board.SDA)
si7021 = adafruit_si7021.SI7021(i2c)
mpl3115a2 = adafruit_mpl3115a2.MPL3115A2(i2c)
ccs811 = adafruit_ccs811.CCS811(i2c)

#wait for the 811 heater to come to temp
while not ccs811.data_ready:
pass

#open a file to write values
tempfile = open('/tmp/samples.txt', 'w')
count =0

#calculate the values
C = si7021.temperature
F = (C * 9/5) + 32
rh = si7021.relative_humidity
inHg = mpl3115a2.pressure / 3340
dew = (dewpoint(C,rh) * 9/5) + 32

#write to the temp file every 3 seconds to keep the probes hot
while True:
   tempfile.write(str(F))
   tempfile.write("|")
   tempfile.write(str(rh))
   tempfile.write("|")
   tempfile.write(str(inHg))
   tempfile.write("|")
   tempfile.write(str(ccs811.eco2))
   tempfile.write("|")
   tempfile.write(str(ccs811.tvoc))
   tempfile.write("|")
   tempfile.write(str(dew))
   tempfile.write("\n")
   count++
   time.sleep(3)

tempfile.close()

Currently, I’m writing a routine to grab 5 minutes worth of samples, average them, and write the averages to a database. Check in soon for progress updates!!

Author: John

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.