feat: implementation of dht22 sensor

This commit is contained in:
Markus Pesch 2019-03-04 11:05:02 +01:00
parent 4bb8df5621
commit 4bae510120
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982

View File

@ -1,9 +1,12 @@
package sensor package sensor
import ( import (
"log" "fmt"
"time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types" "git.cryptic.systems/fh-trier/go-flucky/pkg/types"
"github.com/MichaelS11/go-dht"
uuid "github.com/satori/go.uuid"
) )
type DHT22Sensor struct { type DHT22Sensor struct {
@ -11,11 +14,63 @@ type DHT22Sensor struct {
} }
func (s *DHT22Sensor) ReadHumidity() (*types.Humidity, error) { func (s *DHT22Sensor) ReadHumidity() (*types.Humidity, error) {
log.Println("DHT11 Read Method not yet implemented") err := dht.HostInit()
return nil, nil 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)
}
humidityValue, _, err := dht.Read()
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
} }
func (s *DHT22Sensor) ReadTemperature() (*types.Temperature, error) { func (s *DHT22Sensor) ReadTemperature() (*types.Temperature, error) {
log.Println("DHT22 Read Method not yet implemented") err := dht.HostInit()
return nil, nil 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)
}
_, temperatureValue, err := dht.Read()
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
} }