Weather - pressure and temperature

Keeping track of air pressure is essential at sea. From early on recording barographs were in widespread use. Today instruments that can monitor and display air pressure, current and historically can be bought. However, there is no reason that the Pi can not perform this task. Air pressure and temperature sensors are readily avaiable at remarkably low prices.  I have used a chip called  BMP 180  from Bosch. Documentation can be found here. The board I use is bought from China, show on the picture. 

This board uses the I2C bus to communicate with the Pi board. The I2C is a bus which allow for a range of different units to connect to the same bus. 

The very simple wiring  using the I2C bus make it very suitable for caes where a range of different sensors are to be connected and used with the Pi.
Only 4 wires, power (+ GND) and two wires for I2C, one signal and one clock signal.  The Pi uses two dedicated pins for I2C, they are maked SDA and SLC and are located close to the 3.3V supply at pin 1, SDA is pin 2 and SCL is pin 3.  Verify this before you turn on the power. 

Software is equally simple, drivers and modules for Python are readly available on the net. 
A small Python3 program to collect and print the temperature and pressure can look like the one below, it uses the  BMP085 module, but they are compatible and it works without problems. The Python3 code is:

import Adafruit_BMP.BMP085 as BMP085
sensor = BMP085.BMP085()
print('Temp = {0:0.2f} *C'.format(sensor.read_temperature()))
print('Pressure = {0:0.2f} hPa'.format(sensor.read_pressure()/100))

A Python barograph  program need to log a timestamp and a pressure measurement to a log file befor we can proceed to plot any data. 

import Adafruit_BMP.BMP085 as BMP085
import time
import datetime

sensor = BMP085.BMP085()
cal=1400 # Add 14 hPa

bufsize = 1
logfile = open("logg.dta", 'a', bufsize)

while True:
logfile.write(str(datetime.datetime.now())+','+str((sensor.read_pressure()+cal)/100)+"\n")
time.sleep(1800)

I take a sample every 30 mins which should be enough to keep track of even rapid changes in air pressure.  The log file wil contain a time stamp and a pressure readying, separated by a comma to make parsing easier as the time stamp contain spaces. A line of the log file look like this:
2017-01-01 21:12:12.649517,1010.55

Next is to accumulate some hours of data and write a little Pyton code that can display historical data.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as  dt
from datetime  import datetime

time_format = '%Y-%m-%d %H:%M:%S.%f'
minp=980
maxp=1020
plt.ion()
plt.close('all')
fig, ax = plt.subplots(1)
hfmt = dt.DateFormatter('%d:%H')
ax.xaxis.set_major_formatter(hfmt)
plt.xlabel('Date@hours') # Not very elegant, but there is not room for MMM-DD-HH, an alternative is HH before present.
plt.ylabel('Pressure [hPa]')
while True:
    pr=np.genfromtxt('logg.dta', dtype=('str'), delimiter=',')
    x=pr[-48*2:,0] # Data is logged every half hour.
    y=pr[-48*2:,1] # We display the last 48 hours.
    time = [datetime.strptime(i, time_format) for i in x]    
    p0=y[-1].astype(float)
    p1=y[-2].astype(float)
    if minp>y.astype(float).min():
        minp=round(y.astype(float).min()-5,-1)
    if maxp<y.astype(float).max():
        maxp=round(y.astype(float).max()+5,-1)
    plt.ylim(minp,maxp)
    grad=(p0-p1) * 3600/(time[-1].timestamp()-time[-2].timestamp())
    title='Air pressure trend : '+format(grad,'.2f')+' hPa/h'
    plt.title(title)
plt.xlim(time[0],time[95])  # Rescale to only display the last 48 hours. 
    l=plt.plot_date(time,y,color='red'
    plt.draw() 
    plt.pause(300) 
    l.pop(0).remove()
 
An axample of a barographic plot is given below, this is 48 hours of pressure data: 
 





 
Comments