diff --git a/drivers/i2c/bme280_driver.go b/drivers/i2c/bme280_driver.go index c66280fa..8345eea3 100644 --- a/drivers/i2c/bme280_driver.go +++ b/drivers/i2c/bme280_driver.go @@ -20,7 +20,12 @@ type bmeHumidityCalibrationCoefficients struct { h6 int8 } -// BME280Driver is a driver for the BME280 temperature/humidity sensor +// BME280Driver is a driver for the BME280 temperature/humidity sensor. +// It implements all of the same functions as the BMP280Driver, but also +// adds the Humidity() function by reading the BME280's humidity sensor. +// For details on the BMP280Driver please see: +// https://godoc.org/gobot.io/x/gobot/drivers/i2c#BMP280Driver +// type BME280Driver struct { *BMP280Driver hc *bmeHumidityCalibrationCoefficients diff --git a/drivers/i2c/bmp280_driver.go b/drivers/i2c/bmp280_driver.go index ef1855e2..ffe6190d 100644 --- a/drivers/i2c/bmp280_driver.go +++ b/drivers/i2c/bmp280_driver.go @@ -3,6 +3,7 @@ package i2c import ( "bytes" "encoding/binary" + "math" "gobot.io/x/gobot" ) @@ -12,10 +13,10 @@ const ( bmp280RegisterConfig = 0xf5 bmp280RegisterPressureData = 0xf7 bmp280RegisterTempData = 0xfa + bmp280RegisterCalib00 = 0x88 + bmp280SeaLevelPressure = 1013.25 ) -const bmp280RegisterCalib00 = 0x88 - type bmp280CalibrationCoefficients struct { t1 uint16 t2 int16 @@ -125,6 +126,18 @@ func (d *BMP280Driver) Pressure() (press float32, err error) { return d.calculatePress(rawP, tFine), nil } +// Altitude returns the current altitude in meters based on the +// current barometric pressure and estimated pressure at sea level. +// Calculation is based on code from Adafruit BME280 library +// https://github.com/adafruit/Adafruit_BME280_Library +func (d *BMP280Driver) Altitude() (alt float32, err error) { + atmP, _ := d.Pressure() + atmP /= 100.0 + alt = float32(44330.0 * (1.0 - math.Pow(float64(atmP/bmp280SeaLevelPressure), 0.1903))) + + return +} + // initialization reads the calibration coefficients. func (d *BMP280Driver) initialization() (err error) { // TODO: set sleep mode here... diff --git a/examples/firmata_bme280.go b/examples/firmata_bme280.go index 662788b8..1a39e802 100644 --- a/examples/firmata_bme280.go +++ b/examples/firmata_bme280.go @@ -26,6 +26,9 @@ func main() { p, _ := bme280.Pressure() fmt.Println("Pressure", p) + a, _ := bme280.Altitude() + fmt.Println("Altitude", a) + h, _ := bme280.Humidity() fmt.Println("Humidity", h) }) diff --git a/examples/firmata_bmp180.go b/examples/firmata_bmp180.go index f0118112..2aed8014 100644 --- a/examples/firmata_bmp180.go +++ b/examples/firmata_bmp180.go @@ -20,7 +20,6 @@ func main() { work := func() { gobot.Every(1*time.Second, func() { - //fmt.Println("Pressure", mpl115a2.Pressure()) t, _ := bmp180.Temperature() fmt.Println("Temperature", t) }) diff --git a/examples/firmata_bmp280.go b/examples/firmata_bmp280.go new file mode 100644 index 00000000..ff72fccc --- /dev/null +++ b/examples/firmata_bmp280.go @@ -0,0 +1,41 @@ +// +build example +// +// Do not build by default. + +package main + +import ( + "fmt" + "os" + "time" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/drivers/i2c" + "gobot.io/x/gobot/platforms/firmata" +) + +func main() { + firmataAdaptor := firmata.NewAdaptor(os.Args[1]) + bmp280 := i2c.NewBMP280Driver(firmataAdaptor) + + work := func() { + gobot.Every(1*time.Second, func() { + t, _ := bmp280.Temperature() + fmt.Println("Temperature", t) + + p, _ := bmp280.Pressure() + fmt.Println("Pressure", p) + + a, _ := bmp280.Altitude() + fmt.Println("Altitude", a) + }) + } + + robot := gobot.NewRobot("bmp280bot", + []gobot.Connection{firmataAdaptor}, + []gobot.Device{bmp280}, + work, + ) + + robot.Start() +}