Class: I2CDevice::Bmp180

Inherits:
I2CDevice show all
Defined in:
lib/i2c/device/bmp180.rb

Overview

Implements the I2C-Device BMP085/BMP180 This code was inspired by github.com/adafruit/Adafruit_Python_BMP

Datasheet: www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf

Currently this code was tested on a Banana Pi with a BMP185 device. It should work on a Raspberry or any other Linux with I2C-Dev

Example

Using i2c-2 device (e.g. if you using a banana pi)

bmp = I2CDevice::Bmp180.new(driver: I2CDevice::Driver::I2CDev.new("/dev/i2c-2"), mode: 0)
puts "#{bmp.read_temperature / 10.0}°C"
sleep 1
puts "#{bmp.read_pressure / 100.0}hPa abs"
sleep 1
m_above_sealevel = 500 # position realtive to sealevel in m
puts "#{bmp.read_sealevel_pressure(m_above_sealevel) / 100.0}hPa rel"

Constant Summary collapse

BMP085_I2CADDR =

BMP085 default address.

0x77
BMP085_ULTRALOWPOWER =

Operating Modes

0
BMP085_STANDARD =
1
BMP085_HIGHRES =
2
BMP085_ULTRAHIGHRES =
3
BMP085_CAL_AC1 =

BMP085 Registers

0xAA
BMP085_CAL_AC2 =

R Calibration data (16 bits)

0xAC
BMP085_CAL_AC3 =

R Calibration data (16 bits)

0xAE
BMP085_CAL_AC4 =

R Calibration data (16 bits)

0xB0
BMP085_CAL_AC5 =

R Calibration data (16 bits) unsigned

0xB2
BMP085_CAL_AC6 =

R Calibration data (16 bits) unsigned

0xB4
BMP085_CAL_B1 =

R Calibration data (16 bits) unsigned

0xB6
BMP085_CAL_B2 =

R Calibration data (16 bits)

0xB8
BMP085_CAL_MB =

R Calibration data (16 bits)

0xBA
BMP085_CAL_MC =

R Calibration data (16 bits)

0xBC
BMP085_CAL_MD =

R Calibration data (16 bits)

0xBE
BMP085_CONTROL =

R Calibration data (16 bits)

0xF4
BMP085_TEMPDATA =
0xF6
BMP085_PRESSUREDATA =
0xF6
BMP085_READTEMPCMD =

Commands

0x2E
BMP085_READPRESSURECMD =
0x34

Constants inherited from I2CDevice

VERSION

Instance Attribute Summary

Attributes inherited from I2CDevice

#address

Instance Method Summary collapse

Methods inherited from I2CDevice

#i2cget, #i2cset

Constructor Details

#initialize(args = {}) ⇒ Bmp180

initialize the device and read the calibration registers

params

* args : hash defaults to {}
** :mode : one of BMP085_ULTRALOWPOWER | BMP085_STANDARD | BMP085_HIGHRES | BMP085_ULTRAHIGHRES defaults to BMP085_STANDARD see datasheet for more information
** :address : device address defaults to 0x77

57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/i2c/device/bmp180.rb', line 57

def initialize(args={})
    @mode = args.delete(:mode) || BMP085_STANDARD
    args = {
    address: BMP085_I2CADDR
    }.merge(args)

    super args

    raise "Mode must be between #{BMP085_ULTRALOWPOWER} and #{BMP085_ULTRAHIGHRES}" unless [BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, BMP085_ULTRAHIGHRES].include?(@mode)

    calibration
end

Instance Method Details

#cacl_sealevel_pressure(pressure, altitude) ⇒ Object

calculate the current pressure at sealevel from the given relative pressure and the gitven altitude

params

* altitude : curren altitude above sealevel in m
* pressure : current relative pressure in Pa

105
106
107
# File 'lib/i2c/device/bmp180.rb', line 105

def cacl_sealevel_pressure(pressure, altitude)
    return pressure.to_f / ((1.0 - altitude.to_f / 44330.0) ** 5.255)
end

#get_calObject

get the calibration values

return

array of calibration data

113
114
115
# File 'lib/i2c/device/bmp180.rb', line 113

def get_cal
    return @cal_AC1, @cal_AC2, @cal_AC3, @cal_AC4, @cal_AC5, @cal_AC6, @cal_B1, @cal_B2, @cal_MB, @cal_MC, @cal_MD
end

#read_pressureObject

read the current relative pressure in Pa


76
77
78
# File 'lib/i2c/device/bmp180.rb', line 76

def read_pressure
    return calc_real_pressure(read_raw_temperature, read_raw_pressure)
end

#read_sealevel_pressure(altitude = 0.0) ⇒ Object

calculate the current pressure at sealevel from the current relative pressure and the gitven altitude

params

* altitude : curren altitude above sealevel in m defaults to 0

95
96
97
98
# File 'lib/i2c/device/bmp180.rb', line 95

def read_sealevel_pressure(altitude = 0.0)
    pressure = read_pressure()
    return cacl_sealevel_pressure(pressure, altitude)
end

#read_temperatureObject

read the current real temperature in 0.1°C


71
72
73
# File 'lib/i2c/device/bmp180.rb', line 71

def read_temperature
    return calc_real_temperature(read_raw_temperature)
end

#read_temperature_and_pressureObject

Read current temperature and realtive pressure

return

* temperature in 0.1°C, pressure in Pa

84
85
86
87
88
89
# File 'lib/i2c/device/bmp180.rb', line 84

def read_temperature_and_pressure
    ut = read_raw_temperature
    up = read_raw_pressure

    return calc_real_temperature(ut), calc_real_pressure(ut, up)
end