2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-01-27 02:03:32 +08:00
|
|
|
// TO RUN:
|
2017-02-01 04:14:09 +08:00
|
|
|
// go run ./examples/mqtt_driver_ping.go <SERVER>
|
|
|
|
//
|
|
|
|
// EXAMPLE:
|
2017-01-27 02:03:32 +08:00
|
|
|
// go run ./examples/mqtt_driver_ping.go ssl://iot.eclipse.org:8883
|
2017-02-01 04:14:09 +08:00
|
|
|
//
|
2017-01-26 03:19:15 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-01-27 02:03:32 +08:00
|
|
|
"os"
|
2017-01-26 03:19:15 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/platforms/mqtt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-01-27 02:03:32 +08:00
|
|
|
mqttAdaptor := mqtt.NewAdaptor(os.Args[1], "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},
|
2017-04-04 19:38:51 +08:00
|
|
|
[]gobot.Device{helloDriver, holaDriver},
|
2017-01-26 03:19:15 +08:00
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|