2019-06-30 12:34:13 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
2020-05-03 12:04:08 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2019-06-30 12:34:13 +00:00
|
|
|
|
|
|
|
"github.com/d2r2/go-bsbmp"
|
|
|
|
"github.com/d2r2/go-i2c"
|
|
|
|
"github.com/d2r2/go-logger"
|
2020-05-03 12:04:08 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
|
2020-06-10 19:13:05 +00:00
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/internal/format"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
2019-06-30 12:34:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BME280 is a sensor to measure humidity and temperature.
|
|
|
|
type BME280 struct {
|
|
|
|
*types.Sensor
|
2020-05-03 12:04:08 +00:00
|
|
|
mutex *sync.Mutex
|
2019-06-30 12:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read measured values
|
2020-05-03 12:04:08 +00:00
|
|
|
func (bme280 *BME280) Read() ([]*types.MeasuredValue, error) {
|
|
|
|
|
|
|
|
// Lock multiple access
|
|
|
|
bme280.mutex.Lock()
|
|
|
|
defer bme280.mutex.Unlock()
|
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
|
2020-05-03 12:04:08 +00:00
|
|
|
i2c, err := i2c.NewI2C(*bme280.I2CAddress, *bme280.I2CBus)
|
2019-06-30 12:34:13 +00:00
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return nil, err
|
2019-06-30 12:34:13 +00:00
|
|
|
}
|
|
|
|
defer i2c.Close()
|
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
// Reduce loglevel
|
|
|
|
for _, pkg := range []string{"bsbmp", "i2c"} {
|
|
|
|
err = logger.ChangePackageLogLevel(pkg, logger.InfoLevel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to change package log level: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 12:34:13 +00:00
|
|
|
|
|
|
|
sensor, err := bsbmp.NewBMP(bsbmp.BME280, i2c)
|
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return nil, err
|
2019-06-30 12:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
temperatureValue, err := sensor.ReadTemperatureC(bsbmp.ACCURACY_STANDARD)
|
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return nil, err
|
2019-06-30 12:34:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 13:06:23 +00:00
|
|
|
pressureValue, err := sensor.ReadPressurePa(bsbmp.ACCURACY_STANDARD)
|
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return nil, err
|
2019-06-30 13:06:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 12:34:13 +00:00
|
|
|
_, humidityValue, err := sensor.ReadHumidityRH(bsbmp.ACCURACY_STANDARD)
|
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return nil, err
|
2019-06-30 12:34:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues := []*types.MeasuredValue{
|
2020-05-03 12:04:08 +00:00
|
|
|
{
|
2019-07-02 20:33:01 +00:00
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(humidityValue),
|
2020-09-21 17:36:42 +00:00
|
|
|
ValueType: types.Humidity,
|
2020-05-31 22:52:54 +00:00
|
|
|
Date: format.FormatedTime(),
|
2020-05-03 12:04:08 +00:00
|
|
|
SensorID: bme280.ID,
|
2019-06-30 12:34:13 +00:00
|
|
|
},
|
2020-05-03 12:04:08 +00:00
|
|
|
{
|
2019-07-02 20:33:01 +00:00
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(pressureValue),
|
2020-09-21 17:36:42 +00:00
|
|
|
ValueType: types.Pressure,
|
2020-05-31 22:52:54 +00:00
|
|
|
Date: format.FormatedTime(),
|
2020-05-03 12:04:08 +00:00
|
|
|
SensorID: bme280.ID,
|
2019-06-30 13:06:23 +00:00
|
|
|
},
|
2020-05-03 12:04:08 +00:00
|
|
|
{
|
2019-07-02 20:33:01 +00:00
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(temperatureValue),
|
2020-09-21 17:36:42 +00:00
|
|
|
ValueType: types.Temperature,
|
2020-05-31 22:52:54 +00:00
|
|
|
Date: format.FormatedTime(),
|
2020-05-03 12:04:08 +00:00
|
|
|
SensorID: bme280.ID,
|
2019-06-30 12:34:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
|
|
|
}
|