2017-01-26 03:19:15 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/platforms/mqtt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
mqttAdaptor := mqtt.NewAdaptor("tcp://test.mosquitto.org:1883", "pinger")
|
2017-01-26 23:21:28 +08:00
|
|
|
mqttAdaptor.SetAutoReconnect(true)
|
|
|
|
|
2017-01-26 03:19:15 +08:00
|
|
|
holaDriver := mqtt.NewDriver(mqttAdaptor, "hola")
|
|
|
|
helloDriver := mqtt.NewDriver(mqttAdaptor, "hello")
|
|
|
|
|
|
|
|
work := func() {
|
2017-01-26 16:13:35 +08:00
|
|
|
helloDriver.On(mqtt.Data, func(data interface{}) {
|
2017-01-26 03:19:15 +08:00
|
|
|
fmt.Println("hello")
|
|
|
|
})
|
|
|
|
|
2017-01-26 16:13:35 +08:00
|
|
|
holaDriver.On(mqtt.Data, func(data interface{}) {
|
2017-01-26 03:19:15 +08:00
|
|
|
fmt.Println("hola")
|
|
|
|
})
|
|
|
|
|
|
|
|
data := []byte("o")
|
|
|
|
gobot.Every(1*time.Second, func() {
|
|
|
|
helloDriver.Publish(data)
|
|
|
|
})
|
|
|
|
|
|
|
|
gobot.Every(5*time.Second, func() {
|
|
|
|
holaDriver.Publish(data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("mqttBot",
|
|
|
|
[]gobot.Connection{mqttAdaptor},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|