hybridgroup.gobot/examples/mqtt_ping.go

41 lines
690 B
Go

// +build example
//
// Do not build by default.
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")
work := func() {
mqttAdaptor.On("hello", func(data []byte) {
fmt.Println("hello")
})
mqttAdaptor.On("hola", func(data []byte) {
fmt.Println("hola")
})
data := []byte("o")
gobot.Every(1*time.Second, func() {
mqttAdaptor.Publish("hello", data)
})
gobot.Every(5*time.Second, func() {
mqttAdaptor.Publish("hola", data)
})
}
robot := gobot.NewRobot("mqttBot",
[]gobot.Connection{mqttAdaptor},
work,
)
robot.Start()
}