-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (66 loc) · 2.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"time"
"github.com/yryz/ds18b20"
"github.com/spf13/viper"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
// CtoF converts celsius to fahrenheit
func CtoF(c float64) float64 {
return (c * 1.8) + 32
}
func main() {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
viper.SetDefault("MQTT.port", "1883")
// Connect to the MQTT server
opts := mqtt.NewClientOptions().AddBroker("tcp://" + viper.GetString("MQTT.broker") + ":" + viper.GetString("MQTT.port"))
opts.SetClientID(viper.GetString("MQTT.clientid"))
opts.SetKeepAlive(30 * time.Second)
opts.SetPingTimeout(10 * time.Second)
opts.SetUsername(viper.GetString("MQTT.username"))
opts.SetPassword(viper.GetString("MQTT.password"))
opts.SetWill("home/status/ds18b20-gateway-01", "dead", 1, true)
c := mqtt.NewClient(opts)
sensors, err := ds18b20.Sensors()
if err != nil {
panic(err)
}
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
c.Publish("home/status/ds18b20-gateway-01", 1, true, "running")
fmt.Printf("sensor IDs: %v\n", sensors)
for _, sensor := range sensors {
topic := "homeassistant/sensor/ds18b20-gateway-01/" + sensor + "/config"
config := `{
"unit_of_measurement": "°F",
"payload_not_available": "dead",
"expire_after": 125,
"force_update": true,
"name": "Temp Sensor ` + sensor + `",
"state_topic": "home/sensor/ds18b20-gateway-01/` + sensor + `",
"availability_topic": "home/status/ds18b20-gateway-01",
"unique_id": "ds18b20-gateway-01-` + sensor + `-f",
"payload_available": "running"
}`
c.Publish(topic, 0, true, config)
}
for true {
for _, sensor := range sensors {
t, err := ds18b20.Temperature(sensor)
if err == nil {
fmt.Printf("sensor: %s temperature: %.2f°C, %.2f°F\n", sensor, t, CtoF(t))
topic := "home/sensor/ds18b20-gateway-01/" + sensor
c.Publish(topic, 0, false, fmt.Sprintf("%f", CtoF(t)))
}
}
time.Sleep(1 * time.Minute)
}
}