-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathbleclient_ollie_mqtt.go
88 lines (72 loc) · 1.62 KB
/
bleclient_ollie_mqtt.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
//go:build example
// +build example
//
// Do not build by default.
package main
import (
"os"
"strconv"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble/sphero"
"gobot.io/x/gobot/v2/platforms/bleclient"
"gobot.io/x/gobot/v2/platforms/mqtt"
)
const (
FRENTE = 0
DERECHA = 90
ATRAS = 180
IZQUIERDA = 270
)
func main() {
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
ollie := sphero.NewOllieDriver(bleAdaptor)
mqttAdaptor := mqtt.NewAdaptor("tcp://iot.eclipse.org:1883", "ollie")
work := func() {
ollie.SetRGB(255, 0, 255)
_ = mqttAdaptor.On("sensors/dial", func(msg mqtt.Message) {
val, _ := strconv.Atoi(string(msg.Payload()))
if val > 2000 {
ollie.SetRGB(0, 255, 0)
return
}
if val > 1000 {
ollie.SetRGB(255, 255, 0)
return
}
ollie.SetRGB(255, 0, 0)
})
_ = mqttAdaptor.On("rover/frente", func(msg mqtt.Message) {
ollie.Roll(40, FRENTE)
gobot.After(1*time.Second, func() {
ollie.Stop()
})
})
_ = mqttAdaptor.On("rover/derecha", func(msg mqtt.Message) {
ollie.Roll(40, DERECHA)
gobot.After(1*time.Second, func() {
ollie.Stop()
})
})
_ = mqttAdaptor.On("rover/atras", func(msg mqtt.Message) {
ollie.Roll(40, ATRAS)
gobot.After(1*time.Second, func() {
ollie.Stop()
})
})
_ = mqttAdaptor.On("rover/izquierda", func(msg mqtt.Message) {
ollie.Roll(40, IZQUIERDA)
gobot.After(1*time.Second, func() {
ollie.Stop()
})
})
}
robot := gobot.NewRobot("ollieBot",
[]gobot.Connection{bleAdaptor, mqttAdaptor},
[]gobot.Device{ollie},
work,
)
if err := robot.Start(); err != nil {
panic(err)
}
}