-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (107 loc) · 2.67 KB
/
main.go
File metadata and controls
134 lines (107 loc) · 2.67 KB
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
package main
import (
"errors"
"flag"
"fmt"
"os"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/sstallion/go-hid"
)
var (
temperature float32
co2 uint16
)
func getData(dev *hid.Device) error {
buf := make([]byte, 8)
i, err := dev.Read(buf)
if err != nil {
return err
}
if i != len(buf) {
return errors.New("wrong read bug size")
}
decode(buf)
return nil
}
func decode(buf []byte) {
res := make([]byte, 8)
var c byte
for _, n := range [][]int{{0, 2}, {1, 4}, {3, 7}, {5, 6}} {
c = buf[n[0]]
buf[n[0]] = buf[n[1]]
buf[n[1]] = c
}
var tmp = buf[7] << 5
res[7] = (buf[6] << 5) | (buf[7] >> 3)
res[6] = (buf[5] << 5) | (buf[6] >> 3)
res[5] = (buf[4] << 5) | (buf[5] >> 3)
res[4] = (buf[3] << 5) | (buf[4] >> 3)
res[3] = (buf[2] << 5) | (buf[3] >> 3)
res[2] = (buf[1] << 5) | (buf[2] >> 3)
res[1] = (buf[0] << 5) | (buf[1] >> 3)
res[0] = tmp | (buf[0] >> 3)
var magicWord = []byte("Htemp99e")
for i := 0; i < 8; i++ {
res[i] -= (magicWord[i] << 4) | (magicWord[i] >> 4)
}
if res[3] != res[0]+res[1]+res[2] {
return
}
var w uint16
w = uint16(res[1])*256 + uint16(res[2])
switch res[0] {
case 0x42:
temperature = float32(w)*0.0625 - 273.15
case 0x50:
co2 = w
}
}
func sendMqtt(server, topic, user, password string) error {
opts := MQTT.NewClientOptions()
opts.AddBroker(server)
opts.SetClientID("mqtt-go-sender")
opts.SetUsername(user)
opts.SetPassword(password)
client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
return token.Error()
}
token := client.Publish(topic+"/temperature", 0, false, fmt.Sprintf("%.2f", temperature))
token.Wait()
token = client.Publish(topic+"/co2", 0, false, fmt.Sprintf("%d", co2))
token.Wait()
client.Disconnect(250)
return nil
}
func main() {
server := flag.String("server", "", "The broker URI. ex: tcp://192.168.0.1:1883")
topic := flag.String("topic", "dadget/room", "Base topic to send to")
user := flag.String("user", "", "The User (optional)")
password := flag.String("password", "", "The password (optional)")
flag.Parse()
dev, err := hid.OpenFirst(0x04d9, 0xa052)
if err != nil {
fmt.Printf("can't open device - %v\n", err)
os.Exit(1)
}
time.AfterFunc(time.Second*60, func() {
println("timeout...")
os.Exit(2)
})
magic := make([]byte, 8)
dev.SendFeatureReport(magic)
for temperature == 0 || co2 == 0 {
if err := getData(dev); err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
fmt.Print(".")
}
fmt.Printf("\n\ntemp: %.2f\nco2: %d\n", temperature, co2)
if *server != "" {
if err := sendMqtt(*server, *topic, *user, *password); err != nil {
fmt.Printf("error: %v\n", err)
}
}
}