2019-06-30 12:34:13 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-01-10 18:42:19 +00:00
|
|
|
"time"
|
2019-06-30 12:34:13 +00:00
|
|
|
|
|
|
|
"github.com/d2r2/go-bsbmp"
|
|
|
|
"github.com/d2r2/go-i2c"
|
|
|
|
"github.com/d2r2/go-logger"
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/internal/format"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
2019-06-30 12:34:13 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BME280 is a sensor to measure humidity and temperature.
|
|
|
|
type BME280 struct {
|
|
|
|
*types.Sensor
|
|
|
|
}
|
|
|
|
|
2020-01-10 21:03:49 +00:00
|
|
|
// GetID returns the sensor id
|
|
|
|
func (s *BME280) GetID() string {
|
|
|
|
return s.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTicker returns a new ticker, which tick every when the sensor should be read
|
|
|
|
func (s *BME280) GetTicker() *time.Ticker {
|
|
|
|
duration, err := time.ParseDuration(s.TickDuration)
|
|
|
|
if err != nil {
|
|
|
|
duration = time.Minute
|
|
|
|
}
|
|
|
|
return time.NewTicker(duration)
|
2019-06-30 12:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read measured values
|
2019-07-02 20:33:01 +00:00
|
|
|
func (s *BME280) Read() ([]*types.MeasuredValue, error) {
|
2019-06-30 12:34:13 +00:00
|
|
|
|
|
|
|
// Create new connection to i2c-bus on 1 line with address 0x76.
|
|
|
|
// Use i2cdetect utility to find device address over the i2c-bus
|
|
|
|
i2c, err := i2c.NewI2C(*s.I2CAddress, *s.I2CBus)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer i2c.Close()
|
|
|
|
|
|
|
|
logger.ChangePackageLogLevel("i2c", logger.InfoLevel)
|
|
|
|
|
|
|
|
sensor, err := bsbmp.NewBMP(bsbmp.BME280, i2c)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.ChangePackageLogLevel("bsbmp", logger.InfoLevel)
|
|
|
|
|
|
|
|
temperatureValue, err := sensor.ReadTemperatureC(bsbmp.ACCURACY_STANDARD)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-30 13:06:23 +00:00
|
|
|
pressureValue, err := sensor.ReadPressurePa(bsbmp.ACCURACY_STANDARD)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-30 13:22:02 +00:00
|
|
|
// pressureValueRound := math.Round(float64(pressureValue)/10*0.25) * 10 / 0.25
|
2019-06-30 12:34:13 +00:00
|
|
|
|
|
|
|
_, humidityValue, err := sensor.ReadHumidityRH(bsbmp.ACCURACY_STANDARD)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues := []*types.MeasuredValue{
|
|
|
|
&types.MeasuredValue{
|
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(humidityValue),
|
|
|
|
ValueType: types.MeasuredValueTypeHumidity,
|
2019-08-20 19:37:45 +00:00
|
|
|
FromDate: format.FormatedTime(),
|
|
|
|
TillDate: format.FormatedTime(),
|
2020-01-10 21:03:49 +00:00
|
|
|
SensorID: s.ID,
|
2019-06-30 12:34:13 +00:00
|
|
|
},
|
2019-07-02 20:33:01 +00:00
|
|
|
&types.MeasuredValue{
|
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(pressureValue),
|
|
|
|
ValueType: types.MeasuredValueTypePressure,
|
2019-08-20 19:37:45 +00:00
|
|
|
FromDate: format.FormatedTime(),
|
|
|
|
TillDate: format.FormatedTime(),
|
2020-01-10 21:03:49 +00:00
|
|
|
SensorID: s.ID,
|
2019-06-30 13:06:23 +00:00
|
|
|
},
|
2019-07-02 20:33:01 +00:00
|
|
|
&types.MeasuredValue{
|
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(temperatureValue),
|
|
|
|
ValueType: types.MeasuredValueTypeTemperature,
|
2019-08-20 19:37:45 +00:00
|
|
|
FromDate: format.FormatedTime(),
|
|
|
|
TillDate: format.FormatedTime(),
|
2020-01-10 21:03:49 +00:00
|
|
|
SensorID: s.ID,
|
2019-06-30 12:34:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
|
|
|
}
|