i2c: adds Altitude() function to BMP280/BME280
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
671ead7705
commit
821fbf272e
|
@ -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
|
||||
|
|
|
@ -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...
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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()
|
||||
}
|
Loading…
Reference in New Issue