PKGBUILD/pkg/sensor/dht11.go

77 lines
1.5 KiB
Go
Raw Normal View History

2019-02-22 12:08:58 +00:00
package sensor
import (
2019-03-03 18:13:37 +00:00
"fmt"
"time"
2019-02-22 12:08:58 +00:00
2019-02-24 21:46:36 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
2019-03-03 18:13:37 +00:00
"github.com/MichaelS11/go-dht"
uuid "github.com/satori/go.uuid"
2019-02-22 12:08:58 +00:00
)
type DHT11Sensor struct {
*types.Sensor
}
2019-02-24 21:46:36 +00:00
func (s *DHT11Sensor) ReadHumidity() (*types.Humidity, error) {
2019-03-03 18:13:37 +00:00
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
}
gpio, err := types.GPIOToString(*s.GPIONumber)
if err != nil {
return nil, err
}
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
if err != nil {
return nil, fmt.Errorf("NewDHT error: %v", err)
}
2019-03-04 09:45:19 +00:00
humidityValue, _, err := dht.Read()
2019-03-03 18:13:37 +00:00
if err != nil {
return nil, fmt.Errorf("Read error: %v", err)
}
humidity := &types.Humidity{
HumidityID: uuid.NewV4().String(),
HumidityValue: humidityValue,
HumidityDate: time.Now(),
SensorID: s.SensorID,
}
return humidity, nil
2019-02-24 21:46:36 +00:00
}
func (s *DHT11Sensor) ReadTemperature() (*types.Temperature, error) {
2019-03-03 18:13:37 +00:00
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
}
gpio, err := types.GPIOToString(*s.GPIONumber)
if err != nil {
return nil, err
}
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
if err != nil {
return nil, fmt.Errorf("NewDHT error: %v", err)
}
2019-03-04 09:45:19 +00:00
_, temperatureValue, err := dht.Read()
2019-03-03 18:13:37 +00:00
if err != nil {
return nil, fmt.Errorf("Read error: %v", err)
}
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureDate: time.Now(),
SensorID: s.SensorID,
}
return temperature, nil
2019-02-22 12:08:58 +00:00
}