forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_firmata_blink.go
44 lines (36 loc) · 981 Bytes
/
mqtt_firmata_blink.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
package main
import (
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/mqtt"
)
func main() {
gbot := gobot.NewGobot()
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://test.mosquitto.org:1883", "blinker")
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "13")
work := func() {
mqttAdaptor.On("lights/on", func(data []byte) {
led.On()
})
mqttAdaptor.On("lights/off", func(data []byte) {
led.Off()
})
data := []byte("")
gobot.Every(1*time.Second, func() {
mqttAdaptor.Publish("lights/on", data)
})
gobot.Every(2*time.Second, func() {
mqttAdaptor.Publish("lights/off", data)
})
}
robot := gobot.NewRobot("mqttBot",
[]gobot.Connection{mqttAdaptor, firmataAdaptor},
[]gobot.Device{led},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}