feat: implementation of dht11 sensor

This commit is contained in:
Markus Pesch 2019-03-03 19:13:37 +01:00
parent 48b18b61ec
commit 94dcfee1ec
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
8 changed files with 292 additions and 31 deletions

View File

@ -31,13 +31,19 @@ var addSensorCmd = &cobra.Command{
log.Fatalln(err)
}
// determine gpio port
gpio, err := types.StringToGPIO(args[2])
if err != nil {
log.Fatalln(err)
}
// create new sensor struct
sensor := &types.Sensor{
SensorName: args[0],
SensorModel: sensorModel,
SensorLocation: location,
SensorEnabled: enabled,
GPIONumber: &args[2],
GPIONumber: &gpio,
WireID: &wireID,
}

View File

@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
)
var follow, push bool
var logs bool
var readTemperatureCmd = &cobra.Command{
Use: "read",
@ -39,10 +39,18 @@ var readTemperatureCmd = &cobra.Command{
// print temperatures on stdout
cli.PrintTemperatures(temperatures, fc, os.Stdout)
if logs {
err = fc.FileLogger.LogTemperatures(temperatures)
if err != nil {
log.Fatalln(err)
}
}
},
}
func init() {
temperatureCmd.AddCommand(readTemperatureCmd)
readTemperatureCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow output")
// readTemperatureCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow output")
readTemperatureCmd.Flags().BoolVarP(&logs, "logs", "l", true, "Log temperature")
}

6
go.mod
View File

