Skip to content

Commit

Permalink
i2c: adds Altitude() function to BMP280/BME280
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 1, 2017
1 parent 671ead7 commit 821fbf2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
7 changes: 6 additions & 1 deletion drivers/i2c/bme280_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions drivers/i2c/bmp280_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package i2c
import (
"bytes"
"encoding/binary"
"math"

"gobot.io/x/gobot"
)
Expand All @@ -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
Expand Down Expand Up @@ -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...
Expand Down
3 changes: 3 additions & 0 deletions examples/firmata_bme280.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
1 change: 0 additions & 1 deletion examples/firmata_bmp180.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
41 changes: 41 additions & 0 deletions examples/firmata_bmp280.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 821fbf2

Please sign in to comment.