2017-01-27 02:03:32 +08:00
|
|
|
// TO RUN:
|
|
|
|
// go run ./examples/mqtt_driver_ping.go tcp://test.mosquitto.org:1883
|
|
|
|
// OR
|
|
|
|
// go run ./examples/mqtt_driver_ping.go ssl://iot.eclipse.org:8883
|
|
|
|
|
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},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|