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
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
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
# 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())
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.
Page last edited March 08, 2024
Text editor powered by tinymce.