gobot.io/x/gobot@v1.16.0/examples/mqtt_ping.go (about) 1 // +build example 2 // 3 // Do not build by default. 4 5 package main 6 7 import ( 8 "fmt" 9 "time" 10 11 "gobot.io/x/gobot" 12 "gobot.io/x/gobot/platforms/mqtt" 13 ) 14 15 func main() { 16 mqttAdaptor := mqtt.NewAdaptor("tcp://test.mosquitto.org:1883", "pinger") 17 18 work := func() { 19 mqttAdaptor.On("hello", func(msg mqtt.Message) { 20 fmt.Println("hello") 21 }) 22 mqttAdaptor.On("hola", func(msg mqtt.Message) { 23 fmt.Println("hola") 24 }) 25 data := []byte("o") 26 gobot.Every(1*time.Second, func() { 27 mqttAdaptor.Publish("hello", data) 28 }) 29 gobot.Every(5*time.Second, func() { 30 mqttAdaptor.Publish("hola", data) 31 }) 32 } 33 34 robot := gobot.NewRobot("mqttBot", 35 []gobot.Connection{mqttAdaptor}, 36 work, 37 ) 38 39 robot.Start() 40 }