-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (63 loc) · 1.68 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
package main
import (
"fmt"
"log"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/tectiv3/wiiscale/wiiboard"
)
var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
log.Printf("TOPIC: %s\n", msg.Topic())
log.Printf("MSG: %s\n", msg.Payload())
}
func main() {
board := wiiboard.New()
if err := board.Detect(); err != nil {
log.Println(err)
return
}
if battery, err := board.Battery(); err != nil {
log.Println(err)
return
} else {
log.Printf("Battery level: %d%%\n", battery)
}
go board.Listen()
// mqtt.DEBUG = log.New(os.Stdout, "", 0)
// mqtt.ERROR = log.New(os.Stdout, "", 0)
opts := mqtt.NewClientOptions().AddBroker("tcp://192.168.100.111:1883").SetClientID("wiiboard")
opts.SetKeepAlive(2 * time.Second)
opts.SetDefaultPublishHandler(f)
opts.SetPingTimeout(1 * time.Second)
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
log.Println("Connected to mqtt")
go func() {
for weight := range board.Weights {
w := weight / 100
w += 1.0
token := c.Publish("sensors/wiiboard/raw", 0, false, fmt.Sprintf(`{"weight": %0.2f}`, w))
token.Wait()
}
}()
for weight := range board.Weight {
w := weight / 100
name := "kin"
if w < 60.0 {
name = "santy"
} else if w > 81.0 {
name = "domi"
}
w += 1.7
log.Printf("%s: %0.2f\n", name, w)
// msg, _ := json.Marshal(struct {
// Weight float64 `json:"weight"`
// Person string `json:"name"`
// }{w, name})
// token := c.Publish("sensors/wiiboard/last", 0, false, msg)
token := c.Publish("sensors/wiiboard/last", 0, false, fmt.Sprintf(`{"%s": %0.2f}`, name, w))
token.Wait()
}
}