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