-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
300 lines (239 loc) · 7.18 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"time"
"github.com/pvainio/scd30"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/host/v3"
"github.com/kelseyhightower/envconfig"
mqttClient "github.com/eclipse/paho.mqtt.golang"
)
type Config struct {
MqttUrl string `envconfig:"mqtt_url" required:"true"`
MqttUser string `envconfig:"mqtt_user"`
MqttPwd string `envconfig:"mqtt_password"`
MqttClientId string `envconfig:"mqtt_client_id" default:"scd30"`
Interval uint16 `envconfig:"interval" default:"50"`
AutoCalibration uint16 `envconfig:"autocal" default:"1"`
ForceCalibration uint16 `envconfig:"forcecal"`
TempOffset uint16 `envconfig:"temp_offset" default:"150"`
Id string `envconfig:"id" default:"scd30"`
Name string `envconfig:"name" default:"SCD30"`
Debug bool `envconfig:"debug" default:"false"`
}
type measurement struct {
id string
time time.Time
value float32
format string
min float32
max float32
}
var (
config Config
logInfo *log.Logger
logDebug *log.Logger
co2 *measurement
humidity *measurement
temperature *measurement
mqtt mqttClient.Client
homeassistantStatus = make(chan string, 10)
)
func main() {
mqtt = connectMqtt()
dev, err := openSCD30()
if err != nil {
log.Fatalf("error %v", err)
}
if config.ForceCalibration > 1 {
forcedCalibration(dev)
return
}
if err := dev.StartMeasurements(config.Interval); err != nil {
log.Fatalf("error %v", err)
}
if err := dev.SetAutomaticSelfCalibration(config.AutoCalibration); err != nil {
log.Fatalf("error %v", err)
}
announceMeToMqttDiscovery(mqtt)
for {
select {
case <-time.After(time.Duration(config.Interval) * time.Second):
checkMeasurement(dev)
case status := <-homeassistantStatus:
if status == "online" {
// HA became online, send discovery so it knows about entities
go announceMeToMqttDiscovery(mqtt)
} else if status != "offline" {
logInfo.Printf("unknown HA status message %s", status)
}
}
}
}
func forcedCalibration(dev *scd30.SCD30) {
err := dev.SetAutomaticSelfCalibration(0)
if err := dev.StartMeasurements(2); err != nil {
log.Fatalf("error %v", err)
}
fmt.Printf("Forced calibration %v ppm, waiting for 2 minutes\n", config.ForceCalibration)
time.Sleep(125 * time.Second)
err = dev.SetForcedCalibration(config.ForceCalibration)
if err != nil {
log.Fatalf("error %v", err)
}
fmt.Printf("Forced calibration %v ppm done\n", config.ForceCalibration)
}
func openSCD30() (*scd30.SCD30, error) {
if _, err := host.Init(); err != nil {
return nil, err
}
bus, err := i2creg.Open("")
if err != nil {
return nil, err
}
dev, err := scd30.Open(bus)
if err != nil {
return nil, err
}
var to uint16 = config.TempOffset
if o, err := dev.GetTemperatureOffset(); err != nil {
return nil, err
} else {
logInfo.Printf("Got temp offset %d", o)
if o != to {
logInfo.Printf("Setting offset to %d", to)
if err := dev.SetTemperatureOffset(to); err != nil {
return nil, err
}
}
}
return dev, nil
}
func checkMeasurement(dev *scd30.SCD30) {
if has, err := dev.HasMeasurement(); err != nil {
log.Fatalf("error %v", err)
} else if !has {
return
}
m, err := dev.GetMeasurement()
if err != nil {
log.Fatalf("error %v", err)
}
logDebug.Printf("Got measure %f ppm %f%% %fC", m.CO2, m.Humidity, m.Temperature)
publishIfNeeded(m.CO2, co2, 50)
publishIfNeeded(m.Temperature, temperature, 0.5)
publishIfNeeded(m.Humidity, humidity, 3)
}
func publishIfNeeded(current float32, old *measurement, diff float64) {
if time.Since(old.time) < 600*time.Second && math.Abs(float64(current)-float64(old.value)) < diff {
return
}
if current < old.min || current > old.max {
logInfo.Printf("value for %s is out of range %f", old.id, current)
return
}
old.time = time.Now()
old.value = current
publish(mqtt, stateTopic(old.id), fmt.Sprintf(old.format, current))
}
func subscribe(mqtt mqttClient.Client) {
logInfo.Print("subscribed to topics")
mqtt.Subscribe("homeassistant/status", 0, haStatusHandler)
}
func haStatusHandler(mqtt mqttClient.Client, msg mqttClient.Message) {
body := string(msg.Payload())
logInfo.Printf("received HA status %s", body)
homeassistantStatus <- body
}
func announceMeToMqttDiscovery(mqtt mqttClient.Client) {
publishDiscovery(mqtt, "co2", "co2", "ppm", "carbon_dioxide")
publishDiscovery(mqtt, "temperature", "temperature", "°C", "temperature")
publishDiscovery(mqtt, "humidity", "humidity", "%", "humidity")
}
func publishDiscovery(mqtt mqttClient.Client, id string, name string, unit string, class string) {
uid := config.Id + "_" + id
pname := config.Name + " " + name
discoveryTopic := fmt.Sprintf("homeassistant/sensor/%s/config", uid)
msg := discoveryMsg(id, uid, pname, unit, class)
publish(mqtt, discoveryTopic, msg)
}
func publish(mqtt mqttClient.Client, topic string, msg interface{}) {
logDebug.Printf("publish to %s: %s", topic, msg)
t := mqtt.Publish(topic, 0, false, msg)
go func() {
_ = t.Wait()
if t.Error() != nil {
logInfo.Printf("publishing msg failed %v", t.Error())
}
}()
}
func discoveryMsg(id string, uid string, name string, unit string, class string) []byte {
msg := make(map[string]interface{})
msg["unique_id"] = uid
msg["name"] = name
dev := make(map[string]string)
msg["device"] = dev
dev["identifiers"] = config.Id
dev["manufacturer"] = "Sensirion"
dev["name"] = "Sensirion SCD30"
dev["model"] = "SCD30"
msg["state_topic"] = stateTopic(id)
msg["expire_after"] = 1800
msg["unit_of_measurement"] = unit
msg["state_class"] = "measurement"
msg["device_class"] = class
jsonm, err := json.Marshal(msg)
if err != nil {
logInfo.Printf("cannot marshal json %v", err)
}
return jsonm
}
func stateTopic(id string) string {
return "scd30/" + config.Id + "/" + id
}
func init() {
co2 = &measurement{id: "co2", format: "%.0f", min: 100, max: 10000}
humidity = &measurement{id: "humidity", format: "%.0f", min: 1, max: 100}
temperature = &measurement{id: "temperature", format: "%.1f", min: -50, max: 150}
err := envconfig.Process("scd30", &config)
if err != nil {
log.Fatal(err.Error())
}
logInfo = log.New(os.Stdout, "INFO ", log.Ldate|log.Ltime|log.Lmsgprefix)
if config.Debug {
logDebug = log.New(os.Stdout, "DEBUG ", log.Ldate|log.Ltime|log.Lmsgprefix)
} else {
logDebug = log.New(ioutil.Discard, "DEBUG ", 0)
}
}
func connectHandler(client mqttClient.Client) {
options := client.OptionsReader()
logInfo.Printf("MQTT connected to %s", options.Servers())
subscribe(client)
}
func connectMqtt() mqttClient.Client {
opts := mqttClient.NewClientOptions().
AddBroker(config.MqttUrl).
SetClientID(config.MqttClientId).
SetOrderMatters(false).
SetKeepAlive(150 * time.Second).
SetAutoReconnect(true).
SetOnConnectHandler(connectHandler)
if len(config.MqttUser) > 0 {
opts = opts.SetUsername(config.MqttUser)
}
if len(config.MqttPwd) > 0 {
opts = opts.SetPassword(config.MqttPwd)
}
logInfo.Printf("connecting to mqtt %s client id %s user %s", opts.Servers, opts.ClientID, opts.Username)
c := mqttClient.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
return c
}