Using the Adafruit BMP Python Library (Updated)

Using the BMP sensor with a Raspberry Pi or Beaglebone Black is easy with the Adafruit Python BMP sensor library. First make sure your device is powered on and has access to the internet (through a wired or wireless connection). Then connect to your device in a terminal and navigate to a directory where you want to download the library (like /home/pi on a Raspberry Pi or /root on a Beaglebone Black). Finally execute the following commands to download dependencies and install the library:
sudo apt-get update
sudo apt-get install git build-essential python-dev python-smbus
git clone https://github.com/adafruit/Adafruit_Python_BMP.git
cd Adafruit_Python_BMP
sudo python setup.py install
If you already have git or python-smbus installed you can ignore the message about the package already being installed.

Once the library is installed it will be accessible to any Python script on your device. You can see a few example scripts included in the library source's examples folder. Try running the simpletest.py example which grabs a single reading from the BMP sensor and displays it by executing:
cd examples
sudo python simpletest.py
If you receive an error message, carefully check that the library was installed correctly in the previous steps and try again. Note that the command needs to be run as root with sudo so that it can access the hardware's I2C bus.

After running the script you should see an output such as:
Temp = 20.20 *C
Pressure = 101667.00 Pa
Altitude = -28.27 m
Sealevel Pressure = 101665.00 Pa

Open the simpletest.py code in a text editor to see how to use the library to read the BMP sensor. First the library is imported with this command:

import Adafruit_BMP.BMP085 as BMP085
Next a BMP085 sensor instance is created with this command:
# Default constructor will pick a default I2C bus.
#
# For the Raspberry Pi this means you should hook up to the only exposed I2C bus
# from the main GPIO header and the library will figure out the bus number based
# on the Pi's revision.
#
# For the Beaglebone Black the library will assume bus 1 by default, which is
# exposed with SCL = P9_19 and SDA = P9_20.
sensor = BMP085.BMP085()

# Optionally you can override the bus number:
#sensor = BMP085.BMP085(busnum=2)

# You can also optionally change the BMP085 mode to one of BMP085_ULTRALOWPOWER, 
# BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES.  See the BMP085
# datasheet for more details on the meanings of each mode (accuracy and power
# consumption are primarily the differences).  The default mode is STANDARD.
#sensor = BMP085.BMP085(mode=BMP085.BMP085_ULTRAHIGHRES)

You can see from the comments there are a few ways to create the sensor instance. By default if you pass no parameters the library will try to find the right I2C bus for your device. For a Raspberry Pi the library will detect the revision number and use the appropriate bus (0 or 1). For a Beaglebone Black there are multiple I2C buses so the library defaults to bus 1, which is exposed with pin P9_19 as SCL clock and P9_20 as SDA data. You can explicitly set the bus number by passing it in the busnum parameter.

Note if you're using a BeagleBone Black with the Ubuntu operating system you might need to change busnum to 2 to use the P9_19 & P9_20 pin I2C connection.  Just change the line to look like: sensor = BMP.BMP085(busnum=2)

The library will also choose by default to use the BMP sensor's standard operation mode. You can override this by passing a mode parameter with an explicit mode value--check the BMP datasheet for more information on its modes.

Once the BMP sensor instance is created, you can read its values by calling the read_temperature, read_pressure, read_altitude, and read_sealevel_pressure functions like below:

print 'Temp = {0:0.2f} *C'.format(sensor.read_temperature())
print 'Pressure = {0:0.2f} Pa'.format(sensor.read_pressure())
print 'Altitude = {0:0.2f} m'.format(sensor.read_altitude())
print 'Sealevel Pressure = {0:0.2f} Pa'.format(sensor.read_sealevel_pressure())
That's all you need to do to read BMP sensor values using the Adafruit Python BMP library!

For another example of using the BMP library, check out the google_spreadsheet.py example. This code is similar to the DHT sensor Google Docs spreadsheet logging code, but is modified to use the BMP sensor and write the temperature, pressure, and altitude to a Google Docs spreadsheet. Check out the page on configuring Google Docs to see more details on how to create the spreadsheet and configure the username, password, and spreadsheet name.
Last updated on 2015-08-16 at 07.02.10 PM Published on 2012-08-14 at 01.07.18 PM