@ -3,6 +3,11 @@ module git.cryptic.systems/fh-trier/go-flucky
go 1.12
require (
github.com/MichaelS11/go-dht v0.0.0-20181004212404-be44b9ee7fec
github.com/d2r2/go-dht v0.0.0-20181222061613-42fe873fbabb // indirect
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e // indirect
github.com/d2r2/go-shell v0.0.0-20181221082743-874cb5c847b3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/satori/go.uuid v1.2.0
@ -10,4 +15,5 @@ require (
github.com/spf13/pflag v1.0.3 // indirect
github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
periph.io/x/periph v3.4.0+incompatible // indirect
)

12
go.sum
View File

@ -1,3 +1,13 @@
github.com/MichaelS11/go-dht v0.0.0-20181004212404-be44b9ee7fec h1:xMo0OOokExQqY/MRsTz6azUwy0udvlqhkJwQ5jZ6eN4=
github.com/MichaelS11/go-dht v0.0.0-20181004212404-be44b9ee7fec/go.mod h1:NTx2rUi8kfs8Qk9Fotoyf/3lQnKBdc2mhyZqO4AhVLI=
github.com/d2r2/go-dht v0.0.0-20181222061613-42fe873fbabb h1:kyXaht4l/GxAWx0ubvha2QyVrtOgIx071o03tF3q60o=
github.com/d2r2/go-dht v0.0.0-20181222061613-42fe873fbabb/go.mod h1:AzSqP4S4/6pINOKg3VC79WC7YY3zskQcrXMFzphCry0=
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e h1:ZG3JBA6rPRl0xxQ+nNSfO7tor8w+CNCTs05DNJQYbLM=
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e/go.mod h1:oA+9PUt8F1aKJ6o4YU1T120i7sgo1T6/1LWEEBy0BSs=
github.com/d2r2/go-shell v0.0.0-20181221082743-874cb5c847b3 h1:A30Vp6lwq9bHnh4D7XRHZYOpuJq2/bbMs+B94Y8NeCc=
github.com/d2r2/go-shell v0.0.0-20181221082743-874cb5c847b3/go.mod h1:FdrNob+jQ3UkEpNVeZFVn8mW86Aa2wa6U5z0vGQOEXQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@ -15,3 +25,5 @@ github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624 h1:bePzgtpuLSl+F9aacw
github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624/go.mod h1:MqFju5qeLDFh+S9PqxYT7TEla8xeW7bgGr/69q3oki0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
periph.io/x/periph v3.4.0+incompatible h1:5gzxE4ryPq52cdqSw0mErR6pyJK8cBF2qdUAcOWh0bo=
periph.io/x/periph v3.4.0+incompatible/go.mod h1:EWr+FCIU2dBWz5/wSWeiIUJTriYv9v2j2ENBmgYyy7Y=

View File

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

149
pkg/types/gpio.go Normal file
View File

@ -0,0 +1,149 @@
package types
import (
"fmt"
)
type GPIO string
const (
GPIO02 GPIO = "GPIO02"
GPIO03 = "GPIO03"
GPIO04 = "GPIO04"
GPIO05 = "GPIO05"
GPIO06 = "GPIO06"
GPIO07 = "GPIO07"
GPIO08 = "GPIO08"
GPIO10 = "GPIO10"
GPIO11 = "GPIO11"
GPIO12 = "GPIO12"
GPIO13 = "GPIO13"
GPIO14 = "GPIO14"
GPIO15 = "GPIO15"
GPIO16 = "GPIO16"
GPIO17 = "GPIO17"
GPIO18 = "GPIO18"
GPIO19 = "GPIO19"
GPIO20 = "GPIO20"
GPIO21 = "GPIO21"
GPIO22 = "GPIO22"
GPIO23 = "GPIO23"
GPIO24 = "GPIO24"
GPIO25 = "GPIO25"
GPIO26 = "GPIO26"
GPIO27 = "GPIO27"
)
func GPIOToString(gpio GPIO) (string, error) {
switch gpio {
case GPIO02:
return "GPIO02", nil
case GPIO03:
return "GPIO03", nil
case GPIO04:
return "GPIO04", nil
case GPIO05:
return "GPIO05", nil
case GPIO06:
return "GPIO06", nil
case GPIO07:
return "GPIO07", nil
case GPIO08:
return "GPIO08", nil
case GPIO10:
return "GPIO10", nil
case GPIO11:
return "GPIO11", nil
case GPIO12:
return "GPIO12", nil
case GPIO13:
return "GPIO13", nil
case GPIO14:
return "GPIO14", nil
case GPIO15:
return "GPIO15", nil
case GPIO16:
return "GPIO16", nil
case GPIO17:
return "GPIO17", nil
case GPIO18:
return "GPIO18", nil
case GPIO19:
return "GPIO19", nil
case GPIO20:
return "GPIO20", nil
case GPIO21:
return "GPIO21", nil
case GPIO22:
return "GPIO22", nil
case GPIO23:
return "GPIO23", nil
case GPIO24:
return "GPIO24", nil
case GPIO25:
return "GPIO25", nil
case GPIO26:
return "GPIO26", nil
case GPIO27:
return "GPIO27", nil
default:
return "", fmt.Errorf("Can not determine gpio %v", gpio)
}
}
func StringToGPIO(gpio string) (GPIO, error) {
switch gpio {
case "GPIO02":
return GPIO02, nil
case "GPIO03":
return GPIO03, nil
case "GPIO04":
return GPIO04, nil
case "GPIO05":
return GPIO05, nil
case "GPIO06":
return GPIO06, nil
case "GPIO07":
return GPIO07, nil
case "GPIO08":
return GPIO08, nil
case "GPIO10":
return GPIO10, nil
case "GPIO11":
return GPIO11, nil
case "GPIO12":
return GPIO12, nil
case "GPIO13":
return GPIO13, nil
case "GPIO14":
return GPIO14, nil
case "GPIO15":
return GPIO15, nil
case "GPIO16":
return GPIO16, nil
case "GPIO17":
return GPIO17, nil
case "GPIO18":
return GPIO18, nil
case "GPIO19":
return GPIO19, nil
case "GPIO20":
return GPIO20, nil
case "GPIO21":
return GPIO21, nil
case "GPIO22":
return GPIO22, nil
case "GPIO23":
return GPIO23, nil
case "GPIO24":
return GPIO24, nil
case "GPIO25":
return GPIO25, nil
case "GPIO26":
return GPIO26, nil
case "GPIO27":
return GPIO27, nil
default:
return "", fmt.Errorf("Can not determine gpio %v", gpio)
}
}

View File

@ -1,7 +1,9 @@
package types
import (
"encoding/json"
"fmt"
"io"
"time"
)
@ -11,7 +13,7 @@ type Sensor struct {
SensorName string `json:"sensor_name"`
SensorLocation string `json:"sensor_location"`
WireID *string `json:"wire_id"`
GPIONumber *string `json:"gpio_number"`
GPIONumber *GPIO `json:"gpio_number"`
SensorModel SensorModel `json:"sensor_model"`
SensorEnabled bool `json:"sensor_enabled"`
SensorLastContact time.Time `json:"sensor_last_contact"`
@ -19,6 +21,26 @@ type Sensor struct {
CreationDate time.Time `json:"creation_date"`
}
// JSONDecoder decodes a json into a sensor
func (s *Sensor) JSONDecoder(r io.Reader) error {
decoder := json.NewDecoder(r)
err := decoder.Decode(s)
if err != nil {
return fmt.Errorf("Can not decode sensor from json: %v", err)
}
return nil
}
func (s *Sensor) JSONEncoder(w io.Writer) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
err := encoder.Encode(s)
if err != nil {
return fmt.Errorf("Can not encode sensor to json: %v", err)
}
return nil
}
func (s *Sensor) Name() string {
if s.SensorName != "" {
return s.SensorName
@ -27,25 +49,3 @@ func (s *Sensor) Name() string {
}
return s.SensorID
}
type SensorModel string
const (
DHT11 SensorModel = "DHT11"
DHT22 = "DHT22"
DS18B20 = "DS18B20"
)
// SelectSensorModel converts a string into a constant
func SelectSensorModel(model string) (SensorModel, error) {
switch model {
case "DHT11":
return DHT11, nil
case "DHT22":
return DHT22, nil
case "DS18B20":
return DS18B20, nil
default:
return "", fmt.Errorf("Sensor Model %v currently not supported", model)
}
}

25
pkg/types/sensor_model.go Normal file
View File

@ -0,0 +1,25 @@
package types
import "fmt"
type SensorModel string
const (
DHT11 SensorModel = "DHT11"
DHT22 = "DHT22"
DS18B20 = "DS18B20"
)
// SelectSensorModel converts a string into a constant
func SelectSensorModel(model string) (SensorModel, error) {
switch model {
case "DHT11":
return DHT11, nil
case "DHT22":
return DHT22, nil
case "DS18B20":
return DS18B20, nil
default:
return "", fmt.Errorf("Sensor Model %v currently not supported", model)
}
}